2017-01-25 17:10:08 +01:00
|
|
|
|
namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands
|
2016-12-29 11:03:03 +01:00
|
|
|
|
{
|
2017-01-25 17:10:08 +01:00
|
|
|
|
using Domain.AggregatesModel.OrderAggregate;
|
2018-10-11 17:16:31 +02:00
|
|
|
|
using global::Ordering.API.Application.IntegrationEvents;
|
|
|
|
|
using global::Ordering.API.Application.IntegrationEvents.Events;
|
2016-12-29 11:03:03 +01:00
|
|
|
|
using MediatR;
|
2017-02-27 17:52:14 +01:00
|
|
|
|
using Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Services;
|
2017-03-28 12:02:30 +02:00
|
|
|
|
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency;
|
2019-02-06 21:36:40 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2016-12-29 11:03:03 +01:00
|
|
|
|
using System;
|
2017-12-11 17:42:33 +03:00
|
|
|
|
using System.Threading;
|
2016-12-29 11:03:03 +01:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2017-11-12 14:07:51 -08:00
|
|
|
|
// Regular CommandHandler
|
2017-01-18 16:51:44 -08:00
|
|
|
|
public class CreateOrderCommandHandler
|
2017-12-11 17:42:33 +03:00
|
|
|
|
: IRequestHandler<CreateOrderCommand, bool>
|
2016-12-29 11:03:03 +01:00
|
|
|
|
{
|
2017-03-20 01:42:31 -04:00
|
|
|
|
private readonly IOrderRepository _orderRepository;
|
2017-02-27 17:52:14 +01:00
|
|
|
|
private readonly IIdentityService _identityService;
|
2017-03-14 18:02:28 +01:00
|
|
|
|
private readonly IMediator _mediator;
|
2018-10-11 17:16:31 +02:00
|
|
|
|
private readonly IOrderingIntegrationEventService _orderingIntegrationEventService;
|
2019-02-06 21:36:40 +00:00
|
|
|
|
private readonly ILogger<CreateOrderCommandHandler> _logger;
|
2016-12-29 11:03:03 +01:00
|
|
|
|
|
2017-02-07 16:59:09 -08:00
|
|
|
|
// Using DI to inject infrastructure persistence Repositories
|
2018-10-11 17:16:31 +02:00
|
|
|
|
public CreateOrderCommandHandler(IMediator mediator,
|
|
|
|
|
IOrderingIntegrationEventService orderingIntegrationEventService,
|
2019-02-21 15:56:15 +00:00
|
|
|
|
IOrderRepository orderRepository,
|
2019-02-06 21:36:40 +00:00
|
|
|
|
IIdentityService identityService,
|
|
|
|
|
ILogger<CreateOrderCommandHandler> logger)
|
2016-12-29 11:03:03 +01:00
|
|
|
|
{
|
2017-02-27 10:35:31 +01:00
|
|
|
|
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository));
|
2017-02-27 17:52:14 +01:00
|
|
|
|
_identityService = identityService ?? throw new ArgumentNullException(nameof(identityService));
|
2017-03-14 18:02:28 +01:00
|
|
|
|
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
|
2018-10-11 17:16:31 +02:00
|
|
|
|
_orderingIntegrationEventService = orderingIntegrationEventService ?? throw new ArgumentNullException(nameof(orderingIntegrationEventService));
|
2019-02-06 21:36:40 +00:00
|
|
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
2016-12-29 11:03:03 +01:00
|
|
|
|
}
|
2017-01-25 17:10:08 +01:00
|
|
|
|
|
2017-12-11 17:42:33 +03:00
|
|
|
|
public async Task<bool> Handle(CreateOrderCommand message, CancellationToken cancellationToken)
|
2017-02-08 19:26:05 +01:00
|
|
|
|
{
|
2018-10-11 17:16:31 +02:00
|
|
|
|
// Add Integration event to clean the basket
|
|
|
|
|
var orderStartedIntegrationEvent = new OrderStartedIntegrationEvent(message.UserId);
|
|
|
|
|
await _orderingIntegrationEventService.AddAndSaveEventAsync(orderStartedIntegrationEvent);
|
2019-02-21 15:56:15 +00:00
|
|
|
|
|
2017-02-07 16:59:09 -08:00
|
|
|
|
// Add/Update the Buyer AggregateRoot
|
|
|
|
|
// DDD patterns comment: Add child entities and value-objects through the Order Aggregate-Root
|
|
|
|
|
// methods and constructor so validations, invariants and business logic
|
|
|
|
|
// make sure that consistency is preserved across the whole aggregate
|
2017-03-14 18:02:28 +01:00
|
|
|
|
var address = new Address(message.Street, message.City, message.State, message.Country, message.ZipCode);
|
2018-04-13 13:45:21 +02:00
|
|
|
|
var order = new Order(message.UserId, message.UserName, address, message.CardTypeId, message.CardNumber, message.CardSecurityNumber, message.CardHolderName, message.CardExpiration);
|
2019-02-21 15:56:15 +00:00
|
|
|
|
|
2017-01-30 15:46:43 +01:00
|
|
|
|
foreach (var item in message.OrderItems)
|
2016-12-29 11:03:03 +01:00
|
|
|
|
{
|
2017-01-30 15:46:43 +01:00
|
|
|
|
order.AddOrderItem(item.ProductId, item.ProductName, item.UnitPrice, item.Discount, item.PictureUrl, item.Units);
|
2016-12-29 11:03:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-06 21:36:40 +00:00
|
|
|
|
_logger.LogInformation("----- Creating Order - Order: {@Order}", order);
|
|
|
|
|
|
2019-02-21 15:56:15 +00:00
|
|
|
|
_orderRepository.Add(order);
|
2017-02-08 19:26:05 +01:00
|
|
|
|
|
2017-03-27 14:05:28 +02:00
|
|
|
|
return await _orderRepository.UnitOfWork
|
2017-03-14 18:02:28 +01:00
|
|
|
|
.SaveEntitiesAsync();
|
2016-12-29 11:03:03 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-12 16:28:28 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Use for Idempotency in Command process
|
|
|
|
|
public class CreateOrderIdentifiedCommandHandler : IdentifiedCommandHandler<CreateOrderCommand, bool>
|
|
|
|
|
{
|
2019-02-21 15:56:15 +00:00
|
|
|
|
public CreateOrderIdentifiedCommandHandler(
|
|
|
|
|
IMediator mediator,
|
|
|
|
|
IRequestManager requestManager,
|
|
|
|
|
ILogger<IdentifiedCommandHandler<CreateOrderCommand, bool>> logger)
|
|
|
|
|
: base(mediator, requestManager, logger)
|
2017-11-12 16:28:28 -08:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool CreateResultForDuplicateRequest()
|
|
|
|
|
{
|
|
|
|
|
return true; // Ignore duplicate requests for creating order.
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-12-29 11:03:03 +01:00
|
|
|
|
}
|