public IActionResult Buy(int id, string size, int number) { if (SesionHelper.GetObjectFromJson <List <ShoppingCartItem> >(HttpContext.Session, "cart") == null) { List <ShoppingCartItem> cart = new List <ShoppingCartItem>(); var productModel = _context.Products.Where(x => x.Id == id).Include(x => x.Photos).First(); cart.Add(new ShoppingCartItem { Product = productModel, Quantity = number, Size = (SizeOfPruduct)Enum.Parse(typeof(SizeOfPruduct), size) }); SesionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart); } else { List <ShoppingCartItem> cart = SesionHelper.GetObjectFromJson <List <ShoppingCartItem> >(HttpContext.Session, "cart"); int index = ifExist(id, size); if (index != -1) { cart[index].Quantity++; } else { var productModel = _context.Products.Where(x => x.Id == id).Include(x => x.Photos).First(); cart.Add(new ShoppingCartItem { Product = productModel, Quantity = number, Size = (SizeOfPruduct)Enum.Parse(typeof(SizeOfPruduct), size) }); } SesionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart); } return(RedirectToAction("Index")); }
public IActionResult Remove(int id) { List <ShoppingCartItem> cart = SesionHelper.GetObjectFromJson <List <ShoppingCartItem> >(HttpContext.Session, "cart"); int index = ifExist(id); cart.RemoveAt(index); SesionHelper.SetObjectAsJson(HttpContext.Session, "cart", cart); return(RedirectToAction("Index")); }