Skip to content

Instantly share code, notes, and snippets.

@dcomartin dcomartin/BasketEither.cs

Last active Jul 22, 2020
Embed
What would you like to do?
public interface IBasketService
{
Task<Either<int, QuantityValidationError>> AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity = 1);
}
public class QuantityValidationError
{
public string Message { get; set; }
public QuantityValidationError(string message)
{
Message = message;
}
}
public class BasketService : IBasketService
{
private readonly IAsyncRepository<Basket> _basketRepository;
public BasketService(IAsyncRepository<Basket> basketRepository)
{
_basketRepository = basketRepository;
}
public async Task<Either<int, QuantityValidationError>> AddItemToBasket(int basketId, int catalogItemId, decimal price, int quantity = 1)
{
if (quantity < 1)
{
return new Either<int, QuantityValidationError>(new QuantityValidationError("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 new Either<int, QuantityValidationError>(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.