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 18:25:55 +10:00
|
|
|
|
public ViewResult Index(string returnUrl)
|
|
|
|
|
{
|
|
|
|
|
return View(new CartIndexViewModel
|
|
|
|
|
{
|
|
|
|
|
Cart = GetCart(),
|
|
|
|
|
ReturnUrl = returnUrl
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-13 00:23:59 +10:00
|
|
|
|
// GET: Cart
|
|
|
|
|
public RedirectToRouteResult AddToCart(int productId, string returnUrl)
|
|
|
|
|
{
|
|
|
|
|
Product product = repository.Products
|
|
|
|
|
.FirstOrDefault(p => p.ProductID == productId);
|
|
|
|
|
|
|
|
|
|
if (product != null)
|
|
|
|
|
{
|
|
|
|
|
GetCart().AddItem(product, 1);
|
|
|
|
|
}
|
|
|
|
|
return RedirectToAction("Index", new { returnUrl });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public RedirectToRouteResult RemoveFromCart(int productId, string returnUrl)
|
|
|
|
|
{
|
|
|
|
|
Product product = repository.Products
|
|
|
|
|
.FirstOrDefault(p => p.ProductID == productId);
|
|
|
|
|
|
|
|
|
|
if (product != null)
|
|
|
|
|
{
|
|
|
|
|
GetCart().RemoveLine(product);
|
|
|
|
|
}
|
|
|
|
|
return RedirectToAction("Index", new { returnUrl });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Cart GetCart()
|
|
|
|
|
{
|
|
|
|
|
Cart cart = (Cart)Session["Cart"];
|
|
|
|
|
if (cart == null)
|
|
|
|
|
{
|
|
|
|
|
cart = new Cart();
|
|
|
|
|
Session["Cart"] = cart;
|
|
|
|
|
}
|
|
|
|
|
return cart;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|