示例#1
0
        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();
        }
示例#2
0
        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);
        }