コード例 #1
0
        public int RemoveFromCart(Dessert dessert)
        {
            var shoppingCartItem = _appDbContext.ShoppingCartItems.SingleOrDefault(s => s.Dessert.DessertId == dessert.DessertId && s.ShoppingCartId == ShoppingCartId);

            var localAmount = 0;


            if (shoppingCartItem != null)
            {
                if (shoppingCartItem.Amount > 1)
                {
                    shoppingCartItem.Amount--;
                    localAmount = shoppingCartItem.Amount;
                }
                else
                {
                    _appDbContext.ShoppingCartItems.Remove(shoppingCartItem);
                }
            }
            _appDbContext.SaveChanges();
            return(localAmount);
        }
コード例 #2
0
        public void AddToCart(Dessert dessert, int amount)
        {
            var shoppingCartItem = _appDbContext.ShoppingCartItems.SingleOrDefault(s => s.Dessert.DessertId == dessert.DessertId && s.ShoppingCartId == ShoppingCartId);

            //var shoppingCartItem = _appDbContext.ShoppingCarts.SingleOrDefault(s => s.Dessert.
            //DessertId == dessert.DessertId && s.ShoppingId == ShoppingId);

            if (shoppingCartItem == null)
            {
                shoppingCartItem = new ShoppingCartItem
                {
                    ShoppingCartId = ShoppingCartId,
                    Dessert        = dessert,
                    Amount         = 1
                };
                _appDbContext.ShoppingCartItems.Add(shoppingCartItem);
            }
            else
            {
                shoppingCartItem.Amount++;
            }
            _appDbContext.SaveChanges();
        }