public async Task <bool> Handle(CreateOrderCommand request, CancellationToken cancellationToken) { var orderStartedIntegrationEvent = new OrderStartedIntegrationEvent(request.UserId); await _orderingIntegrationEventService.AddAndSaveEventAsync(orderStartedIntegrationEvent); var address = new Address(request.Street, request.City, request.State, request.Country, request.ZipCode); var order = new Order( request.UserId, request.UserName, address, request.CardTypeId, request.CardNumber, request.CardSecurityNumber, request.CardHolderName, request.CardExpiration); foreach (var item in request.OrderItems) { order.AddOrderItem(item.ProductId, item.ProductName, item.UnitPrice, item.Discount, item.PictureUrl, item.Units); } _logger.LogInformation($"----- Creating Order - Order: {order}"); _orderRepository.Add(order); return(await _orderRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken)); }
public async Task <bool> Handle(CreateOrderCommand message, CancellationToken cancellationToken) { // Add integration event to clean the basket. var orderStartedIntegrationEvent = new OrderStartedIntegrationEvent(message.UserId); await _orderingIntegrationEventService.AddAndSaveEventAsync(orderStartedIntegrationEvent); // Add/Update the Buyer AggregateRoot // DDD patterns comment: Add child entitis and value objects throught the Order Aggregate-Root // methods and constructors so validations, invariants and business logic // make sure that consistency is preserved across the whole aggregate var address = new Address(message.Street, message.City, message.State, message.Country, message.ZipCode); var order = new Order(message.UserId, message.UserName, address, message.CardTypeId, message.CardNumber, message.CardSecurityNumber, message.CardHolderName, message.CardExpiration); foreach (var item in message.OrderItems) { order.AddOrderItem(item.ProductId, item.ProductName, item.UnitPrice, item.Discount, item.PictureUrl, item.Untis); } _logger.LogInformation("------ Creating Order - Order: {@Order}", order); _orderRepository.Add(order); return(await _orderRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken)); }