/Source/Samples/Shop/Bifrost.Samples.Shop.Domain/Shopping/CartHandlers.cs

# · C# · 32 lines · 28 code · 4 blank · 0 comment · 2 complexity · 6a110c1fe923ea5dd9025c6bb74b5098 MD5 · raw file

  1. using Bifrost.Commands;
  2. using Bifrost.Domain;
  3. using Bifrost.Samples.Shop.Domain.Products;
  4. namespace Bifrost.Samples.Shop.Domain.Shopping
  5. {
  6. public class CartHandlers : ICommandHandler
  7. {
  8. private readonly IAggregatedRootRepository<Cart> _cartRootRepository;
  9. private readonly IAggregatedRootRepository<Product> _productRootRepository;
  10. public CartHandlers(
  11. IAggregatedRootRepository<Cart> cartRootRepository,
  12. IAggregatedRootRepository<Product> productRootRepository)
  13. {
  14. _cartRootRepository = cartRootRepository;
  15. _productRootRepository = productRootRepository;
  16. }
  17. public void Handle(AddProductToCart productToCart)
  18. {
  19. var cart = _cartRootRepository.Find(productToCart.Id);
  20. if( null == cart )
  21. {
  22. cart = Cart.Create(productToCart.Id);
  23. }
  24. var product = _productRootRepository.Get(productToCart.ProductId);
  25. cart.AddProduct(product);
  26. }
  27. }
  28. }