2021-09-13 00:23:59 +10:00
|
|
|
|
using SportsStore.Domain.Abstract;
|
|
|
|
|
using SportsStore.Domain.Entities;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Web;
|
|
|
|
|
using System.Web.Mvc;
|
2021-09-18 18:25:55 +10:00
|
|
|
|
using SportsStore.WebUI.Models;
|
2021-09-13 00:23:59 +10:00
|
|
|
|
|
|
|
|
|
namespace SportsStore.WebUI.Controllers
|
|
|
|
|
{
|
|
|
|
|
public class CartController : Controller
|
|
|
|
|
{
|
|
|
|
|
private IProductRepository repository;
|
|
|
|
|
|
|
|
|
|
public CartController(IProductRepository repo)
|
|
|
|
|
{
|
|
|
|
|
repository = repo;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 23:15:05 +10:00
|
|
|
|
public ViewResult Index(Cart cart, string returnUrl)
|
2021-09-18 18:25:55 +10:00
|
|
|
|
{
|
|
|
|
|
return View(new CartIndexViewModel
|
|
|
|
|
{
|
2021-09-18 23:15:05 +10:00
|
|
|
|
Cart = cart,
|
2021-09-18 18:25:55 +10:00
|
|
|
|
ReturnUrl = returnUrl
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-13 00:23:59 +10:00
|
|
|
|
// GET: Cart
|
2021-09-18 23:15:05 +10:00
|
|
|
|
public RedirectToRouteResult AddToCart(Cart cart, int productId, string returnUrl)
|
2021-09-13 00:23:59 +10:00
|
|
|
|
{
|
|
|
|
|
Product product = repository.Products
|
|
|
|
|
.FirstOrDefault(p => p.ProductID == productId);
|
|
|
|
|
|
|
|
|
|
if (product != null)
|
|
|
|
|
{
|
2021-09-18 23:15:05 +10:00
|
|
|
|
cart.AddItem(product, 1);
|
2021-09-13 00:23:59 +10:00
|
|
|
|
}
|
|
|
|
|
return RedirectToAction("Index", new { returnUrl });
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-18 23:15:05 +10:00
|
|
|
|
public RedirectToRouteResult RemoveFromCart(Cart cart, int productId, string returnUrl)
|
2021-09-13 00:23:59 +10:00
|
|
|
|
{
|
|
|
|
|
Product product = repository.Products
|
|
|
|
|
.FirstOrDefault(p => p.ProductID == productId);
|
|
|
|
|
|
|
|
|
|
if (product != null)
|
|
|
|
|
{
|
2021-09-18 23:15:05 +10:00
|
|
|
|
cart.RemoveLine(product);
|
2021-09-13 00:23:59 +10:00
|
|
|
|
}
|
|
|
|
|
return RedirectToAction("Index", new { returnUrl });
|
|
|
|
|
}
|
2021-09-22 11:15:51 +10:00
|
|
|
|
|
|
|
|
|
public PartialViewResult Summary(Cart cart)
|
|
|
|
|
{
|
|
|
|
|
return PartialView(cart);
|
|
|
|
|
}
|
2021-09-13 00:23:59 +10:00
|
|
|
|
}
|
|
|
|
|
}
|