From 511bc05cb4c6707109fd6c3534d67722521015bb Mon Sep 17 00:00:00 2001 From: Christian Arenas Date: Wed, 17 May 2017 19:41:49 +0200 Subject: [PATCH 1/7] Remove SagaManagerIntegrationEventService frrom SagaManager and remove GracePeriod from dockercompose --- docker-compose.override.yml | 1 - ... => GracePeriodConfirmedIntegrationEvent.cs} | 4 ++-- .../ISagaManagerIntegrationEventService.cs | 9 --------- .../SagaManagerIntegrationEventService.cs | 17 ----------------- src/Services/SagaManager/SagaManager/Program.cs | 7 +++---- .../SagaManager/SagaManagerSettings.cs | 4 +++- .../SagaManager/Services/SagaManagerService.cs | 16 ++++++++-------- .../SagaManager/SagaManager/appsettings.json | 4 +++- 8 files changed, 19 insertions(+), 43 deletions(-) rename src/Services/SagaManager/SagaManager/IntegrationEvents/Events/{ConfirmGracePeriodCommand.cs => GracePeriodConfirmedIntegrationEvent.cs} (51%) delete mode 100644 src/Services/SagaManager/SagaManager/IntegrationEvents/ISagaManagerIntegrationEventService.cs delete mode 100644 src/Services/SagaManager/SagaManager/IntegrationEvents/SagaManagerIntegrationEventService.cs diff --git a/docker-compose.override.yml b/docker-compose.override.yml index 0e752fb0e..ae42cb438 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -11,7 +11,6 @@ services: environment: - ConnectionString=Server=sql.data;Database=Microsoft.eShopOnContainers.Services.OrderingDb;User Id=sa;Password=Pass@word - EventBusConnection=rabbitmq - - GracePeriod=15 #In minutes basket.api: environment: diff --git a/src/Services/SagaManager/SagaManager/IntegrationEvents/Events/ConfirmGracePeriodCommand.cs b/src/Services/SagaManager/SagaManager/IntegrationEvents/Events/GracePeriodConfirmedIntegrationEvent.cs similarity index 51% rename from src/Services/SagaManager/SagaManager/IntegrationEvents/Events/ConfirmGracePeriodCommand.cs rename to src/Services/SagaManager/SagaManager/IntegrationEvents/Events/GracePeriodConfirmedIntegrationEvent.cs index fd507a265..0eb422d6f 100644 --- a/src/Services/SagaManager/SagaManager/IntegrationEvents/Events/ConfirmGracePeriodCommand.cs +++ b/src/Services/SagaManager/SagaManager/IntegrationEvents/Events/GracePeriodConfirmedIntegrationEvent.cs @@ -2,10 +2,10 @@ { using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - public class ConfirmGracePeriodCommand : IntegrationEvent + public class GracePeriodConfirmedIntegrationEvent : IntegrationEvent { public int OrderId { get;} - public ConfirmGracePeriodCommand(int orderId) => OrderId = orderId; + public GracePeriodConfirmedIntegrationEvent(int orderId) => OrderId = orderId; } } \ No newline at end of file diff --git a/src/Services/SagaManager/SagaManager/IntegrationEvents/ISagaManagerIntegrationEventService.cs b/src/Services/SagaManager/SagaManager/IntegrationEvents/ISagaManagerIntegrationEventService.cs deleted file mode 100644 index 41407702d..000000000 --- a/src/Services/SagaManager/SagaManager/IntegrationEvents/ISagaManagerIntegrationEventService.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace SagaManager.IntegrationEvents -{ - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - - public interface ISagaManagerIntegrationEventService - { - void PublishThroughEventBus(IntegrationEvent evt); - } -} \ No newline at end of file diff --git a/src/Services/SagaManager/SagaManager/IntegrationEvents/SagaManagerIntegrationEventService.cs b/src/Services/SagaManager/SagaManager/IntegrationEvents/SagaManagerIntegrationEventService.cs deleted file mode 100644 index 5107881ef..000000000 --- a/src/Services/SagaManager/SagaManager/IntegrationEvents/SagaManagerIntegrationEventService.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace SagaManager.IntegrationEvents -{ - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - using System; - - public class SagaManagerIntegrationEventService : ISagaManagerIntegrationEventService - { - private readonly IEventBus _eventBus; - - public SagaManagerIntegrationEventService(IEventBus eventBus) - => _eventBus = eventBus ?? throw new ArgumentNullException(nameof(eventBus)); - - - public void PublishThroughEventBus(IntegrationEvent evt) => _eventBus.Publish(evt); - } -} \ No newline at end of file diff --git a/src/Services/SagaManager/SagaManager/Program.cs b/src/Services/SagaManager/SagaManager/Program.cs index 97784fc85..5e07bddad 100644 --- a/src/Services/SagaManager/SagaManager/Program.cs +++ b/src/Services/SagaManager/SagaManager/Program.cs @@ -14,7 +14,6 @@ using Microsoft.Extensions.Options; using RabbitMQ.Client; using Services; - using IntegrationEvents; public class Program { @@ -34,11 +33,13 @@ var sagaManagerService = serviceProvider .GetRequiredService(); + var checkUpdateTime = serviceProvider + .GetRequiredService>().Value.CheckUpdateTime; while (true) { sagaManagerService.CheckConfirmedGracePeriodOrders(); - await Task.Delay(90000); + await Task.Delay(checkUpdateTime); } } @@ -58,8 +59,6 @@ .AddOptions() .Configure(Configuration) .AddSingleton() - .AddSingleton() - .AddSingleton(sp => { var settings = sp.GetRequiredService>().Value; diff --git a/src/Services/SagaManager/SagaManager/SagaManagerSettings.cs b/src/Services/SagaManager/SagaManager/SagaManagerSettings.cs index ab9184b8a..3b6d3bd08 100644 --- a/src/Services/SagaManager/SagaManager/SagaManagerSettings.cs +++ b/src/Services/SagaManager/SagaManager/SagaManagerSettings.cs @@ -6,6 +6,8 @@ public string EventBusConnection { get; set; } - public int GracePeriod { get; set; } + public int GracePeriodTime { get; set; } + + public int CheckUpdateTime { get; set; } } } \ No newline at end of file diff --git a/src/Services/SagaManager/SagaManager/Services/SagaManagerService.cs b/src/Services/SagaManager/SagaManager/Services/SagaManagerService.cs index f7455e27c..fd5ab1438 100644 --- a/src/Services/SagaManager/SagaManager/Services/SagaManagerService.cs +++ b/src/Services/SagaManager/SagaManager/Services/SagaManagerService.cs @@ -5,21 +5,21 @@ using Microsoft.Extensions.Options; using Microsoft.Extensions.Logging; using Dapper; - using IntegrationEvents; using IntegrationEvents.Events; + using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; public class SagaManagerService : ISagaManagerService { private readonly SagaManagerSettings _settings; - private readonly ISagaManagerIntegrationEventService _sagaManagerIntegrationEventService; + private readonly IEventBus _eventBus; private readonly ILogger _logger; public SagaManagerService(IOptions settings, - ISagaManagerIntegrationEventService sagaManagerIntegrationEventService, + IEventBus eventBus, ILogger logger) { _settings = settings.Value; - _sagaManagerIntegrationEventService = sagaManagerIntegrationEventService; + _eventBus = eventBus; _logger = logger; } @@ -29,8 +29,8 @@ foreach (var orderId in orderIds) { - var confirmGracePeriodEvent = new ConfirmGracePeriodCommand(orderId); - _sagaManagerIntegrationEventService.PublishThroughEventBus(confirmGracePeriodEvent); + var confirmGracePeriodEvent = new GracePeriodConfirmedIntegrationEvent(orderId); + _eventBus.Publish(confirmGracePeriodEvent); } } @@ -45,9 +45,9 @@ conn.Open(); orderIds = conn.Query( @"SELECT Id FROM [Microsoft.eShopOnContainers.Services.OrderingDb].[ordering].[orders] - WHERE DATEDIFF(hour, [OrderDate], GETDATE()) >= @GracePeriod + WHERE DATEDIFF(hour, [OrderDate], GETDATE()) >= @GracePeriodTime AND [OrderStatusId] = 1", - new { GracePeriod = _settings.GracePeriod }); + new { GracePeriodTime = _settings.GracePeriodTime }); } catch (SqlException exception) { diff --git a/src/Services/SagaManager/SagaManager/appsettings.json b/src/Services/SagaManager/SagaManager/appsettings.json index 00472e82a..0a514a514 100644 --- a/src/Services/SagaManager/SagaManager/appsettings.json +++ b/src/Services/SagaManager/SagaManager/appsettings.json @@ -7,5 +7,7 @@ "Microsoft": "Information" } }, - "ConnectionString": "Server=tcp:127.0.0.1,5433;Database=Microsoft.eShopOnContainers.Services.OrderingDb;User Id=sa;Password=Pass@word;" + "ConnectionString": "Server=tcp:127.0.0.1,5433;Database=Microsoft.eShopOnContainers.Services.OrderingDb;User Id=sa;Password=Pass@word;", + "GracePeriodTime": "15", + "CheckUpdateTime": "30000" } From cceca92cf9f80177703fd584e1db1e282c8aa001 Mon Sep 17 00:00:00 2001 From: Christian Arenas Date: Wed, 17 May 2017 19:42:45 +0200 Subject: [PATCH 2/7] Remove PaymentIntegrationEventService and remove integration command to integration event --- .../CommandHandlers/PayOrderCommandHandler.cs | 27 -------------- .../Commands/PayOrderCommand.cs | 11 ------ ...ToStockConfirmedIntegrationEventHandler.cs | 37 +++++++++++++++++++ ...ChangedToStockConfirmedIntegrationEvent.cs | 12 ++++++ .../IPaymentIntegrationEventService.cs | 9 ----- .../PaymentIntegrationEventService.cs | 21 ----------- .../Payment/Payment.API/PaymentSettings.cs | 8 ++++ src/Services/Payment/Payment.API/Startup.cs | 16 ++++---- .../Payment/Payment.API/appsettings.json | 3 +- 9 files changed, 67 insertions(+), 77 deletions(-) delete mode 100644 src/Services/Payment/Payment.API/IntegrationCommands/CommandHandlers/PayOrderCommandHandler.cs delete mode 100644 src/Services/Payment/Payment.API/IntegrationCommands/Commands/PayOrderCommand.cs create mode 100644 src/Services/Payment/Payment.API/IntegrationEvents/EventHandling/OrderStatusChangedToStockConfirmedIntegrationEventHandler.cs create mode 100644 src/Services/Payment/Payment.API/IntegrationEvents/Events/OrderStatusChangedToStockConfirmedIntegrationEvent.cs delete mode 100644 src/Services/Payment/Payment.API/IntegrationEvents/IPaymentIntegrationEventService.cs delete mode 100644 src/Services/Payment/Payment.API/IntegrationEvents/PaymentIntegrationEventService.cs create mode 100644 src/Services/Payment/Payment.API/PaymentSettings.cs diff --git a/src/Services/Payment/Payment.API/IntegrationCommands/CommandHandlers/PayOrderCommandHandler.cs b/src/Services/Payment/Payment.API/IntegrationCommands/CommandHandlers/PayOrderCommandHandler.cs deleted file mode 100644 index ea1398cf8..000000000 --- a/src/Services/Payment/Payment.API/IntegrationCommands/CommandHandlers/PayOrderCommandHandler.cs +++ /dev/null @@ -1,27 +0,0 @@ -namespace Payment.API.IntegrationCommands.CommandHandlers -{ - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; - using Payment.API.IntegrationCommands.Commands; - using System.Threading.Tasks; - using Payment.API.IntegrationEvents; - using Payment.API.IntegrationEvents.Events; - - public class PayOrderCommandHandler : IIntegrationEventHandler - { - private readonly IPaymentIntegrationEventService _paymentIntegrationEventService; - - public PayOrderCommandHandler(IPaymentIntegrationEventService paymentIntegrationEventService) - => _paymentIntegrationEventService = paymentIntegrationEventService; - - public async Task Handle(PayOrderCommand @event) - { - //PAYMENT SUCCESSED - var orderPaymentSuccededIntegrationEvent = new OrderPaymentSuccededIntegrationEvent(@event.OrderId); - _paymentIntegrationEventService.PublishThroughEventBus(orderPaymentSuccededIntegrationEvent); - - //PAYMENT FAILED - //var orderPaymentFailedIntegrationEvent = new OrderPaymentFailedIntegrationEvent(@event.OrderId); - //_paymentIntegrationEventService.PublishThroughEventBus(orderPaymentFailedIntegrationEvent); - } - } -} \ No newline at end of file diff --git a/src/Services/Payment/Payment.API/IntegrationCommands/Commands/PayOrderCommand.cs b/src/Services/Payment/Payment.API/IntegrationCommands/Commands/PayOrderCommand.cs deleted file mode 100644 index d6476fa6e..000000000 --- a/src/Services/Payment/Payment.API/IntegrationCommands/Commands/PayOrderCommand.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace Payment.API.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/Payment/Payment.API/IntegrationEvents/EventHandling/OrderStatusChangedToStockConfirmedIntegrationEventHandler.cs b/src/Services/Payment/Payment.API/IntegrationEvents/EventHandling/OrderStatusChangedToStockConfirmedIntegrationEventHandler.cs new file mode 100644 index 000000000..805431704 --- /dev/null +++ b/src/Services/Payment/Payment.API/IntegrationEvents/EventHandling/OrderStatusChangedToStockConfirmedIntegrationEventHandler.cs @@ -0,0 +1,37 @@ +namespace Payment.API.IntegrationEvents.EventHandling +{ + using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; + using System.Threading.Tasks; + using Payment.API.IntegrationEvents.Events; + using Microsoft.Extensions.Options; + using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; + + public class OrderStatusChangedToStockConfirmedIntegrationEventHandler : + IIntegrationEventHandler + { + private readonly IEventBus _eventBus; + private readonly PaymentSettings _settings; + + public OrderStatusChangedToStockConfirmedIntegrationEventHandler(IEventBus eventBus, + IOptionsSnapshot settings) + { + _eventBus = eventBus; + _settings = settings.Value; + } + + public async Task Handle(OrderStatusChangedToStockConfirmedIntegrationEvent @event) + { + IntegrationEvent orderPaymentIntegrationEvent; + if(_settings.SuccessPayment) + { + orderPaymentIntegrationEvent = new OrderPaymentSuccededIntegrationEvent(@event.OrderId); + } + else + { + orderPaymentIntegrationEvent = new OrderPaymentFailedIntegrationEvent(@event.OrderId); + } + + _eventBus.Publish(orderPaymentIntegrationEvent); + } + } +} \ No newline at end of file diff --git a/src/Services/Payment/Payment.API/IntegrationEvents/Events/OrderStatusChangedToStockConfirmedIntegrationEvent.cs b/src/Services/Payment/Payment.API/IntegrationEvents/Events/OrderStatusChangedToStockConfirmedIntegrationEvent.cs new file mode 100644 index 000000000..88f60e0d6 --- /dev/null +++ b/src/Services/Payment/Payment.API/IntegrationEvents/Events/OrderStatusChangedToStockConfirmedIntegrationEvent.cs @@ -0,0 +1,12 @@ +namespace Payment.API.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/Payment/Payment.API/IntegrationEvents/IPaymentIntegrationEventService.cs b/src/Services/Payment/Payment.API/IntegrationEvents/IPaymentIntegrationEventService.cs deleted file mode 100644 index f34315763..000000000 --- a/src/Services/Payment/Payment.API/IntegrationEvents/IPaymentIntegrationEventService.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace Payment.API.IntegrationEvents -{ - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - - public interface IPaymentIntegrationEventService - { - void PublishThroughEventBus(IntegrationEvent evt); - } -} diff --git a/src/Services/Payment/Payment.API/IntegrationEvents/PaymentIntegrationEventService.cs b/src/Services/Payment/Payment.API/IntegrationEvents/PaymentIntegrationEventService.cs deleted file mode 100644 index 7b2f37814..000000000 --- a/src/Services/Payment/Payment.API/IntegrationEvents/PaymentIntegrationEventService.cs +++ /dev/null @@ -1,21 +0,0 @@ -namespace Payment.API.IntegrationEvents -{ - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - using System; - - public class PaymentIntegrationEventService : IPaymentIntegrationEventService - { - private readonly IEventBus _eventBus; - - public PaymentIntegrationEventService(IEventBus eventBus) - { - _eventBus = eventBus ?? throw new ArgumentNullException(nameof(eventBus)); - } - - public void PublishThroughEventBus(IntegrationEvent evt) - { - _eventBus.Publish(evt); ; - } - } -} \ No newline at end of file diff --git a/src/Services/Payment/Payment.API/PaymentSettings.cs b/src/Services/Payment/Payment.API/PaymentSettings.cs new file mode 100644 index 000000000..dc0776e2e --- /dev/null +++ b/src/Services/Payment/Payment.API/PaymentSettings.cs @@ -0,0 +1,8 @@ +namespace Payment.API +{ + public class PaymentSettings + { + public bool SuccessPayment { get; set; } + public string EventBusConnection { get; set; } + } +} diff --git a/src/Services/Payment/Payment.API/Startup.cs b/src/Services/Payment/Payment.API/Startup.cs index e7a47f766..c2db0194d 100644 --- a/src/Services/Payment/Payment.API/Startup.cs +++ b/src/Services/Payment/Payment.API/Startup.cs @@ -7,12 +7,11 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; -using Payment.API.IntegrationCommands.Commands; using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ; using RabbitMQ.Client; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus; -using Payment.API.IntegrationEvents; -using Payment.API.IntegrationCommands.CommandHandlers; +using Payment.API.IntegrationEvents.Events; +using Payment.API.IntegrationEvents.EventHandling; namespace Payment.API { @@ -35,8 +34,7 @@ namespace Payment.API { // Add framework services. services.AddMvc(); - - services.AddTransient(); + services.Configure(Configuration); services.AddSingleton(sp => { var logger = sp.GetRequiredService>(); @@ -88,13 +86,15 @@ namespace Payment.API services.AddSingleton(); services.AddSingleton(); - services.AddTransient, PayOrderCommandHandler>(); + services.AddTransient, + OrderStatusChangedToStockConfirmedIntegrationEventHandler>(); } private void ConfigureEventBus(IApplicationBuilder app) { var eventBus = app.ApplicationServices.GetRequiredService(); - eventBus.Subscribe>(); + eventBus.Subscribe>(); } } -} +} \ No newline at end of file diff --git a/src/Services/Payment/Payment.API/appsettings.json b/src/Services/Payment/Payment.API/appsettings.json index 5fff67bac..285d91c4b 100644 --- a/src/Services/Payment/Payment.API/appsettings.json +++ b/src/Services/Payment/Payment.API/appsettings.json @@ -4,5 +4,6 @@ "LogLevel": { "Default": "Warning" } - } + }, + "SuccessPayment": "true" } From 429d27b9ca7347f9af14c1ef1dc6fdedebbd96cd Mon Sep 17 00:00:00 2001 From: Christian Arenas Date: Thu, 18 May 2017 09:43:37 +0200 Subject: [PATCH 3/7] Change variable parameter name --- .../Basket.API/Controllers/BasketController.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Services/Basket/Basket.API/Controllers/BasketController.cs b/src/Services/Basket/Basket.API/Controllers/BasketController.cs index 75efe4740..7cfbbc7dd 100644 --- a/src/Services/Basket/Basket.API/Controllers/BasketController.cs +++ b/src/Services/Basket/Basket.API/Controllers/BasketController.cs @@ -53,16 +53,16 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers [Route("checkout")] [HttpPost] - public async Task Checkout([FromBody]BasketCheckout value, [FromHeader(Name = "x-requestid")] string requestId) + public async Task Checkout([FromBody]BasketCheckout basketCheckout, [FromHeader(Name = "x-requestid")] string requestId) { var userId = _identitySvc.GetUserIdentity(); - value.RequestId = (Guid.TryParse(requestId, out Guid guid) && guid != Guid.Empty) ? - guid : value.RequestId; + basketCheckout.RequestId = (Guid.TryParse(requestId, out Guid guid) && guid != Guid.Empty) ? + guid : basketCheckout.RequestId; var basket = await _repository.GetBasketAsync(userId); - var eventMessage = new UserCheckoutAcceptedIntegrationEvent(userId, value.City, value.Street, - value.State, value.Country, value.ZipCode, value.CardNumber, value.CardHolderName, - value.CardExpiration, value.CardSecurityNumber, value.CardTypeId, value.Buyer, value.RequestId, basket); + var eventMessage = new UserCheckoutAcceptedIntegrationEvent(userId, basketCheckout.City, basketCheckout.Street, + basketCheckout.State, basketCheckout.Country, basketCheckout.ZipCode, basketCheckout.CardNumber, basketCheckout.CardHolderName, + basketCheckout.CardExpiration, basketCheckout.CardSecurityNumber, basketCheckout.CardTypeId, basketCheckout.Buyer, basketCheckout.RequestId, basket); // Once basket is checkout, sends an integration event to // ordering.api to convert basket to order and proceeds with From 31bdcd0b2f78428ddf90aca99f4694a902beedf9 Mon Sep 17 00:00:00 2001 From: Christian Arenas Date: Thu, 18 May 2017 11:37:50 +0200 Subject: [PATCH 4/7] Changing status item condition in order view --- src/Web/WebMVC/Views/Order/Index.cshtml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Web/WebMVC/Views/Order/Index.cshtml b/src/Web/WebMVC/Views/Order/Index.cshtml index 227b95686..8216c264d 100644 --- a/src/Web/WebMVC/Views/Order/Index.cshtml +++ b/src/Web/WebMVC/Views/Order/Index.cshtml @@ -31,9 +31,9 @@ Detail
- @if ((item.Status.ToLower() != "shipped") && (item.Status.ToLower() != "cancelled")) + @if (item.Status.ToLower() == "submited") { - Cancel + Cancel }
From 2228ca36125290196c4d18568c8c89ffb30e8561 Mon Sep 17 00:00:00 2001 From: Christian Arenas Date: Thu, 18 May 2017 11:42:22 +0200 Subject: [PATCH 5/7] - 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) From 6b36972a7f24b2a55aa13cb1c2e0784ea11a342e Mon Sep 17 00:00:00 2001 From: Christian Arenas Date: Thu, 18 May 2017 11:43:19 +0200 Subject: [PATCH 6/7] - Change Integration Command to Integration Events - Rename OrderStockNotConfirmedIntegrationEvent to OrderStockRejectedIntegrationEvent --- .../Catalog/Catalog.API/Catalog.API.csproj | 5 ++++ ...itingValidationIntegrationEventHandler.cs} | 28 ++++++------------- ...usChangedToPaidIntegrationEventHandler.cs} | 11 ++++---- ...edToAwaitingValidationIntegrationEvent.cs} | 6 ++-- ...derStatusChangedToPaidIntegrationEvent.cs} | 6 ++-- ... => OrderStockRejectedIntegrationEvent.cs} | 4 +-- src/Services/Catalog/Catalog.API/Startup.cs | 22 +++++++-------- 7 files changed, 38 insertions(+), 44 deletions(-) rename src/Services/Catalog/Catalog.API/{IntegrationCommands/CommandHandlers/ConfirmOrderStockCommandHandler.cs => IntegrationEvents/EventHandling/OrderStatusChangedToAwaitingValidationIntegrationEventHandler.cs} (62%) rename src/Services/Catalog/Catalog.API/{IntegrationCommands/CommandHandlers/DecrementOrderStockCommandHandler.cs => IntegrationEvents/EventHandling/OrderStatusChangedToPaidIntegrationEventHandler.cs} (61%) rename src/Services/Catalog/Catalog.API/{IntegrationCommands/Commands/ConfirmOrderStockCommand.cs => IntegrationEvents/Events/OrderStatusChangedToAwaitingValidationIntegrationEvent.cs} (77%) rename src/Services/Catalog/Catalog.API/{IntegrationCommands/Commands/DecrementOrderStockCommand.cs => IntegrationEvents/Events/OrderStatusChangedToPaidIntegrationEvent.cs} (72%) rename src/Services/Catalog/Catalog.API/IntegrationEvents/Events/{OrderStockNotConfirmedIntegrationEvent.cs => OrderStockRejectedIntegrationEvent.cs} (83%) diff --git a/src/Services/Catalog/Catalog.API/Catalog.API.csproj b/src/Services/Catalog/Catalog.API/Catalog.API.csproj index 3c2686278..2f0a0d20a 100644 --- a/src/Services/Catalog/Catalog.API/Catalog.API.csproj +++ b/src/Services/Catalog/Catalog.API/Catalog.API.csproj @@ -74,4 +74,9 @@ + + + + + diff --git a/src/Services/Catalog/Catalog.API/IntegrationCommands/CommandHandlers/ConfirmOrderStockCommandHandler.cs b/src/Services/Catalog/Catalog.API/IntegrationEvents/EventHandling/OrderStatusChangedToAwaitingValidationIntegrationEventHandler.cs similarity index 62% rename from src/Services/Catalog/Catalog.API/IntegrationCommands/CommandHandlers/ConfirmOrderStockCommandHandler.cs rename to src/Services/Catalog/Catalog.API/IntegrationEvents/EventHandling/OrderStatusChangedToAwaitingValidationIntegrationEventHandler.cs index 7edfd5cf7..0f30a3e0a 100644 --- a/src/Services/Catalog/Catalog.API/IntegrationCommands/CommandHandlers/ConfirmOrderStockCommandHandler.cs +++ b/src/Services/Catalog/Catalog.API/IntegrationEvents/EventHandling/OrderStatusChangedToAwaitingValidationIntegrationEventHandler.cs @@ -1,4 +1,4 @@ -namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationCommands.CommandHandlers +namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.EventHandling { using BuildingBlocks.EventBus.Abstractions; using System.Threading.Tasks; @@ -6,53 +6,41 @@ using Infrastructure; using System.Collections.Generic; using System.Linq; - using global::Catalog.API.Infrastructure.Exceptions; using global::Catalog.API.IntegrationEvents; - using Model; - using Commands; using IntegrationEvents.Events; - public class ConfirmOrderStockCommandHandler : IIntegrationEventHandler + public class OrderStatusChangedToAwaitingValidationIntegrationEventHandler : + IIntegrationEventHandler { private readonly CatalogContext _catalogContext; private readonly ICatalogIntegrationEventService _catalogIntegrationEventService; - public ConfirmOrderStockCommandHandler(CatalogContext catalogContext, + public OrderStatusChangedToAwaitingValidationIntegrationEventHandler(CatalogContext catalogContext, ICatalogIntegrationEventService catalogIntegrationEventService) { _catalogContext = catalogContext; _catalogIntegrationEventService = catalogIntegrationEventService; } - public async Task Handle(ConfirmOrderStockCommand command) + public async Task Handle(OrderStatusChangedToAwaitingValidationIntegrationEvent command) { var confirmedOrderStockItems = new List(); foreach (var orderStockItem in command.OrderStockItems) { var catalogItem = _catalogContext.CatalogItems.Find(orderStockItem.ProductId); - CheckValidcatalogItemId(catalogItem); - - var confirmedOrderStockItem = new ConfirmedOrderStockItem(catalogItem.Id, - catalogItem.AvailableStock >= orderStockItem.Units); + var hasStock = catalogItem.AvailableStock >= orderStockItem.Units; + var confirmedOrderStockItem = new ConfirmedOrderStockItem(catalogItem.Id, hasStock); confirmedOrderStockItems.Add(confirmedOrderStockItem); } var confirmedIntegrationEvent = confirmedOrderStockItems.Any(c => !c.HasStock) - ? (IntegrationEvent) new OrderStockNotConfirmedIntegrationEvent(command.OrderId, confirmedOrderStockItems) + ? (IntegrationEvent) new OrderStockRejectedIntegrationEvent(command.OrderId, confirmedOrderStockItems) : new OrderStockConfirmedIntegrationEvent(command.OrderId); await _catalogIntegrationEventService.SaveEventAndCatalogContextChangesAsync(confirmedIntegrationEvent); await _catalogIntegrationEventService.PublishThroughEventBusAsync(confirmedIntegrationEvent); } - - private void CheckValidcatalogItemId(CatalogItem catalogItem) - { - if (catalogItem is null) - { - throw new CatalogDomainException("Not able to process catalog event. Reason: no valid catalogItemId"); - } - } } } \ No newline at end of file diff --git a/src/Services/Catalog/Catalog.API/IntegrationCommands/CommandHandlers/DecrementOrderStockCommandHandler.cs b/src/Services/Catalog/Catalog.API/IntegrationEvents/EventHandling/OrderStatusChangedToPaidIntegrationEventHandler.cs similarity index 61% rename from src/Services/Catalog/Catalog.API/IntegrationCommands/CommandHandlers/DecrementOrderStockCommandHandler.cs rename to src/Services/Catalog/Catalog.API/IntegrationEvents/EventHandling/OrderStatusChangedToPaidIntegrationEventHandler.cs index 1c7d1b9a3..0a45547f9 100644 --- a/src/Services/Catalog/Catalog.API/IntegrationCommands/CommandHandlers/DecrementOrderStockCommandHandler.cs +++ b/src/Services/Catalog/Catalog.API/IntegrationEvents/EventHandling/OrderStatusChangedToPaidIntegrationEventHandler.cs @@ -1,20 +1,21 @@ -namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationCommands.CommandHandlers +namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.EventHandling { using BuildingBlocks.EventBus.Abstractions; using System.Threading.Tasks; using Infrastructure; - using Commands; + using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.Events; - public class DecrementOrderStockCommandHandler : IIntegrationEventHandler + public class OrderStatusChangedToPaidIntegrationEventHandler : + IIntegrationEventHandler { private readonly CatalogContext _catalogContext; - public DecrementOrderStockCommandHandler(CatalogContext catalogContext) + public OrderStatusChangedToPaidIntegrationEventHandler(CatalogContext catalogContext) { _catalogContext = catalogContext; } - public async Task Handle(DecrementOrderStockCommand command) + public async Task Handle(OrderStatusChangedToPaidIntegrationEvent command) { //we're not blocking stock/inventory foreach (var orderStockItem in command.OrderStockItems) diff --git a/src/Services/Catalog/Catalog.API/IntegrationCommands/Commands/ConfirmOrderStockCommand.cs b/src/Services/Catalog/Catalog.API/IntegrationEvents/Events/OrderStatusChangedToAwaitingValidationIntegrationEvent.cs similarity index 77% rename from src/Services/Catalog/Catalog.API/IntegrationCommands/Commands/ConfirmOrderStockCommand.cs rename to src/Services/Catalog/Catalog.API/IntegrationEvents/Events/OrderStatusChangedToAwaitingValidationIntegrationEvent.cs index 2c5296776..9787aaedd 100644 --- a/src/Services/Catalog/Catalog.API/IntegrationCommands/Commands/ConfirmOrderStockCommand.cs +++ b/src/Services/Catalog/Catalog.API/IntegrationEvents/Events/OrderStatusChangedToAwaitingValidationIntegrationEvent.cs @@ -1,14 +1,14 @@ -namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationCommands.Commands +namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.Events { using BuildingBlocks.EventBus.Events; using System.Collections.Generic; - 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/Catalog/Catalog.API/IntegrationCommands/Commands/DecrementOrderStockCommand.cs b/src/Services/Catalog/Catalog.API/IntegrationEvents/Events/OrderStatusChangedToPaidIntegrationEvent.cs similarity index 72% rename from src/Services/Catalog/Catalog.API/IntegrationCommands/Commands/DecrementOrderStockCommand.cs rename to src/Services/Catalog/Catalog.API/IntegrationEvents/Events/OrderStatusChangedToPaidIntegrationEvent.cs index ca241d6fd..881aa21fe 100644 --- a/src/Services/Catalog/Catalog.API/IntegrationCommands/Commands/DecrementOrderStockCommand.cs +++ b/src/Services/Catalog/Catalog.API/IntegrationEvents/Events/OrderStatusChangedToPaidIntegrationEvent.cs @@ -1,14 +1,14 @@ -namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationCommands.Commands +namespace Microsoft.eShopOnContainers.Services.Catalog.API.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/Catalog/Catalog.API/IntegrationEvents/Events/OrderStockNotConfirmedIntegrationEvent.cs b/src/Services/Catalog/Catalog.API/IntegrationEvents/Events/OrderStockRejectedIntegrationEvent.cs similarity index 83% rename from src/Services/Catalog/Catalog.API/IntegrationEvents/Events/OrderStockNotConfirmedIntegrationEvent.cs rename to src/Services/Catalog/Catalog.API/IntegrationEvents/Events/OrderStockRejectedIntegrationEvent.cs index b32b0dff5..eb74af8dd 100644 --- a/src/Services/Catalog/Catalog.API/IntegrationEvents/Events/OrderStockNotConfirmedIntegrationEvent.cs +++ b/src/Services/Catalog/Catalog.API/IntegrationEvents/Events/OrderStockRejectedIntegrationEvent.cs @@ -3,13 +3,13 @@ using 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/Catalog/Catalog.API/Startup.cs b/src/Services/Catalog/Catalog.API/Startup.cs index 1f65e6a91..15a385867 100644 --- a/src/Services/Catalog/Catalog.API/Startup.cs +++ b/src/Services/Catalog/Catalog.API/Startup.cs @@ -14,9 +14,8 @@ using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF; using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services; using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure; - using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationCommands.Commands; - using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents; - using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationCommands.CommandHandlers; + using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.EventHandling; + using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.Events; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.HealthChecks; @@ -27,7 +26,7 @@ using System.Data.Common; using System.Data.SqlClient; using System.Reflection; - + public class Startup { public IConfigurationRoot Configuration { get; } @@ -190,19 +189,20 @@ services.AddSingleton(); services.AddSingleton(); - services.AddTransient, - ConfirmOrderStockCommandHandler>(); - services.AddTransient, - DecrementOrderStockCommandHandler>(); - + services.AddTransient, + OrderStatusChangedToAwaitingValidationIntegrationEventHandler>(); + services.AddTransient, + OrderStatusChangedToPaidIntegrationEventHandler>(); } private void ConfigureEventBus(IApplicationBuilder app) { var eventBus = app.ApplicationServices.GetRequiredService(); - eventBus.Subscribe>(); - eventBus.Subscribe>(); + eventBus.Subscribe>(); + eventBus.Subscribe>(); } } } From 689096f4c83c19abd72267c61335db6d254247a3 Mon Sep 17 00:00:00 2001 From: Christian Arenas Date: Thu, 18 May 2017 12:03:17 +0200 Subject: [PATCH 7/7] - Remove unused IntegrationCommands folders - minor name changes --- src/Services/Catalog/Catalog.API/Catalog.API.csproj | 5 ----- .../OrderStockRejectedIntegrationEventHandler.cs | 4 ++-- .../Ordering.API/Application/Sagas/OrderSaga.cs | 4 +--- src/Services/Ordering/Ordering.API/Ordering.API.csproj | 2 -- .../AggregatesModel/OrderAggregate/Order.cs | 10 +++++----- 5 files changed, 8 insertions(+), 17 deletions(-) diff --git a/src/Services/Catalog/Catalog.API/Catalog.API.csproj b/src/Services/Catalog/Catalog.API/Catalog.API.csproj index 2f0a0d20a..3c2686278 100644 --- a/src/Services/Catalog/Catalog.API/Catalog.API.csproj +++ b/src/Services/Catalog/Catalog.API/Catalog.API.csproj @@ -74,9 +74,4 @@ - - - - - diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockRejectedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockRejectedIntegrationEventHandler.cs index 124bb8a11..c70eba187 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockRejectedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockRejectedIntegrationEventHandler.cs @@ -19,11 +19,11 @@ { var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId); - var orderStockNotConfirmedItems = @event.OrderStockItems + var orderStockRejectedItems = @event.OrderStockItems .FindAll(c => !c.HasStock) .Select(c => c.ProductId); - orderToUpdate.SetCancelledStatusWhenStockIsRejected(orderStockNotConfirmedItems); + orderToUpdate.SetCancelledStatusWhenStockIsRejected(orderStockRejectedItems); await _orderRepository.UnitOfWork.SaveEntitiesAsync(); } diff --git a/src/Services/Ordering/Ordering.API/Application/Sagas/OrderSaga.cs b/src/Services/Ordering/Ordering.API/Application/Sagas/OrderSaga.cs index b1041971a..a570d7eca 100644 --- a/src/Services/Ordering/Ordering.API/Application/Sagas/OrderSaga.cs +++ b/src/Services/Ordering/Ordering.API/Application/Sagas/OrderSaga.cs @@ -17,13 +17,11 @@ namespace Ordering.API.Application.Sagas public override Order FindSagaById(int id) { - var order = _orderingContext.Orders + return _orderingContext.Orders .Include(c => c.OrderStatus) .Include(c => c.OrderItems) .Include(c => c.Address) .Single(c => c.Id == id); - - return order; } public override async Task SaveChangesAsync() diff --git a/src/Services/Ordering/Ordering.API/Ordering.API.csproj b/src/Services/Ordering/Ordering.API/Ordering.API.csproj index e0970db38..63f98fc9a 100644 --- a/src/Services/Ordering/Ordering.API/Ordering.API.csproj +++ b/src/Services/Ordering/Ordering.API/Ordering.API.csproj @@ -79,8 +79,6 @@ - - diff --git a/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs b/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs index 1f3a0dbbf..a7389a227 100644 --- a/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs +++ b/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs @@ -156,7 +156,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O _description = $"The order was cancelled."; } - public void SetCancelledStatusWhenStockIsRejected(IEnumerable orderStockNotConfirmedItems) + public void SetCancelledStatusWhenStockIsRejected(IEnumerable orderStockRejectedItems) { if (_orderStatusId != OrderStatus.AwaitingValidation.Id) { @@ -165,12 +165,12 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O _orderStatusId = OrderStatus.Cancelled.Id; - var itemsStockNotConfirmedProductNames = OrderItems - .Where(c => orderStockNotConfirmedItems.Contains(c.ProductId)) + var itemsStockRejectedProductNames = OrderItems + .Where(c => orderStockRejectedItems.Contains(c.ProductId)) .Select(c => c.GetOrderItemProductName()); - var itemsStockNotConfirmedDescription = string.Join(", ", itemsStockNotConfirmedProductNames); - _description = $"The product items don't have stock: ({itemsStockNotConfirmedDescription})."; + var itemsStockRejectedDescription = string.Join(", ", itemsStockRejectedProductNames); + _description = $"The product items don't have stock: ({itemsStockRejectedDescription})."; } private void AddOrderStartedDomainEvent(string userId, int cardTypeId, string cardNumber,