public void AddToCart(Pie pie, int amount) { var shoppingCartItem = _context.ShoppingCartItems.SingleOrDefault(s => s.Pie.PieId == pie.PieId && s.ShoppingCartId == ShoppingCartId); if (shoppingCartItem == null) { shoppingCartItem = new ShoppingCartItem { ShoppingCartId = ShoppingCartId, Pie = pie, Amount = 1 }; _context.ShoppingCartItems.Add(shoppingCartItem); } else { _context.SaveChanges(); } }
public int RemoveFromCart(Pie pie) { var shoppingCartItem = _context.ShoppingCartItems.SingleOrDefault(s => s.Pie.PieId == pie.PieId && s.ShoppingCartId == ShoppingCartId); var localAmount = 0; if (shoppingCartItem != null) { if (shoppingCartItem.Amount > 1) { shoppingCartItem.Amount--; localAmount = shoppingCartItem.Amount; } else { _context.ShoppingCartItems.Remove(shoppingCartItem); } } _context.SaveChanges(); return(localAmount); }