public async Task AddToCartAsync(Clothes clothes, int amount) { var shoppingCartItem = await db.ShoppingCartItems.SingleOrDefaultAsync( s => s.Clothes.Id == clothes.Id && s.ShoppingCartId == ShoppingCartId); if (shoppingCartItem == null) { shoppingCartItem = new ShoppingCartItem { ShoppingCartId = ShoppingCartId, Clothes = clothes, Amount = 1 }; db.ShoppingCartItems.Add(shoppingCartItem); } else { shoppingCartItem.Amount++; } await db.SaveChangesAsync(); }
public async Task <int> RemoveFromCartAsync(Clothes clothes) { var shoppingCartItem = await db.ShoppingCartItems.SingleOrDefaultAsync( s => s.Clothes.Id == clothes.Id && s.ShoppingCartId == ShoppingCartId); var localAmount = 0; if (shoppingCartItem != null) { if (shoppingCartItem.Amount > 1) { shoppingCartItem.Amount--; localAmount = shoppingCartItem.Amount; } else { db.ShoppingCartItems.Remove(shoppingCartItem); } } await db.SaveChangesAsync(); return(localAmount); }