59 lines
2.7 KiB
C#
Raw Normal View History

namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands
{
using Domain.AggregatesModel.OrderAggregate;
using MediatR;
2017-02-27 17:52:14 +01:00
using Microsoft.eShopOnContainers.Services.Ordering.API.Infrastructure.Services;
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency;
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.
}
}
public class CreateOrderCommandHandler
: IAsyncRequestHandler<CreateOrderCommand, bool>
{
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;
// Using DI to inject infrastructure persistence Repositories
public CreateOrderCommandHandler(IMediator mediator, IOrderRepository orderRepository, IIdentityService identityService)
{
_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));
}
public async Task<bool> Handle(CreateOrderCommand message)
{
// 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);
order.SetOrderStatusId(OrderStatus.Submited.Id);
foreach (var item in message.OrderItems)
{
order.AddOrderItem(item.ProductId, item.ProductName, item.UnitPrice, item.Discount, item.PictureUrl, item.Units);
}
2017-03-14 18:02:28 +01:00
_orderRepository.Add(order);
return await _orderRepository.UnitOfWork
2017-03-14 18:02:28 +01:00
.SaveEntitiesAsync();
}
}
}