Skip to content

Instantly share code, notes, and snippets.

@thangchung
Created September 7, 2018 03:58
Show Gist options
  • Save thangchung/144b039d9b8abfa76fecb1444120cbb9 to your computer and use it in GitHub Desktop.
Save thangchung/144b039d9b8abfa76fecb1444120cbb9 to your computer and use it in GitHub Desktop.
Clean Domain-driven Design article - Delete Product in the Cart
public class RequestHandler : TxRequestHandlerBase<DeleteItemRequest, DeleteItemResponse>
{
private readonly ICatalogGateway _catalogGateway;
private readonly IShippingGateway _shippingGateway;
private readonly IPromoGateway _promoGateway;
public RequestHandler(IUnitOfWorkAsync uow, IQueryRepositoryFactory qf,
ICatalogGateway cgw, IShippingGateway shippingGateway, IPromoGateway promoGateway)
: base(uow, qf)
{
_catalogGateway = cgw;
_shippingGateway = shippingGateway;
_promoGateway = promoGateway;
}
public override async Task<DeleteItemResponse> Handle(DeleteItemRequest 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);
cart.RemoveCartItem(cartItem.Id);
await cart.CalculateCartAsync(TaxType.NoTax, _catalogGateway, _promoGateway, _shippingGateway);
await cartCommander.UpdateAsync(cart);
await UnitOfWork.SaveChangesAsync(cancellationToken);
return new DeleteItemResponse { ProductId = cartItem.Product.ProductId };
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment