コード例 #1
0
ファイル: ShoppingCart.cs プロジェクト: Amane345/KidStore
        public void AddToCart(Clothes clothes, int amount)
        {
            var shoppingCartItem = _atDbContext.ShopppingCartItems.SingleOrDefault(
                s => s.Clothes.ClothesId == clothes.ClothesId && s.ShoppingCartId == ShoppingCartId);

            if (shoppingCartItem == null)
            {
                shoppingCartItem = new ShoppingCartItem
                {
                    ShoppingCartId = ShoppingCartId,
                    Clothes        = clothes,
                    Amount         = amount
                };

                _atDbContext.ShopppingCartItems.Add(shoppingCartItem);
            }
            else
            {
                shoppingCartItem.Amount++;
            }

            _atDbContext.SaveChanges();
        }
コード例 #2
0
ファイル: ShoppingCart.cs プロジェクト: Amane345/KidStore
        public int RemoveFromCart(Clothes clothes)
        {
            var shoppingCartItem = _atDbContext.ShopppingCartItems.SingleOrDefault(
                s => s.Clothes.ClothesId == clothes.ClothesId && s.ShoppingCartId == ShoppingCartId);

            var localAmount = 0;

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

            _atDbContext.SaveChanges();

            return(localAmount);
        }