public void AddToCart(Tire tire, int amount) { var shoppingCartItem = _appDbContext.ShopppingCartItems.SingleOrDefault( s => s.Tire.TireId == tire.TireId && s.ShoppingCartId == ShoppingCartId); if (shoppingCartItem == null) { shoppingCartItem = new ShoppingCartItem { ShoppingCartId = ShoppingCartId, Tire = tire, Amount = amount }; _appDbContext.ShopppingCartItems.Add(shoppingCartItem); } else { shoppingCartItem.Amount++; } _appDbContext.SaveChanges(); }
public int RemoveFromCart(Tire tire) { var shoppingCartItem = _appDbContext.ShopppingCartItems.SingleOrDefault( s => s.Tire.TireId == tire.TireId && s.ShoppingCartId == ShoppingCartId); var localAmount = 0; if (shoppingCartItem != null) { if (shoppingCartItem.Amount > 1) { shoppingCartItem.Amount--; localAmount = shoppingCartItem.Amount; } else { _appDbContext.ShopppingCartItems.Remove(shoppingCartItem); } } _appDbContext.SaveChanges(); return(localAmount); }