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-17 18:32:40 -08:00
|
|
|
|
using Domain.AggregatesModel.BuyerAggregate;
|
2017-01-25 17:10:08 +01:00
|
|
|
|
using Domain.AggregatesModel.OrderAggregate;
|
2016-12-29 11:03:03 +01:00
|
|
|
|
using MediatR;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2017-01-18 16:51:44 -08:00
|
|
|
|
public class CreateOrderCommandHandler
|
|
|
|
|
: IAsyncRequestHandler<CreateOrderCommand, bool>
|
2016-12-29 11:03:03 +01:00
|
|
|
|
{
|
2017-02-26 20:32:34 -08:00
|
|
|
|
private readonly IBuyerRepository<Buyer> _buyerRepository;
|
|
|
|
|
private readonly IOrderRepository<Order> _orderRepository;
|
2016-12-29 11:03:03 +01:00
|
|
|
|
|
2017-02-07 16:59:09 -08:00
|
|
|
|
// Using DI to inject infrastructure persistence Repositories
|
2017-02-26 20:32:34 -08:00
|
|
|
|
public CreateOrderCommandHandler(IBuyerRepository<Buyer> buyerRepository, IOrderRepository<Order> orderRepository)
|
2016-12-29 11:03:03 +01:00
|
|
|
|
{
|
2017-02-27 10:35:31 +01:00
|
|
|
|
_buyerRepository = buyerRepository ?? throw new ArgumentNullException(nameof(buyerRepository));
|
|
|
|
|
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository));
|
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
|
2016-12-29 11:03:03 +01:00
|
|
|
|
|
2017-02-07 23:03:35 -08:00
|
|
|
|
var buyer = await _buyerRepository.FindAsync(message.BuyerIdentityGuid);
|
2016-12-29 11:03:03 +01:00
|
|
|
|
|
|
|
|
|
if (buyer == null)
|
|
|
|
|
{
|
2017-02-07 23:03:35 -08:00
|
|
|
|
buyer = new Buyer(message.BuyerIdentityGuid);
|
2016-12-29 11:03:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-01-27 11:38:23 +01:00
|
|
|
|
var payment = buyer.AddPaymentMethod(message.CardTypeId,
|
2017-01-25 17:10:08 +01:00
|
|
|
|
$"Payment Method on {DateTime.UtcNow}",
|
|
|
|
|
message.CardNumber,
|
|
|
|
|
message.CardSecurityNumber,
|
|
|
|
|
message.CardHolderName,
|
|
|
|
|
message.CardExpiration);
|
|
|
|
|
|
|
|
|
|
_buyerRepository.Add(buyer);
|
2016-12-29 11:03:03 +01:00
|
|
|
|
|
|
|
|
|
await _buyerRepository.UnitOfWork
|
|
|
|
|
.SaveChangesAsync();
|
|
|
|
|
|
2017-02-07 16:59:09 -08:00
|
|
|
|
// Create the Order 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
|
2016-12-29 11:03:03 +01:00
|
|
|
|
|
2017-01-25 17:10:08 +01:00
|
|
|
|
var order = new Order(buyer.Id, payment.Id, new Address(message.Street, message.City, message.State, message.Country, message.ZipCode));
|
2016-12-29 11:03:03 +01: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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_orderRepository.Add(order);
|
|
|
|
|
|
|
|
|
|
var result = await _orderRepository.UnitOfWork
|
|
|
|
|
.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
return result > 0;
|
2017-02-08 19:26:05 +01:00
|
|
|
|
|
2016-12-29 11:03:03 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|