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(); }
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); }