public ActionResult BasketPage() { var basket = basketManager.GetBasket(User.Identity.GetUserId()); var products = productManager.GetById(basket.Products.Select(it => it.ProductId)); var basketProductViewModelsList = products.Select(it => { var basketProductViewModel = Mapper.Map <BasketProductViewModel>(it); basketProductViewModel.Count = basket.Products.FirstOrDefault(p => p.ProductId == basketProductViewModel.Id).Count; return(basketProductViewModel); }); var basketPrice = 0m; foreach (var p in basketProductViewModelsList) { basketPrice += p.Price * p.Count; } var model = new BasketViewModel() { Products = basketProductViewModelsList.ToList(), TotalPrice = basketPrice }; return(View(model)); }
public IActionResult Purchase() { var basket = BasketManager.GetBasket(HttpContext); basket.Purchase(); HttpContext.Session.SetString("basket", new BasketStringDecorator(basket).ConvertToString()); return(RedirectToAction("Index")); }
// GET: Basket public ActionResult Index() { var basketPosition = basketManager.GetBasket(); var fullPrice = basketManager.GetBasketValue(); BasketViewModel basketVM = new BasketViewModel() { BasketPositions = basketPosition, FullPrice = fullPrice }; return(View(basketVM)); }
public IActionResult AddToBasket([FromForm] int id) { Logger.Log($"Request to add product to basket with id: {id}"); var product = ProductFactory.CreateProduct(id); if (product.StockChecker.GetStock() <= 0) { Logger.Log($"Product {id} out of stock."); ViewBag.Error = "Out of stock"; var products = ProductRepository.GetAll(); return(View("Index", products)); } var basket = BasketManager.GetBasket(HttpContext); basket.Add(product); HttpContext.Session.SetString("basket", new BasketStringDecorator(basket).ConvertToString()); return(RedirectToAction("Index")); }
public IActionResult Index() { var basket = BasketManager.GetBasket(HttpContext); return(View(basket)); }