public void AddItemToShoppingCart(int shoppingCartId, int productId, int qty) { ShoppingCart shoppingCart = _context.ShoppingCarts.SingleOrDefault(s => s.ShoppingCartId == shoppingCartId); if (shoppingCart == null) { throw new NullReferenceException(); } ShoppingItemDetails shoppingItemDetails = shoppingCart.ShoppingItems?.SingleOrDefault(s => s.ProductId == productId); if (shoppingItemDetails == null) { shoppingItemDetails = new ShoppingItemDetails() { ProductId = productId, Qty = qty }; shoppingCart.ShoppingItems.Add(shoppingItemDetails); } else { shoppingItemDetails.Qty += qty; } _context.SaveChanges(); }
public void RemoveItemFromShoppingCart(int shoppingCartId, int productId) { ShoppingCart shoppingCart = _context.ShoppingCarts.SingleOrDefault(s => s.ShoppingCartId == shoppingCartId); if (shoppingCart == null) { throw new NullReferenceException(); } ShoppingItemDetails shoppingItemDetails = shoppingCart.ShoppingItems?.SingleOrDefault(s => s.ProductId == productId); if (shoppingItemDetails != null) { shoppingCart.ShoppingItems.Remove(shoppingItemDetails); _context.SaveChanges(); } }