示例#1
0
        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));
        }
示例#2
0
 public async Task <int> RemoveFromCartAsync(AutomotivePart part)
 {
     return(await AddOrRemoveCartAsync(part, -1));
 }
示例#3
0
 public async Task <int> AddToCartAsync(AutomotivePart part)
 {
     return(await AddOrRemoveCartAsync(part, 1));
 }