Пример #1
0
        public async Task <int> RemoveFromCartAsync(Pizzas pizza)
        {
            var shoppingCartItem =
                await _appDbContext.ShoppingCartItems.SingleOrDefaultAsync(
                    s => s.Pizza.Id == pizza.Id && s.ShoppingCartId == ShoppingCartId);

            var localAmount = 0;

            if (shoppingCartItem != null)
            {
                if (shoppingCartItem.Amount > 1)
                {
                    shoppingCartItem.Amount--;
                    localAmount = shoppingCartItem.Amount;
                }
                else
                {
                    _appDbContext.ShoppingCartItems.Remove(shoppingCartItem);
                }
            }

            await _appDbContext.SaveChangesAsync();

            return(localAmount);
        }
Пример #2
0
        public async Task AddToCartAsync(Pizzas pizza, int amount)
        {
            var shoppingCartItem =
                await _appDbContext.ShoppingCartItems.SingleOrDefaultAsync(
                    s => s.Pizza.Id == pizza.Id && s.ShoppingCartId == ShoppingCartId);

            if (shoppingCartItem == null)
            {
                shoppingCartItem = new ShoppingCartItem
                {
                    ShoppingCartId = ShoppingCartId,
                    Pizza          = pizza,
                    Amount         = 1
                };

                _appDbContext.ShoppingCartItems.Add(shoppingCartItem);
            }
            else
            {
                shoppingCartItem.Amount++;
            }

            await _appDbContext.SaveChangesAsync();
        }