Exemplo n.º 1
0
        public void AddCart(Tobacco tobacco, int quantity)
        {
            var ShoppingCart =
                _context.ShoppingCarts.SingleOrDefault(
                    s => s.Tobacco.TobaccoId == tobacco.TobaccoId && s.SCartId == SCartId);

            if (ShoppingCart == null)
            {
                ShoppingCart = new ShoppingCart
                {
                    SCartId  = SCartId,
                    Tobacco  = tobacco,
                    Quantity = 1
                };
                _context.ShoppingCarts.Add(ShoppingCart);
            }
            else
            {
                ShoppingCart.Quantity++;
            }
            _context.SaveChanges();
        }
Exemplo n.º 2
0
        public int RemoveCart(Tobacco tobacco)
        {
            var ShoppingCart =
                _context.ShoppingCarts.SingleOrDefault(
                    s => s.Tobacco.TobaccoId == tobacco.TobaccoId && s.SCartId == SCartId);

            var quantityLocal = 0;

            if (ShoppingCart != null)
            {
                if (ShoppingCart.Quantity > 1)
                {
                    ShoppingCart.Quantity--;
                    quantityLocal = ShoppingCart.Quantity;
                }
                else
                {
                    _context.ShoppingCarts.Remove(ShoppingCart);
                }
            }
            _context.SaveChanges();
            return(quantityLocal);
        }