private async Task <int> AddOrRemoveCartAsync(AutomotivePart part, int qty) { var shoppingCartItem = _context.ShoppingCartItems .SingleOrDefault(s => s.AutomotivePartId == part.Id && s.ShoppingCartId == Id); if (shoppingCartItem == null) { shoppingCartItem = new ShoppingCartItem { ShoppingCartId = Id, AutomotivePart = part, Quantity = 0 }; _context.ShoppingCartItems.Add(shoppingCartItem); } shoppingCartItem.Quantity += qty; if (shoppingCartItem.Quantity <= 0) { shoppingCartItem.Quantity = 0; _context.ShoppingCartItems.Remove(shoppingCartItem); } await _context.SaveChangesAsync(); ShoppingCartItems = null; return(await Task.FromResult(shoppingCartItem.Quantity)); }
public async Task <int> RemoveFromCartAsync(AutomotivePart part) { return(await AddOrRemoveCartAsync(part, -1)); }
public async Task <int> AddToCartAsync(AutomotivePart part) { return(await AddOrRemoveCartAsync(part, 1)); }