public IActionResult Buy() { if (Request.HasFormContentType == true) { int productID = Int32.Parse(Request.Form["product-id"]); int productQuantity = Int32.Parse(Request.Form["product-qty"]); string currentCart = Request.Cookies["COOKIE_CART_CONTENT"]; List <CartElement> currentCartList = new List <CartElement>(); if (currentCart != "") { currentCartList = JsonSerializer.Deserialize <List <CartElement> >(currentCart); } CartElement cartElement = new CartElement { ProductID = productID, ProductQuantity = productQuantity }; currentCartList.Add(cartElement); currentCart = JsonSerializer.Serialize <List <CartElement> >(currentCartList); Response.Cookies.Delete("COOKIE_CART_CONTENT"); Response.Cookies.Append("COOKIE_CART_CONTENT", currentCart); return(RedirectToAction("Index", "User")); } else { TempData["ErrorHeader"] = "Data transfer error"; TempData["ErrorMessage"] = "Did not received any data, form is empty"; return(RedirectToAction("Error", "User")); } }
public void AddToCart(Product product, int count) { var cartElement = ( from element in _context.CartElement where element.SessionId == this.CartSessionId && element.ProductId == product.Id select element ).FirstOrDefault(); if (cartElement == null) { cartElement = new CartElement() { SessionId = this.CartSessionId, ProductId = product.Id, Count = count, CreatedAt = DateTime.Now }; _context.CartElement.Add(cartElement); } else { cartElement.Count += count; } _context.SaveChanges(); }
public void AddToCart([FromBody] CartElement model) { repository.AddToCart(model); }
public void AddToCart(CartElement model) { log.Info("Adding to cart model with CarId " + model.CarId); context.Cart.Add(model); context.SaveChanges(); }