OrderStockConfirmedDomainEvent implemented

This commit is contained in:
Christian Arenas 2017-05-11 18:39:06 +02:00
parent 2f41736f91
commit d3d15f1de5
5 changed files with 34 additions and 31 deletions

View File

@ -1,4 +1,6 @@
namespace Ordering.API.Application.DomainEventHandlers.OrderStartedEvent using System.Linq;
namespace Ordering.API.Application.DomainEventHandlers.OrderStartedEvent
{ {
using MediatR; using MediatR;
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
@ -9,14 +11,14 @@
using Ordering.API.Application.IntegrationCommands.Commands; using Ordering.API.Application.IntegrationCommands.Commands;
using Ordering.API.Application.IntegrationEvents; using Ordering.API.Application.IntegrationEvents;
public class UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler public class OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler
: IAsyncNotificationHandler<OrderStockMethodVerifiedDomainEvent> : IAsyncNotificationHandler<OrderStockConfirmedDomainEvent>
{ {
private readonly IOrderRepository _orderRepository; private readonly IOrderRepository _orderRepository;
private readonly ILoggerFactory _logger; private readonly ILoggerFactory _logger;
private readonly IOrderingIntegrationEventService _orderingIntegrationEventService; private readonly IOrderingIntegrationEventService _orderingIntegrationEventService;
public UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler( public OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler(
IOrderRepository orderRepository, ILoggerFactory logger, IOrderRepository orderRepository, ILoggerFactory logger,
IOrderingIntegrationEventService orderingIntegrationEventService) IOrderingIntegrationEventService orderingIntegrationEventService)
{ {
@ -25,23 +27,18 @@
_orderingIntegrationEventService = orderingIntegrationEventService; _orderingIntegrationEventService = orderingIntegrationEventService;
} }
public async Task Handle(OrderStockMethodVerifiedDomainEvent orderStockMethodVerifiedDomainEvent) public async Task Handle(OrderStockConfirmedDomainEvent orderStockMethodVerifiedDomainEvent)
{ {
var orderToUpdate = await _orderRepository.GetAsync(orderStockMethodVerifiedDomainEvent.OrderId); _logger.CreateLogger(nameof(OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler))
orderToUpdate.SetOrderStatusId(orderStockMethodVerifiedDomainEvent.OrderStatus.Id);
_orderRepository.Update(orderToUpdate);
await _orderRepository.UnitOfWork
.SaveEntitiesAsync();
_logger.CreateLogger(nameof(UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler))
.LogTrace($"Order with Id: {orderStockMethodVerifiedDomainEvent.OrderId} has been successfully updated with " + .LogTrace($"Order with Id: {orderStockMethodVerifiedDomainEvent.OrderId} has been successfully updated with " +
$"a status order id: { orderStockMethodVerifiedDomainEvent.OrderStatus.Id }"); $"a status order id: { orderStockMethodVerifiedDomainEvent.OrderStatus.Id }");
var payOrderCommandMsg = new PayOrderCommandMsg(orderToUpdate.Id); if (orderStockMethodVerifiedDomainEvent.OrderStatus == OrderStatus.StockValidated)
{
var payOrderCommandMsg = new PayOrderCommandMsg(orderStockMethodVerifiedDomainEvent.OrderId);
await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommandMsg); await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommandMsg);
await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommandMsg); await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommandMsg);
} }
} }
} }
}

View File

