public void AddItem(Snack snack, int quantity) { ShoppingCartItem shoppingCartItem = _context .ShoppingCartItems .SingleOrDefault(sci => sci.Snack.Id == snack.Id && sci.ShoppingCartId == ShoppingCartId); if (shoppingCartItem == null) { shoppingCartItem = new ShoppingCartItem { ShoppingCartId = ShoppingCartId, Snack = snack, Quantity = quantity }; _context.ShoppingCartItems.Add(shoppingCartItem); } else { shoppingCartItem.Quantity++; } _context.SaveChanges(); }
public int RemoveItem(Snack snack) { ShoppingCartItem shoppingCartItem = _context .ShoppingCartItems .SingleOrDefault(sci => sci.Snack.Id == snack.Id && sci.ShoppingCartId == ShoppingCartId); int currentQuantity = 0; if (shoppingCartItem != null) { if (shoppingCartItem.Quantity > 1) { shoppingCartItem.Quantity--; currentQuantity = shoppingCartItem.Quantity; } else { _context.ShoppingCartItems.Remove(shoppingCartItem); } _context.SaveChanges(); } return(currentQuantity); }