From bf6360a82ad8be2f4216f3a55aadf1a6dfb11c9e Mon Sep 17 00:00:00 2001 From: Christian Arenas Date: Thu, 11 May 2017 16:05:27 +0200 Subject: [PATCH] Add Order Payment Integration events --- ...PaymentMethodVerifiedDomainEventHandler.cs | 3 --- ...erStockMethodVerifiedDomainEventHandler.cs | 23 ++++++++----------- ...derPaymentFailedIntegrationEventHandler.cs | 14 +++++++++++ ...rPaymentSuccededIntegrationEventHandler.cs | 14 +++++++++++ ...erStockConfirmedIntegrationEventHandler.cs | 5 ++-- ...tockNotConfirmedIntegrationEventHandler.cs | 15 ++++++------ ...CheckoutAcceptedIntegrationEventHandler.cs | 3 --- .../OrderPaymentFailedIntegrationEvent .cs | 11 +++++++++ .../OrderPaymentSuccededIntegrationEvent.cs | 11 +++++++++ .../Application/Sagas/OrderProcessSaga.cs | 6 ++--- 10 files changed, 73 insertions(+), 32 deletions(-) create mode 100644 src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs create mode 100644 src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs create mode 100644 src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderPaymentFailedIntegrationEvent .cs create mode 100644 src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderPaymentSuccededIntegrationEvent.cs 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 657552810..6a0159115 100644 --- a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/BuyerAndPaymentMethodVerified/UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/BuyerAndPaymentMethodVerified/UpdateOrderWhenBuyerAndPaymentMethodVerifiedDomainEventHandler.cs @@ -36,12 +36,9 @@ namespace Ordering.API.Application.DomainEventHandlers.BuyerAndPaymentMethodVeri var orderStartedIntegrationEvent = new OrderStartedIntegrationEvent(buyerPaymentMethodVerifiedEvent.Buyer.IdentityGuid); - // Using a local transaction to achieve atomicity between original Ordering database operation and - // the IntegrationEventLog. Only saving event if order has been successfully persisted to db await _orderingIntegrationEventService .SaveEventAndOrderingContextChangesAsync(orderStartedIntegrationEvent); - // Publish ordering integration event and mark it as published await _orderingIntegrationEventService .PublishThroughEventBusAsync(orderStartedIntegrationEvent); diff --git a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmation/UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmation/UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler.cs index abea5e0da..40b2fa865 100644 --- a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmation/UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmation/UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler.cs @@ -6,23 +6,25 @@ using Domain.Events; using System; using System.Threading.Tasks; + using Ordering.API.Application.IntegrationCommands.Commands; + using Ordering.API.Application.IntegrationEvents; public class UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler : IAsyncNotificationHandler { private readonly IOrderRepository _orderRepository; - private readonly ILoggerFactory _logger; + private readonly ILoggerFactory _logger; + private readonly IOrderingIntegrationEventService _orderingIntegrationEventService; public UpdateOrderWhenOrderStockMethodVerifiedDomainEventHandler( - IOrderRepository orderRepository, ILoggerFactory logger) + IOrderRepository orderRepository, ILoggerFactory logger, + IOrderingIntegrationEventService orderingIntegrationEventService) { _orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); + _orderingIntegrationEventService = orderingIntegrationEventService; } - // Domain Logic comment: - // When the Order Stock items method have been validate and confirmed, - // then we can update the original Order with the new order status public async Task Handle(OrderStockMethodVerifiedDomainEvent orderStockMethodVerifiedDomainEvent) { var orderToUpdate = await _orderRepository.GetAsync(orderStockMethodVerifiedDomainEvent.OrderId); @@ -37,14 +39,9 @@ .LogTrace($"Order with Id: {orderStockMethodVerifiedDomainEvent.OrderId} has been successfully updated with " + $"a status order id: { orderStockMethodVerifiedDomainEvent.OrderStatus.Id }"); - - //var payOrderCommandMsg = new PayOrderCommandMsg(order.Id); - - //// Achieving atomicity between original Catalog database operation and the IntegrationEventLog thanks to a local transaction - //await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommandMsg); - - //// Publish through the Event Bus and mark the saved event as published - //await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommandMsg); + var payOrderCommandMsg = new PayOrderCommandMsg(orderToUpdate.Id); + 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/OrderPaymentFailedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs new file mode 100644 index 000000000..485cf750f --- /dev/null +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs @@ -0,0 +1,14 @@ +namespace Ordering.API.Application.IntegrationEvents.EventHandling +{ + using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; + using Ordering.API.Application.IntegrationEvents.Events; + using System.Threading.Tasks; + + public class OrderPaymentFailedIntegrationEventHandler : + IIntegrationEventHandler + { + public async Task Handle(OrderPaymentFailedIntegrationEvent @event) + { + } + } +} diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs new file mode 100644 index 000000000..86e3d5482 --- /dev/null +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs @@ -0,0 +1,14 @@ +namespace Ordering.API.Application.IntegrationEvents.EventHandling +{ + using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; + using Ordering.API.Application.IntegrationEvents.Events; + using System.Threading.Tasks; + + public class OrderPaymentSuccededIntegrationEventHandler : + IIntegrationEventHandler + { + public async Task Handle(OrderPaymentSuccededIntegrationEvent @event) + { + } + } +} 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 34dec2351..cfc2fdbe0 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs @@ -7,7 +7,8 @@ using Ordering.API.Application.IntegrationCommands.Commands; using Ordering.Domain.Exceptions; - public class OrderStockConfirmedIntegrationEventHandler : IIntegrationEventHandler + public class OrderStockConfirmedIntegrationEventHandler : + IIntegrationEventHandler { private readonly IOrderingIntegrationEventService _orderingIntegrationEventService; private readonly IOrderRepository _orderRepository; @@ -25,7 +26,7 @@ var order = await _orderRepository.GetAsync(@event.OrderId); CheckValidSagaId(order); - order.SetOrderStockConfirmed(true); + order.SetOrderStockConfirmed(); //Create Integration Event to be published through the Event Bus var payOrderCommandMsg = new PayOrderCommandMsg(order.Id); 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 c5b7e6b01..dfcb1d480 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,6 @@ -namespace Ordering.API.Application.IntegrationEvents.EventHandling +using System.Linq; + +namespace Ordering.API.Application.IntegrationEvents.EventHandling { using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using System; @@ -27,14 +29,11 @@ var order = await _orderRepository.GetAsync(@event.OrderId); CheckValidSagaId(order); - order.SetOrderStockConfirmed(false); - - var orderStockNotConfirmedItems = @event.OrderStockItems.FindAll(c => !c.Confirmed); + var orderStockNotConfirmedItems = @event.OrderStockItems + .FindAll(c => !c.Confirmed) + .Select(c => c.ProductId); - foreach (var orderStockNotConfirmedItem in orderStockNotConfirmedItems) - { - //TODO: Add messages - } + order.SetOrderStockConfirmed(orderStockNotConfirmedItems); } 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 f846f5031..71d07ae64 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/UserCheckoutAcceptedIntegrationEventHandler.cs @@ -1,7 +1,4 @@ using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; -using System; -using System.Collections.Generic; -using System.Linq; using System.Threading.Tasks; namespace Ordering.API.Application.IntegrationEvents.EventHandling diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderPaymentFailedIntegrationEvent .cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderPaymentFailedIntegrationEvent .cs new file mode 100644 index 000000000..337fcd351 --- /dev/null +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderPaymentFailedIntegrationEvent .cs @@ -0,0 +1,11 @@ +using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; + +namespace Ordering.API.Application.IntegrationEvents.Events +{ + public class OrderPaymentFailedIntegrationEvent : IntegrationEvent + { + public int OrderId { get; } + + public OrderPaymentFailedIntegrationEvent(int orderId) => OrderId = orderId; + } +} diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderPaymentSuccededIntegrationEvent.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderPaymentSuccededIntegrationEvent.cs new file mode 100644 index 000000000..525da09db --- /dev/null +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderPaymentSuccededIntegrationEvent.cs @@ -0,0 +1,11 @@ +using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; + +namespace Ordering.API.Application.IntegrationEvents.Events +{ + public class OrderPaymentSuccededIntegrationEvent : IntegrationEvent + { + public int OrderId { get; } + + public OrderPaymentSuccededIntegrationEvent(int orderId) => OrderId = orderId; + } +} diff --git a/src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs b/src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs index d071d0ea8..0f06a893c 100644 --- a/src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs +++ b/src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs @@ -72,7 +72,7 @@ namespace Ordering.API.Application.Sagas /// has been completed and order has not been cancelled. /// If so, the process continues for validation. /// - /// + /// /// Integration command message which is sent by a saga /// scheduler which provides the sagas that its grace /// period has completed. @@ -91,10 +91,10 @@ namespace Ordering.API.Application.Sagas var orderStockList = orderSaga.OrderItems .Select(orderItem => new OrderStockItem(orderItem.ProductId, orderItem.GetUnits())); - //Create Integration Event to be published through the Event Bus var confirmOrderStockEvent = new ConfirmOrderStockCommandMsg(orderSaga.Id, orderStockList); - // Publish through the Event Bus and mark the saved event as published + await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(confirmOrderStockEvent); + await _orderingIntegrationEventService.PublishThroughEventBusAsync(confirmOrderStockEvent); } }