コード例 #1
0
        public void AddToCart(Lipstick lipstick, int amount)
        {
            var shoppingCartItem = appDbContext.ShoppingCartItems.SingleOrDefault(s => s.Lipstick.LipstickId == lipstick.LipstickId &&
                                                                                  s.ShoppingCartId == ShoppingCartId);

            if (shoppingCartItem == null)
            {
                shoppingCartItem = new ShoppingCartItem
                {
                    ShoppingCartId = ShoppingCartId,
                    Lipstick       = lipstick,
                    Amount         = 1
                };

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

            appDbContext.SaveChanges();
        }
コード例 #2
0
        public int RemoveFromCart(Lipstick lipstick)
        {
            var shoppingCartItem = appDbContext.ShoppingCartItems.SingleOrDefault(s => s.Lipstick.LipstickId == lipstick.LipstickId &&
                                                                                  s.ShoppingCartId == ShoppingCartId);

            int localAmount = 0;

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

            appDbContext.SaveChanges();

            return(localAmount);
        }