From d6ccf27100ff608877761ca1f738eae4f87c02d2 Mon Sep 17 00:00:00 2001 From: Christian Arenas Date: Thu, 11 May 2017 18:39:06 +0200 Subject: [PATCH] OrderStockConfirmedDomainEvent implemented --- ...nOrderStockConfirmedDomainEventHandler.cs} | 31 +++++++++---------- ...tockNotConfirmedIntegrationEventHandler.cs | 12 ++++--- ...CheckoutAcceptedIntegrationEventHandler.cs | 1 - .../AggregatesModel/OrderAggregate/Order.cs | 11 ++++--- .../OrderStockMethodVerifiedDomainEvent.cs | 10 +++--- 5 files changed, 34 insertions(+), 31 deletions(-) rename src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmation/{UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler.cs => OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler.cs} (53%) diff --git a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmation/UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmation/OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler.cs similarity index 53% rename from src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmation/UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler.cs rename to src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmation/OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler.cs index 40b2fa865..cd99f7d66 100644 --- a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmation/UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmation/OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler.cs @@ -1,4 +1,6 @@ -namespace Ordering.API.Application.DomainEventHandlers.OrderStartedEvent +using System.Linq; + +namespace Ordering.API.Application.DomainEventHandlers.OrderStartedEvent { using MediatR; using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; @@ -9,14 +11,14 @@ using Ordering.API.Application.IntegrationCommands.Commands; using Ordering.API.Application.IntegrationEvents; - public class UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler - : IAsyncNotificationHandler + public class OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler + : IAsyncNotificationHandler { private readonly IOrderRepository _orderRepository; private readonly ILoggerFactory _logger; private readonly IOrderingIntegrationEventService _orderingIntegrationEventService; - public UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler( + public OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler( IOrderRepository orderRepository, ILoggerFactory logger, IOrderingIntegrationEventService orderingIntegrationEventService) { @@ -25,23 +27,18 @@ _orderingIntegrationEventService = orderingIntegrationEventService; } - public async Task Handle(OrderStockMethodVerifiedDomainEvent orderStockMethodVerifiedDomainEvent) + public async Task Handle(OrderStockConfirmedDomainEvent orderStockMethodVerifiedDomainEvent) { - var orderToUpdate = await _orderRepository.GetAsync(orderStockMethodVerifiedDomainEvent.OrderId); - orderToUpdate.SetOrderStatusId(orderStockMethodVerifiedDomainEvent.OrderStatus.Id); - - _orderRepository.Update(orderToUpdate); - - await _orderRepository.UnitOfWork - .SaveEntitiesAsync(); - - _logger.CreateLogger(nameof(UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler)) + _logger.CreateLogger(nameof(OrderStatusChangedWhenOrderStockConfirmedDomainEventHandler)) .LogTrace($"Order with Id: {orderStockMethodVerifiedDomainEvent.OrderId} has been successfully updated with " + $"a status order id: { orderStockMethodVerifiedDomainEvent.OrderStatus.Id }"); - var payOrderCommandMsg = new PayOrderCommandMsg(orderToUpdate.Id); - await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommandMsg); - await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommandMsg); + if (orderStockMethodVerifiedDomainEvent.OrderStatus == OrderStatus.StockValidated) + { + var payOrderCommandMsg = new PayOrderCommandMsg(orderStockMethodVerifiedDomainEvent.OrderId); + await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommandMsg); + await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommandMsg); + } } } } \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs index dfcb1d480..04c9babb0 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs @@ -1,4 +1,5 @@ using System.Linq; +using Ordering.API.Application.IntegrationCommands.Commands; namespace Ordering.API.Application.IntegrationEvents.EventHandling { @@ -25,15 +26,18 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling 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 order = await _orderRepository.GetAsync(@event.OrderId); - CheckValidSagaId(order); + var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId); + CheckValidSagaId(orderToUpdate); var orderStockNotConfirmedItems = @event.OrderStockItems .FindAll(c => !c.Confirmed) .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) diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs index f28544a6f..ab325fa03 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs @@ -14,7 +14,6 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling private readonly ILoggerFactory _logger; public UserCheckoutAcceptedIntegrationEventHandler(IMediator mediator, - IOrderingIntegrationEventService orderingIntegrationEventService, ILoggerFactory logger) { _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator)); diff --git a/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs b/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs index 1920eb030..c1e2c4184 100644 --- a/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs +++ b/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs @@ -103,21 +103,22 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O if(orderStockNotConfirmedItems is null) { OrderStatus = OrderStatus.StockValidated; + _description = "All the items were confirmed with available stock."; - //AddDomainEvent(new OrderStockMethodVerifiedDomainEvent(Id, OrderStatus.StockValidated)); } else { + OrderStatus = OrderStatus.Cancelled; + var itemsStockNotConfirmedProductNames = OrderItems .Where(c => orderStockNotConfirmedItems.Contains(c.ProductId)) .Select(c => c.GetOrderItemProductName()); var itemsStockNotConfirmedDescription = string.Join(", ", itemsStockNotConfirmedProductNames); - - OrderStatus = OrderStatus.Cancelled; - _description = $"The product items don't have stock: ({itemsStockNotConfirmedDescription})."; - //AddDomainEvent(new OrderStockMethodVerifiedDomainEvent(Id, OrderStatus.Cancelled)); + _description = $"The product items don't have stock: ({itemsStockNotConfirmedDescription})."; } + + AddDomainEvent(new OrderStockConfirmedDomainEvent(Id, OrderStatus)); } private void AddOrderStartedDomainEvent(int cardTypeId, string cardNumber, diff --git a/src/Services/Ordering/Ordering.Domain/Events/OrderStockMethodVerifiedDomainEvent.cs b/src/Services/Ordering/Ordering.Domain/Events/OrderStockMethodVerifiedDomainEvent.cs index c5acd26dd..86f9c34d4 100644 --- a/src/Services/Ordering/Ordering.Domain/Events/OrderStockMethodVerifiedDomainEvent.cs +++ b/src/Services/Ordering/Ordering.Domain/Events/OrderStockMethodVerifiedDomainEvent.cs @@ -1,18 +1,20 @@ -namespace Ordering.Domain.Events +using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; + +namespace Ordering.Domain.Events { using MediatR; - using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; + using System.Collections.Generic; /// /// Event used when the order stock items are verified /// - public class OrderStockMethodVerifiedDomainEvent + public class OrderStockConfirmedDomainEvent : IAsyncNotification { public int OrderId { get; } public OrderStatus OrderStatus { get; } - public OrderStockMethodVerifiedDomainEvent(int orderId, + public OrderStockConfirmedDomainEvent(int orderId, OrderStatus orderStatus) { OrderId = orderId;