public void AddToCart(Album album) { // Get the matching cart and album instances var cartItem = _db.CartItems.SingleOrDefault( c => c.CartId == ShoppingCartId && c.AlbumId == album.AlbumId); if (cartItem == null) { // TODO [EF] Swap to store generated key once we support identity pattern var nextCartItemId = _db.CartItems.Any() ? _db.CartItems.Max(c => c.CartItemId) + 1 : 1; // Create a new cart item if no cart item exists cartItem = new CartItem { CartItemId = nextCartItemId, AlbumId = album.AlbumId, CartId = ShoppingCartId, Count = 1, DateCreated = DateTime.Now }; _db.CartItems.Add(cartItem); } else { // If the item does exist in the cart, then add one to the quantity cartItem.Count++; // TODO [EF] Remove this line once change detection is available _db.Update(cartItem); } }
public void AddToCart(Album album) { // Get the matching cart and album instances var cartItem = _db.CartItems.SingleOrDefault( c => c.CartId == ShoppingCartId && c.AlbumId == album.AlbumId); if (cartItem == null) { // Create a new cart item if no cart item exists cartItem = new CartItem { AlbumId = album.AlbumId, CartId = ShoppingCartId, Count = 1, DateCreated = DateTime.Now }; _db.CartItems.Add(cartItem); } else { // If the item does exist in the cart, then add one to the quantity cartItem.Count++; } }