/// <summary> /// 使用指定的商品数量,更新购物篮中的项目。 /// </summary> /// <param name="shoppingCartItemID">需要更新的购物篮项目的全局唯一标识。</param> /// <param name="quantity">商品数量。</param> public void UpdateShoppingCartItem(Guid shoppingCartItemID, int quantity) { var shoppingCartItem = shoppingCartItemRepository.Get(shoppingCartItemID); shoppingCartItem.UpdateQuantity(quantity); shoppingCartItemRepository.Update(shoppingCartItem); Context.Commit(); }
public ShoppingCart UpdateItem(int shoppingCartItemId, int quantity, int userId) { if (quantity < 0) { throw new Exception("Quantity cannot be less than zero"); } ShoppingCartItem item = shoppingCartItemRepository.Get(shoppingCartItemId); if (item == null) { throw new Exception($"Item with id {shoppingCartItemId} was not found"); } if (quantity == 0) { shoppingCartItemRepository.Delete(item.Id); } else { item.Quantity = quantity; shoppingCartItemRepository.Update(item); } return(GetCart(item.ShoppingCartId)); }