diff --git a/SportsStore/SportsStore.WebUI/Controllers/CartController.cs b/SportsStore/SportsStore.WebUI/Controllers/CartController.cs new file mode 100644 index 0000000..1bc57cb --- /dev/null +++ b/SportsStore/SportsStore.WebUI/Controllers/CartController.cs @@ -0,0 +1,56 @@ +using SportsStore.Domain.Abstract; +using SportsStore.Domain.Entities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.Mvc; + +namespace SportsStore.WebUI.Controllers +{ + public class CartController : Controller + { + private IProductRepository repository; + + public CartController(IProductRepository repo) + { + repository = repo; + } + + // 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; + } + } +} \ No newline at end of file diff --git a/SportsStore/SportsStore.WebUI/SportsStore.WebUI.csproj b/SportsStore/SportsStore.WebUI/SportsStore.WebUI.csproj index 6ce6275..0072a59 100644 --- a/SportsStore/SportsStore.WebUI/SportsStore.WebUI.csproj +++ b/SportsStore/SportsStore.WebUI/SportsStore.WebUI.csproj @@ -130,6 +130,7 @@ + @@ -159,6 +160,7 @@ +