From 55a82c24f4816ac165233f2c583448b4eb670c95 Mon Sep 17 00:00:00 2001 From: Christian Arenas Date: Thu, 18 May 2017 11:42:22 +0200 Subject: [PATCH] - Change Integration Command to Integration Events - Refactor PublishThroughEventBusAsync methods from OrderingIntegrationEventService - Add private empty Address constructor. - Modify order aggregate methods. - Remove GetWithDependenciesAsync methods and modify GetAsync with entity framework Explicit loading. --- ...PaymentMethodVerifiedDomainEventHandler.cs | 6 +- ...dToAwaitingValidationDomainEventHandler.cs | 9 +-- ...erStatusChangedToPaidDomainEventHandler.cs | 7 +- ...angedToStockConfirmedDomainEventHandler.cs | 7 +- .../Commands/ConfirmGracePeriodCommand.cs | 12 --- .../Commands/PayOrderCommand.cs | 14 ---- ...derPaymentFailedIntegrationEventHandler.cs | 2 +- ...rPaymentSuccededIntegrationEventHandler.cs | 2 +- ...erStockConfirmedIntegrationEventHandler.cs | 2 +- ...tockNotConfirmedIntegrationEventHandler.cs | 33 -------- ...derStockRejectedIntegrationEventHandler.cs | 31 ++++++++ .../GracePeriodConfirmedIntegrationEvent.cs | 12 +++ .../OrderPaymentFailedIntegrationEvent .cs | 8 +- .../OrderPaymentSuccededIntegrationEvent.cs | 8 +- ...edToAwaitingValidationIntegrationEvent.cs} | 6 +- ...derStatusChangedToPaidIntegrationEvent.cs} | 6 +- ...ChangedToStockConfirmedIntegrationEvent.cs | 12 +++ ... => OrderStockRejectedIntegrationEvent.cs} | 9 +-- .../IOrderingIntegrationEventService.cs | 1 - .../OrderingIntegrationEventService.cs | 3 +- .../Application/Sagas/OrderProcessSaga.cs | 13 +-- .../Application/Sagas/OrderSaga.cs | 1 + .../Ordering/Ordering.API/Ordering.API.csproj | 1 + src/Services/Ordering/Ordering.API/Startup.cs | 18 +---- .../AggregatesModel/OrderAggregate/Address.cs | 2 + .../OrderAggregate/IOrderRepository.cs | 2 - .../AggregatesModel/OrderAggregate/Order.cs | 79 ++++++++----------- .../Repositories/OrderRepository.cs | 19 +++-- 28 files changed, 142 insertions(+), 183 deletions(-) delete mode 100644 src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ConfirmGracePeriodCommand.cs delete mode 100644 src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/PayOrderCommand.cs delete mode 100644 src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs create mode 100644 src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockRejectedIntegrationEventHandler.cs create mode 100644 src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/GracePeriodConfirmedIntegrationEvent.cs rename src/Services/Ordering/Ordering.API/Application/{IntegrationCommands/Commands/ConfirmOrderStockCommand.cs => IntegrationEvents/Events/OrderStatusChangedToAwaitingValidationIntegrationEvent.cs} (72%) rename src/Services/Ordering/Ordering.API/Application/{IntegrationCommands/Commands/DecrementOrderStockCommand.cs => IntegrationEvents/Events/OrderStatusChangedToPaidIntegrationEvent.cs} (64%) create mode 100644 src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToStockConfirmedIntegrationEvent.cs rename src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/{OrderStockNotConfirmedIntegrationEvent.cs => OrderStockRejectedIntegrationEvent.cs} (72%) diff --git a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/BuyerAndPaymentMethodVerified/UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/BuyerAndPaymentMethodVerified/UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler.cs index 6a0159115..08897fe46 100644 --- a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/BuyerAndPaymentMethodVerified/UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/BuyerAndPaymentMethodVerified/UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler.cs @@ -36,11 +36,7 @@ namespace Ordering.API.Application.DomainEventHandlers.BuyerAndPaymentMethodVeri var orderStartedIntegrationEvent = new OrderStartedIntegrationEvent(buyerPaymentMethodVerifiedEvent.Buyer.IdentityGuid); - await _orderingIntegrationEventService - .SaveEventAndOrderingContextChangesAsync(orderStartedIntegrationEvent); - - await _orderingIntegrationEventService - .PublishThroughEventBusAsync(orderStartedIntegrationEvent); + await _orderingIntegrationEventService.PublishThroughEventBusAsync(orderStartedIntegrationEvent); _logger.CreateLogger(nameof(UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler)) .LogTrace($"Order with Id: {buyerPaymentMethodVerifiedEvent.OrderId} has been successfully updated with a payment method id: { buyerPaymentMethodVerifiedEvent.Payment.Id }"); diff --git a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderGracePeriodConfirmed/OrderStatusChangedToAwaitingValidationDomainEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderGracePeriodConfirmed/OrderStatusChangedToAwaitingValidationDomainEventHandler.cs index 9a74d6a90..3c0168656 100644 --- a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderGracePeriodConfirmed/OrderStatusChangedToAwaitingValidationDomainEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderGracePeriodConfirmed/OrderStatusChangedToAwaitingValidationDomainEventHandler.cs @@ -6,9 +6,9 @@ using Domain.Events; using System; using System.Threading.Tasks; - using Ordering.API.Application.IntegrationCommands.Commands; using Ordering.API.Application.IntegrationEvents; using System.Linq; + using Ordering.API.Application.IntegrationEvents.Events; public class OrderStatusChangedToAwaitingValidationDomainEventHandler : IAsyncNotificationHandler @@ -35,10 +35,9 @@ var orderStockList = orderStatusChangedToAwaitingValidationDomainEvent.OrderItems .Select(orderItem => new OrderStockItem(orderItem.ProductId, orderItem.GetUnits())); - var confirmOrderStockCommand = new ConfirmOrderStockCommand(orderStatusChangedToAwaitingValidationDomainEvent.OrderId, - orderStockList); - await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(confirmOrderStockCommand); - await _orderingIntegrationEventService.PublishThroughEventBusAsync(confirmOrderStockCommand); + var orderStatusChangedToAwaitingValidationIntegrationEvent = new OrderStatusChangedToAwaitingValidationIntegrationEvent( + orderStatusChangedToAwaitingValidationDomainEvent.OrderId, orderStockList); + await _orderingIntegrationEventService.PublishThroughEventBusAsync(orderStatusChangedToAwaitingValidationIntegrationEvent); } } } \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderPaid/OrderStatusChangedToPaidDomainEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderPaid/OrderStatusChangedToPaidDomainEventHandler.cs index 300d5d75f..60f56c2e2 100644 --- a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderPaid/OrderStatusChangedToPaidDomainEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderPaid/OrderStatusChangedToPaidDomainEventHandler.cs @@ -6,9 +6,9 @@ using Domain.Events; using System; using System.Threading.Tasks; - using Ordering.API.Application.IntegrationCommands.Commands; using Ordering.API.Application.IntegrationEvents; using System.Linq; + using Ordering.API.Application.IntegrationEvents.Events; public class OrderStatusChangedToPaidDomainEventHandler : IAsyncNotificationHandler @@ -35,10 +35,9 @@ var orderStockList = orderStatusChangedToPaidDomainEvent.OrderItems .Select(orderItem => new OrderStockItem(orderItem.ProductId, orderItem.GetUnits())); - var decrementOrderStockCommand = new DecrementOrderStockCommand(orderStatusChangedToPaidDomainEvent.OrderId, + var orderStatusChangedToPaidIntegrationEvent = new OrderStatusChangedToPaidIntegrationEvent(orderStatusChangedToPaidDomainEvent.OrderId, orderStockList); - await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(decrementOrderStockCommand); - await _orderingIntegrationEventService.PublishThroughEventBusAsync(decrementOrderStockCommand); + await _orderingIntegrationEventService.PublishThroughEventBusAsync(orderStatusChangedToPaidIntegrationEvent); } } } \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmed/OrderStatusChangedToStockConfirmedDomainEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmed/OrderStatusChangedToStockConfirmedDomainEventHandler.cs index 272cbb061..7ead82c4d 100644 --- a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmed/OrderStatusChangedToStockConfirmedDomainEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmed/OrderStatusChangedToStockConfirmedDomainEventHandler.cs @@ -6,8 +6,8 @@ using Domain.Events; using System; using System.Threading.Tasks; - using Ordering.API.Application.IntegrationCommands.Commands; using Ordering.API.Application.IntegrationEvents; + using Ordering.API.Application.IntegrationEvents.Events; public class OrderStatusChangedToStockConfirmedDomainEventHandler : IAsyncNotificationHandler @@ -31,9 +31,8 @@ .LogTrace($"Order with Id: {orderStatusChangedToStockConfirmedDomainEvent.OrderId} has been successfully updated with " + $"a status order id: {OrderStatus.StockConfirmed.Id}"); - var payOrderCommand = new PayOrderCommand(orderStatusChangedToStockConfirmedDomainEvent.OrderId); - await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommand); - await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommand); + var orderStatusChangedToStockConfirmedIntegrationEvent = new OrderStatusChangedToStockConfirmedIntegrationEvent(orderStatusChangedToStockConfirmedDomainEvent.OrderId); + await _orderingIntegrationEventService.PublishThroughEventBusAsync(orderStatusChangedToStockConfirmedIntegrationEvent); } } } \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ConfirmGracePeriodCommand.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ConfirmGracePeriodCommand.cs deleted file mode 100644 index 33b509f16..000000000 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ConfirmGracePeriodCommand.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - -namespace Ordering.API.Application.IntegrationCommands.Commands -{ - public class ConfirmGracePeriodCommand : IntegrationEvent - { - public int OrderId { get; } - - public ConfirmGracePeriodCommand(int orderId) => - OrderId = orderId; - } -} diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/PayOrderCommand.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/PayOrderCommand.cs deleted file mode 100644 index 8a3ca665c..000000000 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/PayOrderCommand.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace Ordering.API.Application.IntegrationCommands.Commands -{ - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - - public class PayOrderCommand : IntegrationEvent - { - public int OrderId { get; } - - public PayOrderCommand(int orderId) - { - OrderId = orderId; - } - } -} \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs index 3f81f8f67..259b7ec34 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs @@ -17,7 +17,7 @@ public async Task Handle(OrderPaymentFailedIntegrationEvent @event) { - var orderToUpdate = await _orderRepository.GetWithDependenciesAsync(@event.OrderId); + var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId); orderToUpdate.SetCancelledStatus(); diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs index 80144d0ed..0e8598dcc 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs @@ -17,7 +17,7 @@ public async Task Handle(OrderPaymentSuccededIntegrationEvent @event) { - var orderToUpdate = await _orderRepository.GetWithDependenciesAsync(@event.OrderId); + var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId); orderToUpdate.SetPaidStatus(); diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs index ac4bc5936..fa7463041 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs @@ -17,7 +17,7 @@ public async Task Handle(OrderStockConfirmedIntegrationEvent @event) { - var orderToUpdate = await _orderRepository.GetWithDependenciesAsync(@event.OrderId); + var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId); orderToUpdate.SetStockConfirmedStatus(); diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs deleted file mode 100644 index 49708fe92..000000000 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System.Linq; -using Ordering.API.Application.IntegrationCommands.Commands; - -namespace Ordering.API.Application.IntegrationEvents.EventHandling -{ - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; - using System.Threading.Tasks; - using Events; - using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; - - public class OrderStockNotConfirmedIntegrationEventHandler : IIntegrationEventHandler - { - private readonly IOrderRepository _orderRepository; - - public OrderStockNotConfirmedIntegrationEventHandler(IOrderRepository orderRepository) - { - _orderRepository = orderRepository; - } - - public async Task Handle(OrderStockNotConfirmedIntegrationEvent @event) - { - var orderToUpdate = await _orderRepository.GetWithDependenciesAsync(@event.OrderId); - - var orderStockNotConfirmedItems = @event.OrderStockItems - .FindAll(c => !c.HasStock) - .Select(c => c.ProductId); - - orderToUpdate.SetStockConfirmedStatus(orderStockNotConfirmedItems); - - await _orderRepository.UnitOfWork.SaveEntitiesAsync(); - } - } -} \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockRejectedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockRejectedIntegrationEventHandler.cs new file mode 100644 index 000000000..124bb8a11 --- /dev/null +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockRejectedIntegrationEventHandler.cs @@ -0,0 +1,31 @@ +namespace Ordering.API.Application.IntegrationEvents.EventHandling +{ + using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; + using System.Threading.Tasks; + using Events; + using System.Linq; + using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; + + public class OrderStockRejectedIntegrationEventHandler : IIntegrationEventHandler + { + private readonly IOrderRepository _orderRepository; + + public OrderStockRejectedIntegrationEventHandler(IOrderRepository orderRepository) + { + _orderRepository = orderRepository; + } + + public async Task Handle(OrderStockRejectedIntegrationEvent @event) + { + var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId); + + var orderStockNotConfirmedItems = @event.OrderStockItems + .FindAll(c => !c.HasStock) + .Select(c => c.ProductId); + + orderToUpdate.SetCancelledStatusWhenStockIsRejected(orderStockNotConfirmedItems); + + await _orderRepository.UnitOfWork.SaveEntitiesAsync(); + } + } +} \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/GracePeriodConfirmedIntegrationEvent.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/GracePeriodConfirmedIntegrationEvent.cs new file mode 100644 index 000000000..15b0aebb5 --- /dev/null +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/GracePeriodConfirmedIntegrationEvent.cs @@ -0,0 +1,12 @@ +namespace Ordering.API.Application.IntegrationEvents.Events +{ + using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; + + public class GracePeriodConfirmedIntegrationEvent : IntegrationEvent + { + public int OrderId { get; } + + public GracePeriodConfirmedIntegrationEvent(int orderId) => + OrderId = orderId; + } +} diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderPaymentFailedIntegrationEvent .cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderPaymentFailedIntegrationEvent .cs index 337fcd351..fec066521 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderPaymentFailedIntegrationEvent .cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderPaymentFailedIntegrationEvent .cs @@ -1,11 +1,11 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - -namespace Ordering.API.Application.IntegrationEvents.Events +namespace Ordering.API.Application.IntegrationEvents.Events { + using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; + public class OrderPaymentFailedIntegrationEvent : IntegrationEvent { public int OrderId { get; } public OrderPaymentFailedIntegrationEvent(int orderId) => OrderId = orderId; } -} +} \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderPaymentSuccededIntegrationEvent.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderPaymentSuccededIntegrationEvent.cs index 525da09db..778aa8114 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderPaymentSuccededIntegrationEvent.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderPaymentSuccededIntegrationEvent.cs @@ -1,11 +1,11 @@ -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - -namespace Ordering.API.Application.IntegrationEvents.Events +namespace Ordering.API.Application.IntegrationEvents.Events { + using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; + public class OrderPaymentSuccededIntegrationEvent : IntegrationEvent { public int OrderId { get; } public OrderPaymentSuccededIntegrationEvent(int orderId) => OrderId = orderId; } -} +} \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ConfirmOrderStockCommand.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToAwaitingValidationIntegrationEvent.cs similarity index 72% rename from src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ConfirmOrderStockCommand.cs rename to src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToAwaitingValidationIntegrationEvent.cs index b3f73617c..63ae02246 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ConfirmOrderStockCommand.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToAwaitingValidationIntegrationEvent.cs @@ -1,14 +1,14 @@ -namespace Ordering.API.Application.IntegrationCommands.Commands +namespace Ordering.API.Application.IntegrationEvents.Events { using System.Collections.Generic; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - public class ConfirmOrderStockCommand : IntegrationEvent + public class OrderStatusChangedToAwaitingValidationIntegrationEvent : IntegrationEvent { public int OrderId { get; } public IEnumerable OrderStockItems { get; } - public ConfirmOrderStockCommand(int orderId, + public OrderStatusChangedToAwaitingValidationIntegrationEvent(int orderId, IEnumerable orderStockItems) { OrderId = orderId; diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/DecrementOrderStockCommand.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToPaidIntegrationEvent.cs similarity index 64% rename from src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/DecrementOrderStockCommand.cs rename to src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToPaidIntegrationEvent.cs index 3d0457ee8..115592308 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/DecrementOrderStockCommand.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToPaidIntegrationEvent.cs @@ -1,14 +1,14 @@ -namespace Ordering.API.Application.IntegrationCommands.Commands +namespace Ordering.API.Application.IntegrationEvents.Events { using System.Collections.Generic; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - public class DecrementOrderStockCommand : IntegrationEvent + public class OrderStatusChangedToPaidIntegrationEvent : IntegrationEvent { public int OrderId { get; } public IEnumerable OrderStockItems { get; } - public DecrementOrderStockCommand(int orderId, + public OrderStatusChangedToPaidIntegrationEvent(int orderId, IEnumerable orderStockItems) { OrderId = orderId; diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToStockConfirmedIntegrationEvent.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToStockConfirmedIntegrationEvent.cs new file mode 100644 index 000000000..d0b1ef2c4 --- /dev/null +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStatusChangedToStockConfirmedIntegrationEvent.cs @@ -0,0 +1,12 @@ +namespace Ordering.API.Application.IntegrationEvents.Events +{ + using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; + + public class OrderStatusChangedToStockConfirmedIntegrationEvent : IntegrationEvent + { + public int OrderId { get; } + + public OrderStatusChangedToStockConfirmedIntegrationEvent(int orderId) + => OrderId = orderId; + } +} \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStockNotConfirmedIntegrationEvent.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStockRejectedIntegrationEvent.cs similarity index 72% rename from src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStockNotConfirmedIntegrationEvent.cs rename to src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStockRejectedIntegrationEvent.cs index 6bdb38ab2..647970581 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStockNotConfirmedIntegrationEvent.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStockRejectedIntegrationEvent.cs @@ -1,16 +1,15 @@ -using System.Collections.Generic; - -namespace Ordering.API.Application.IntegrationEvents.Events +namespace Ordering.API.Application.IntegrationEvents.Events { using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; + using System.Collections.Generic; - public class OrderStockNotConfirmedIntegrationEvent : IntegrationEvent + public class OrderStockRejectedIntegrationEvent : IntegrationEvent { public int OrderId { get; } public List OrderStockItems { get; } - public OrderStockNotConfirmedIntegrationEvent(int orderId, + public OrderStockRejectedIntegrationEvent(int orderId, List orderStockItems) { OrderId = orderId; diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/IOrderingIntegrationEventService.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/IOrderingIntegrationEventService.cs index 28227a66f..373bafaa5 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/IOrderingIntegrationEventService.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/IOrderingIntegrationEventService.cs @@ -5,7 +5,6 @@ namespace Ordering.API.Application.IntegrationEvents { public interface IOrderingIntegrationEventService { - Task SaveEventAndOrderingContextChangesAsync(IntegrationEvent evt); Task PublishThroughEventBusAsync(IntegrationEvent evt); } } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/OrderingIntegrationEventService.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/OrderingIntegrationEventService.cs index 831a1ec1e..b3c0201b5 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/OrderingIntegrationEventService.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/OrderingIntegrationEventService.cs @@ -30,11 +30,12 @@ namespace Ordering.API.Application.IntegrationEvents public async Task PublishThroughEventBusAsync(IntegrationEvent evt) { + await SaveEventAndOrderingContextChangesAsync(evt); _eventBus.Publish(evt); await _eventLogService.MarkEventAsPublishedAsync(evt); } - public async Task SaveEventAndOrderingContextChangesAsync(IntegrationEvent evt) + private async Task SaveEventAndOrderingContextChangesAsync(IntegrationEvent evt) { //Use of an EF Core resiliency strategy when using multiple DbContexts within an explicit BeginTransaction(): //See: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency diff --git a/src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs b/src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs index a769c7e4c..f26c46925 100644 --- a/src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs +++ b/src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs @@ -5,8 +5,7 @@ using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.Order using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure; using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency; using Ordering.API.Application.Commands; -using Ordering.API.Application.IntegrationCommands.Commands; -using Ordering.API.Application.IntegrationEvents; +using Ordering.API.Application.IntegrationEvents.Events; using Ordering.Domain.Exceptions; using System.Threading.Tasks; @@ -21,7 +20,7 @@ namespace Ordering.API.Application.Sagas /// with the validations. /// public class OrderProcessSaga : OrderSaga, - IIntegrationEventHandler, + IIntegrationEventHandler, IAsyncRequestHandler, IAsyncRequestHandler { @@ -43,9 +42,9 @@ namespace Ordering.API.Application.Sagas /// period has completed. /// /// - public async Task Handle(ConfirmGracePeriodCommand command) + public async Task Handle(GracePeriodConfirmedIntegrationEvent @event) { - var orderSaga = FindSagaById(command.OrderId); + var orderSaga = FindSagaById(@event.OrderId); CheckValidSagaId(orderSaga); orderSaga.SetAwaitingValidationStatus(); @@ -96,8 +95,6 @@ namespace Ordering.API.Application.Sagas } } - #region CommandHandlerIdentifiers - public class CancelOrderCommandIdentifiedHandler : IdentifierCommandHandler { public CancelOrderCommandIdentifiedHandler(IMediator mediator, IRequestManager requestManager) : base(mediator, requestManager) @@ -121,7 +118,5 @@ namespace Ordering.API.Application.Sagas return true; // Ignore duplicate requests for processing order. } } - - #endregion } } diff --git a/src/Services/Ordering/Ordering.API/Application/Sagas/OrderSaga.cs b/src/Services/Ordering/Ordering.API/Application/Sagas/OrderSaga.cs index babc66188..b1041971a 100644 --- a/src/Services/Ordering/Ordering.API/Application/Sagas/OrderSaga.cs +++ b/src/Services/Ordering/Ordering.API/Application/Sagas/OrderSaga.cs @@ -20,6 +20,7 @@ namespace Ordering.API.Application.Sagas var order = _orderingContext.Orders .Include(c => c.OrderStatus) .Include(c => c.OrderItems) + .Include(c => c.Address) .Single(c => c.Id == id); return order; diff --git a/src/Services/Ordering/Ordering.API/Ordering.API.csproj b/src/Services/Ordering/Ordering.API/Ordering.API.csproj index d5ef524bb..e0970db38 100644 --- a/src/Services/Ordering/Ordering.API/Ordering.API.csproj +++ b/src/Services/Ordering/Ordering.API/Ordering.API.csproj @@ -80,6 +80,7 @@ + diff --git a/src/Services/Ordering/Ordering.API/Startup.cs b/src/Services/Ordering/Ordering.API/Startup.cs index fc2bb90f2..94c20d2d6 100644 --- a/src/Services/Ordering/Ordering.API/Startup.cs +++ b/src/Services/Ordering/Ordering.API/Startup.cs @@ -3,10 +3,8 @@ using AspNetCore.Http; using Autofac; using Autofac.Extensions.DependencyInjection; - using global::Ordering.API.Application.IntegrationCommands.Commands; using global::Ordering.API.Application.IntegrationEvents; using global::Ordering.API.Application.IntegrationEvents.Events; - using global::Ordering.API.Application.Sagas; using global::Ordering.API.Infrastructure.Middlewares; using Infrastructure; using Infrastructure.Auth; @@ -30,7 +28,6 @@ using System; using System.Data.Common; using System.Reflection; - using global::Ordering.API.Application.IntegrationEvents.EventHandling; public class Startup { @@ -169,17 +166,6 @@ { services.AddSingleton(); services.AddSingleton(); - - services.AddTransient>(); - services.AddTransient, OrderProcessSaga>(); - services.AddTransient, - OrderStockConfirmedIntegrationEventHandler>(); - services.AddTransient, - OrderStockNotConfirmedIntegrationEventHandler>(); - services.AddTransient, - OrderPaymentFailedIntegrationEventHandler>(); - services.AddTransient, - OrderPaymentSuccededIntegrationEventHandler>(); } private void ConfigureEventBus(IApplicationBuilder app) @@ -187,9 +173,9 @@ var eventBus = app.ApplicationServices.GetRequiredService(); eventBus.Subscribe>(); - eventBus.Subscribe>(); + eventBus.Subscribe>(); eventBus.Subscribe>(); - eventBus.Subscribe>(); + eventBus.Subscribe>(); eventBus.Subscribe>(); eventBus.Subscribe>(); } diff --git a/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Address.cs b/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Address.cs index 961d83d3a..5579cf45e 100644 --- a/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Address.cs +++ b/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Address.cs @@ -17,6 +17,8 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O public String ZipCode { get; private set; } + private Address() { } + public Address(string street, string city, string state, string country, string zipcode) { Street = street; diff --git a/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/IOrderRepository.cs b/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/IOrderRepository.cs index 368410d62..d7346ee4f 100644 --- a/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/IOrderRepository.cs +++ b/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/IOrderRepository.cs @@ -13,7 +13,5 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O void Update(Order order); Task GetAsync(int orderId); - - Task GetWithDependenciesAsync(int orderId); } } diff --git a/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs b/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs index eaecba5f9..1f3a0dbbf 100644 --- a/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs +++ b/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs @@ -94,14 +94,12 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O _buyerId = id; } - #region Status Changes - public void SetAwaitingValidationStatus() { - if (_orderStatusId != OrderStatus.Submited.Id && - _orderStatusId != OrderStatus.Cancelled.Id) + if (_orderStatusId == OrderStatus.Cancelled.Id || + _orderStatusId != OrderStatus.Submited.Id) { - StatusChangeException(); + StatusChangeException(OrderStatus.AwaitingValidation); } AddDomainEvent(new OrderStatusChangedToAwaitingValidationDomainEvent(Id, _orderItems)); @@ -109,38 +107,24 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O _orderStatusId = OrderStatus.AwaitingValidation.Id; } - public void SetStockConfirmedStatus(IEnumerable orderStockNotConfirmedItems = null) + public void SetStockConfirmedStatus() { if (_orderStatusId != OrderStatus.AwaitingValidation.Id) { - StatusChangeException(); - } - - if (orderStockNotConfirmedItems is null) - { - AddDomainEvent(new OrderStatusChangedToStockConfirmedDomainEvent(Id)); - - _orderStatusId = OrderStatus.StockConfirmed.Id; - _description = "All the items were confirmed with available stock."; + StatusChangeException(OrderStatus.StockConfirmed); } - else - { - _orderStatusId = OrderStatus.Cancelled.Id; - var itemsStockNotConfirmedProductNames = OrderItems - .Where(c => orderStockNotConfirmedItems.Contains(c.ProductId)) - .Select(c => c.GetOrderItemProductName()); + AddDomainEvent(new OrderStatusChangedToStockConfirmedDomainEvent(Id)); - var itemsStockNotConfirmedDescription = string.Join(", ", itemsStockNotConfirmedProductNames); - _description = $"The product items don't have stock: ({itemsStockNotConfirmedDescription})."; - } + _orderStatusId = OrderStatus.StockConfirmed.Id; + _description = "All the items were confirmed with available stock."; } public void SetPaidStatus() { if (_orderStatusId != OrderStatus.StockConfirmed.Id) { - StatusChangeException(); + StatusChangeException(OrderStatus.Paid); } AddDomainEvent(new OrderStatusChangedToPaidDomainEvent(Id, OrderItems)); @@ -153,40 +137,41 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O { if (_orderStatusId != OrderStatus.Paid.Id) { - StatusChangeException(); + StatusChangeException(OrderStatus.Shipped); } _orderStatusId = OrderStatus.Shipped.Id; - _description = ""; + _description = "The order was shipped."; } public void SetCancelledStatus() { - if (_orderStatusId == OrderStatus.Submited.Id) - { - _description = "The order was cancelled before the grace period was confirmed."; - } - else if (_orderStatusId == OrderStatus.AwaitingValidation.Id) - { - _description = "The order was cancelled before to check the order stock items."; - } - else if (_orderStatusId == OrderStatus.StockConfirmed.Id) + if (_orderStatusId == OrderStatus.Paid.Id || + _orderStatusId == OrderStatus.Shipped.Id) { - _description = "The order was cancelled before to pay the order."; + StatusChangeException(OrderStatus.Cancelled); } - else if (_orderStatusId == OrderStatus.Paid.Id) - { - _description = "The order was cancelled before to ship the order."; - } - else if(_orderStatusId == OrderStatus.Shipped.Id) + + _orderStatusId = OrderStatus.Cancelled.Id; + _description = $"The order was cancelled."; + } + + public void SetCancelledStatusWhenStockIsRejected(IEnumerable orderStockNotConfirmedItems) + { + if (_orderStatusId != OrderStatus.AwaitingValidation.Id) { - throw new OrderingDomainException("Not possible to change order status. Reason: cannot cancel order it is already shipped."); + StatusChangeException(OrderStatus.Cancelled); } _orderStatusId = OrderStatus.Cancelled.Id; - } - #endregion + var itemsStockNotConfirmedProductNames = OrderItems + .Where(c => orderStockNotConfirmedItems.Contains(c.ProductId)) + .Select(c => c.GetOrderItemProductName()); + + var itemsStockNotConfirmedDescription = string.Join(", ", itemsStockNotConfirmedProductNames); + _description = $"The product items don't have stock: ({itemsStockNotConfirmedDescription})."; + } private void AddOrderStartedDomainEvent(string userId, int cardTypeId, string cardNumber, string cardSecurityNumber, string cardHolderName, DateTime cardExpiration) @@ -198,9 +183,9 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O this.AddDomainEvent(orderStartedDomainEvent); } - private void StatusChangeException() + private void StatusChangeException(OrderStatus orderStatusToChange) { - throw new OrderingDomainException("Not able to process order event. Reason: no valid order status change"); + throw new OrderingDomainException($"Not possible to change order status from {OrderStatus.Name} to {orderStatusToChange.Name}."); } } } diff --git a/src/Services/Ordering/Ordering.Infrastructure/Repositories/OrderRepository.cs b/src/Services/Ordering/Ordering.Infrastructure/Repositories/OrderRepository.cs index df7b07aaa..4aaf738af 100644 --- a/src/Services/Ordering/Ordering.Infrastructure/Repositories/OrderRepository.cs +++ b/src/Services/Ordering/Ordering.Infrastructure/Repositories/OrderRepository.cs @@ -33,15 +33,18 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Repositor public async Task GetAsync(int orderId) { - return await _context.Orders.FindAsync(orderId); - } + var order = await _context.Orders.FindAsync(orderId); + if (order != null) + { + await _context.Entry(order) + .Collection(i => i.OrderItems).LoadAsync(); + await _context.Entry(order) + .Reference(i => i.OrderStatus).LoadAsync(); + await _context.Entry(order) + .Reference(i => i.Address).LoadAsync(); + } - public async Task GetWithDependenciesAsync(int orderId) - { - return await _context.Orders - .Include(c => c.OrderStatus) - .Include(c => c.OrderItems) - .SingleAsync(c => c.Id == orderId); + return order; } public void Update(Order order)