60 lines
1.6 KiB
C#
60 lines
1.6 KiB
C#
using SportsStore.Domain.Abstract;
|
|
using SportsStore.Domain.Entities;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using System.Web.Mvc;
|
|
using SportsStore.WebUI.Models;
|
|
|
|
namespace SportsStore.WebUI.Controllers
|
|
{
|
|
public class CartController : Controller
|
|
{
|
|
private IProductRepository repository;
|
|
|
|
public CartController(IProductRepository repo)
|
|
{
|
|
repository = repo;
|
|
}
|
|
|
|
public ViewResult Index(Cart cart, string returnUrl)
|
|
{
|
|
return View(new CartIndexViewModel
|
|
{
|
|
Cart = cart,
|
|
ReturnUrl = returnUrl
|
|
});
|
|
}
|
|
|
|
// GET: Cart
|
|
public RedirectToRouteResult AddToCart(Cart cart, int productId, string returnUrl)
|
|
{
|
|
Product product = repository.Products
|
|
.FirstOrDefault(p => p.ProductID == productId);
|
|
|
|
if (product != null)
|
|
{
|
|
cart.AddItem(product, 1);
|
|
}
|
|
return RedirectToAction("Index", new { returnUrl });
|
|
}
|
|
|
|
public RedirectToRouteResult RemoveFromCart(Cart cart, int productId, string returnUrl)
|
|
{
|
|
Product product = repository.Products
|
|
.FirstOrDefault(p => p.ProductID == productId);
|
|
|
|
if (product != null)
|
|
{
|
|
cart.RemoveLine(product);
|
|
}
|
|
return RedirectToAction("Index", new { returnUrl });
|
|
}
|
|
|
|
public PartialViewResult Summary(Cart cart)
|
|
{
|
|
return PartialView(cart);
|
|
}
|
|
}
|
|
} |