Created
September 7, 2018 03:56
-
-
Save thangchung/4622a95add25d793efed93b507ca2297 to your computer and use it in GitHub Desktop.
Clean Domain-driven Design article - Update Product in the Cart
This file contains hidden or 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 class RequestHandler : TxRequestHandlerBase<UpdateItemInCartRequest, UpdateItemInCartResponse> | |
| { | |
| private readonly ICatalogGateway _catalogGateway; | |
| private readonly IShippingGateway _shippingGateway; | |
| private readonly IPromoGateway _promoGateway; | |
| public RequestHandler(IUnitOfWorkAsync uow, IQueryRepositoryFactory qrf, | |
| ICatalogGateway catalogGateway, IShippingGateway shippingGateway, | |
| IPromoGateway promoGateway) : base(uow, qrf) | |
| { | |
| _catalogGateway = catalogGateway; | |
| _shippingGateway = shippingGateway; | |
| _promoGateway = promoGateway; | |
| } | |
| public override async Task<UpdateItemInCartResponse> Handle(UpdateItemInCartRequest request, | |
| CancellationToken cancellationToken) | |
| { | |
| var cartCommander = UnitOfWork.Repository<Domain.Cart>(); | |
| var cartQuery = QueryFactory.QueryEfRepository<Domain.Cart>(); | |
| var cart = await cartQuery | |
| .GetFullCartAsync(request.CartId); | |
| var cartItem = cart.FindCartItem(request.ProductId); | |
| // if not exists then it should be a new item | |
| if (cartItem == null) | |
| { | |
| cart.InsertItemToCart(request.ProductId, request.Quantity); | |
| } | |
| else | |
| { | |
| // otherwise is updating the current item in the cart | |
| cart.AccumulateCartItemQuantity(cartItem.Id, request.Quantity); | |
| } | |
| await cart.CalculateCartAsync(TaxType.NoTax, _catalogGateway, _promoGateway, _shippingGateway); | |
| await cartCommander.UpdateAsync(cart); | |
| await UnitOfWork.SaveChangesAsync(cancellationToken); | |
| return new UpdateItemInCartResponse {Result = cart.ToDto()}; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment