Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Last active July 22, 2020 23:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcomartin/32831f18dd3aa857255141b33a321622 to your computer and use it in GitHub Desktop.
Save dcomartin/32831f18dd3aa857255141b33a321622 to your computer and use it in GitHub Desktop.
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