Last active
July 22, 2020 23:13
-
-
Save dcomartin/23f977915fe1180940f8d7ccc6c7222e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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