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;
|
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;
|
2016-12-29 11:03:03 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2017-03-03 12:03:31 +01:00
|
|
|
|
|
|
|
|
|
public class CreateOrderCommandIdentifiedHandler : IdentifierCommandHandler<CreateOrderCommand, bool>
|
|
|
|
|
{
|
|
|
|
|
public CreateOrderCommandIdentifiedHandler(IMediator mediator, IRequestManager requestManager) : base(mediator, requestManager)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool CreateResultForDuplicateRequest()
|
|
|
|
|
{
|
|
|
|
|
return true; // Ignore duplicate requests for creating order.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-18 16:51:44 -08:00
|
|
|
|
public class CreateOrderCommandHandler
|
|
|
|
|
: IAsyncRequestHandler<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;
|
2016-12-29 11:03:03 +01:00
|
|
|
|
|
2017-02-07 16:59:09 -08:00
|
|
|
|
// Using DI to inject infrastructure persistence Repositories
|
2017-03-29 11:43:30 +02:00
|
|
|
|
public CreateOrderCommandHandler(IMediator mediator, IOrderRepository orderRepository, IIdentityService identityService)
|
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));
|
2016-12-29 11:03:03 +01:00
|
|
|
|
}
|
2017-01-25 17:10:08 +01:00
|
|
|
|
|
2017-01-18 16:51:44 -08:00
|
|
|
|
public async Task<bool> Handle(CreateOrderCommand message)
|
2017-02-08 19:26:05 +01: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);
|
|
|
|
|
var order = new Order(address , message.CardTypeId, message.CardNumber, message.CardSecurityNumber, message.CardHolderName, message.CardExpiration);
|
2017-05-11 13:55:40 +02:00
|
|
|
|
order.SetOrderStatusId(OrderStatus.Submited.Id);
|
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
|
|
|
|
}
|
|
|
|
|
|
2017-03-14 18:02:28 +01: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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|