//Remove from cart public int RemoveFromCart(CafeProducts product) { //Gets product from cart var shoppingCartItem = _DbContext.ShoppingCartItems.SingleOrDefault( s => s.Products.ProductId == product.ProductId && s.ShoppingCartID == ShoppingCartID); //Define local amount of items in cart var localAmount = 0; if (shoppingCartItem != null) { if (shoppingCartItem.Amount > 1) { //Reduce items in cart by 1 shoppingCartItem.Amount--; //Update variable localAmount = shoppingCartItem.Amount; } else { //Remove shoppingCart object _DbContext.ShoppingCartItems.Remove(shoppingCartItem); } } _DbContext.SaveChanges(); return(localAmount); }
public async Task <IActionResult> PostCafeProducts([FromBody] CafeProducts cafeProducts) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _context.CafeProducts.Add(cafeProducts); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { if (CafeProductsExists(cafeProducts.ProductId)) { return(new StatusCodeResult(StatusCodes.Status409Conflict)); } else { throw; } } return(CreatedAtAction("GetCafeProducts", new { id = cafeProducts.ProductId }, cafeProducts)); }
//Add to Cart public void AddToCart(CafeProducts product, int amount) { //Gets product from cart var shoppingCartItem = _DbContext.ShoppingCartItems.SingleOrDefault( s => s.Products.ProductId == product.ProductId && s.ShoppingCartID == ShoppingCartID); //If no items are in cart if (shoppingCartItem == null) { //Create new object of cart shoppingCartItem = new ShoppingCartItem { ShoppingCartID = ShoppingCartID, Products = product, Amount = 1 }; //Add to database _DbContext.ShoppingCartItems.Add(shoppingCartItem); } else { //Increment item amount of existing cart shoppingCartItem.Amount++; } _DbContext.SaveChanges(); }
public async Task <IActionResult> PutCafeProducts([FromRoute] int id, [FromBody] CafeProducts cafeProducts) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != cafeProducts.ProductId) { return(BadRequest()); } _context.Entry(cafeProducts).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CafeProductsExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> EditProduct(int id, [Bind("ProductId,ProductType,ProductPrice,ProductName,ProductCalories,ProductDetails,ProductImage")] CafeProducts cafeProducts) { if (id != cafeProducts.ProductId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(cafeProducts); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CafeProductsExists(cafeProducts.ProductId)) { return(NotFound()); } else { throw; } } ViewBag.Success = true; return(View()); } return(View(cafeProducts)); }
public async Task <IActionResult> Create([Bind("ProductId,ProductType,ProductPrice,ProductName,ProductCalories,ProductDetails")] CafeProducts cafeProducts) { if (ModelState.IsValid) { _context.Add(cafeProducts); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Menu))); } return(View(cafeProducts)); }
//Adds to quantity (Specfiic for quantity bar) public RedirectToActionResult AddToQuantity(int productID) { CafeProducts selectedProduct = new CafeProducts(); selectedProduct = _productRepository.GetProductById(productID); if (selectedProduct != null) { _shoppingCart.AddToCart(selectedProduct, 1); } return(RedirectToAction("Index")); }
public async Task <IActionResult> CreateProduct(CafeProducts product) { if (ModelState.IsValid) { CafeProducts lastProduct = _context.CafeProducts.LastOrDefault(c => c.ProductId == c.ProductId); if (lastProduct != null) { product.ProductId = lastProduct.ProductId + 1; } else { product.ProductId = 1; } _context.Add(product); await _context.SaveChangesAsync(); ViewBag.Success = true; return(View()); } return(View()); }