Skip to content

Instantly share code, notes, and snippets.

@dcomartin dcomartin/Basket.cs

Last active Jul 22, 2020
Embed
What would you like to do?
public interface IBasketService
{
Task<int> AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity = 1)
}
public class BasketService : IBasketService
{
private readonly IAsyncRepository<Basket> _basketRepository;
public BasketService(IAsyncRepository<Basket> basketRepository)
{
_basketRepository = basketRepository;
}
public async Task<int> AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity = 1)
{
if (quantity < 1)
{
throw new InvalidOperationException("Quantity must be greater than 0.");
}
var basket = await _basketRepository.GetByIdAsync(basketId);
// This returns the quantity of the product we have in our shopping cart.
var result = basket.AddItem(catalogItemId, price, quantity);
await _basketRepository.UpdateAsync(basket);
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.