コード例 #1
0
        public async Task <int> SetItemAmount(Merchandise merchandise, int amount)
        {
            AppDbContext     appDbContext     = _appDbContext;
            ShoppingCartItem shoppingCartItem = await appDbContext.ShoppingCartItems.SingleOrDefaultAsync(s => s.Merchandise.MerchandiseId == merchandise.MerchandiseId && s.ShoppingCartId == ShoppingCartId);

            int localAmount = 0;

            shoppingCartItem.Amount = amount;

            if (shoppingCartItem.Amount > 0)
            {
                localAmount = shoppingCartItem.Amount;
            }
            else
            {
                appDbContext.ShoppingCartItems.Remove(shoppingCartItem);
            }


            await appDbContext.SaveChangesAsync();

            return(localAmount);
        }
コード例 #2
0
        public async Task UpdateCartItem(Merchandise merchandise, int amount)
        {
            AppDbContext     appDbContext     = _appDbContext;
            ShoppingCartItem shoppingCartItem = await appDbContext.ShoppingCartItems.SingleOrDefaultAsync(s => s.Merchandise.MerchandiseId == merchandise.MerchandiseId && s.ShoppingCartId == ShoppingCartId);

            if (shoppingCartItem == null)
            {
                shoppingCartItem = new ShoppingCartItem
                {
                    ShoppingCartId = ShoppingCartId,
                    Merchandise    = merchandise,
                    Amount         = amount
                };

                appDbContext.ShoppingCartItems.Add(shoppingCartItem);
            }
            else
            {
                shoppingCartItem.Amount = amount;
            }

            await appDbContext.SaveChangesAsync();
        }