public int RemoveFromCart(Candy candy) { var shoppingCartItem = _appDbContext.ShoppingCartItems.FirstOrDefault (x => x.Candy.CandyId == candy.CandyId && x.ShoppingCartId == ShoppingCartId); var localAmount = 0; if (shoppingCartItem != null) { if (shoppingCartItem.Amount > 1) { shoppingCartItem.Amount--; localAmount = shoppingCartItem.Amount; } else { _appDbContext.ShoppingCartItems.Remove(shoppingCartItem); } } _appDbContext.SaveChanges(); return(localAmount); }
public void AddToCart(Candy candy, int amount) { var shoppingCartItem = _appDbContext.ShoppingCartItems.SingleOrDefault(s => s.Candy == candy && s.ShoppingCartId == ShoppingCartId); if (shoppingCartItem == null) { shoppingCartItem = new ShoppingCartItem { ShoppingCartId = ShoppingCartId, Candy = candy, Amount = amount }; _appDbContext.ShoppingCartItems.Add(shoppingCartItem); } else { shoppingCartItem.Amount++; } _appDbContext.SaveChanges(); }
public int RemoveFromCart(Candy candy) { var shoppingCartItem = _appDbContext.ShoppingCartItems.FirstOrDefault(s => s.Candy.CandyId == candy.CandyId && s.ShoppingCartId == ShoppingCartId); var localAmount = 0; if (shoppingCartItem != null) { shoppingCartItem.Amount--; localAmount = shoppingCartItem.Amount; if (shoppingCartItem.Amount == 0) { _appDbContext.ShoppingCartItems.Remove(shoppingCartItem); } _appDbContext.SaveChanges(); } return(localAmount); }
public void AddToCart(Candy candy, int amount) { var shoppingCartItem = _appDbContext.ShoppingCartItems.FirstOrDefault (x => x.Candy.CandyId == candy.CandyId && x.ShoppingCartId == ShoppingCartId); if (shoppingCartItem == null) { shoppingCartItem = new ShoppingCartItem { ShoppingCartId = ShoppingCartId, Candy = candy, Amount = amount }; _appDbContext.ShoppingCartItems.Add(shoppingCartItem); } else { shoppingCartItem.Amount += 1; } _appDbContext.SaveChanges(); }