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