@ -1,4 +1,5 @@
using System.Linq; using System.Linq;
using Ordering.API.Application.IntegrationCommands.Commands;
namespace Ordering.API.Application.IntegrationEvents.EventHandling namespace Ordering.API.Application.IntegrationEvents.EventHandling
{ {
@ -25,15 +26,18 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling
public async Task Handle(OrderStockNotConfirmedIntegrationEvent @event) public async Task Handle(OrderStockNotConfirmedIntegrationEvent @event)
{ {
//TODO: must update the order state to cancelled and the CurrentOrderStateContextDescription with the reasons of no-stock confirm var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId);
var order = await _orderRepository.GetAsync(@event.OrderId); CheckValidSagaId(orderToUpdate);
CheckValidSagaId(order);
var orderStockNotConfirmedItems = @event.OrderStockItems var orderStockNotConfirmedItems = @event.OrderStockItems
.FindAll(c => !c.Confirmed) .FindAll(c => !c.Confirmed)
.Select(c => c.ProductId); .Select(c => c.ProductId);
order.SetOrderStockConfirmed(orderStockNotConfirmedItems); orderToUpdate.SetOrderStockConfirmed(orderStockNotConfirmedItems);
var payOrderCommandMsg = new PayOrderCommandMsg(orderToUpdate.Id);
await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommandMsg);
await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommandMsg);
} }
private void CheckValidSagaId(Order orderSaga) private void CheckValidSagaId(Order orderSaga)

View File

@ -14,7 +14,6 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling
private readonly ILoggerFactory _logger; private readonly ILoggerFactory _logger;
public UserCheckoutAcceptedIntegrationEventHandler(IMediator mediator, public UserCheckoutAcceptedIntegrationEventHandler(IMediator mediator,
IOrderingIntegrationEventService orderingIntegrationEventService,
ILoggerFactory logger) ILoggerFactory logger)
{ {
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));

View File

@ -103,21 +103,22 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O
if(orderStockNotConfirmedItems is null) if(orderStockNotConfirmedItems is null)
{ {
OrderStatus = OrderStatus.StockValidated; OrderStatus = OrderStatus.StockValidated;
_description = "All the items were confirmed with available stock."; _description = "All the items were confirmed with available stock.";
//AddDomainEvent(new OrderStockMethodVerifiedDomainEvent(Id, OrderStatus.StockValidated));
} }
else else
{ {
OrderStatus = OrderStatus.Cancelled;
var itemsStockNotConfirmedProductNames = OrderItems var itemsStockNotConfirmedProductNames = OrderItems
.Where(c => orderStockNotConfirmedItems.Contains(c.ProductId)) .Where(c => orderStockNotConfirmedItems.Contains(c.ProductId))
.Select(c => c.GetOrderItemProductName()); .Select(c => c.GetOrderItemProductName());
var itemsStockNotConfirmedDescription = string.Join(", ", itemsStockNotConfirmedProductNames); var itemsStockNotConfirmedDescription = string.Join(", ", itemsStockNotConfirmedProductNames);
OrderStatus = OrderStatus.Cancelled;
_description = $"The product items don't have stock: ({itemsStockNotConfirmedDescription})."; _description = $"The product items don't have stock: ({itemsStockNotConfirmedDescription}).";
//AddDomainEvent(new OrderStockMethodVerifiedDomainEvent(Id, OrderStatus.Cancelled));
} }
AddDomainEvent(new OrderStockConfirmedDomainEvent(Id, OrderStatus));
} }
private void AddOrderStartedDomainEvent(int cardTypeId, string cardNumber, private void AddOrderStartedDomainEvent(int cardTypeId, string cardNumber,

View File

@ -1,18 +1,20 @@
namespace Ordering.Domain.Events using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
namespace Ordering.Domain.Events
{ {
using MediatR; using MediatR;
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; using System.Collections.Generic;
/// <summary> /// <summary>
/// Event used when the order stock items are verified /// Event used when the order stock items are verified
/// </summary> /// </summary>
public class OrderStockMethodVerifiedDomainEvent public class OrderStockConfirmedDomainEvent
: IAsyncNotification : IAsyncNotification
{ {
public int OrderId { get; } public int OrderId { get; }
public OrderStatus OrderStatus { get; } public OrderStatus OrderStatus { get; }
public OrderStockMethodVerifiedDomainEvent(int orderId, public OrderStockConfirmedDomainEvent(int orderId,
OrderStatus orderStatus) OrderStatus orderStatus)
{ {
OrderId = orderId; OrderId = orderId;