diff --git a/src/Services/Basket/Basket.API/Startup.cs b/src/Services/Basket/Basket.API/Startup.cs index 2c1e70645..84f6c0570 100644 --- a/src/Services/Basket/Basket.API/Startup.cs +++ b/src/Services/Basket/Basket.API/Startup.cs @@ -165,12 +165,6 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API protected virtual void ConfigureEventBus(IApplicationBuilder app) { - var catalogPriceHandler = app.ApplicationServices - .GetService>(); - - var orderStartedHandler = app.ApplicationServices - .GetService>(); - var eventBus = app.ApplicationServices.GetRequiredService(); eventBus.Subscribe(); diff --git a/src/Services/Catalog/Catalog.API/IntegrationCommands/CommandHandlers/DecrementOrderStockCommandMsgHandler.cs b/src/Services/Catalog/Catalog.API/IntegrationCommands/CommandHandlers/DecrementOrderStockCommandMsgHandler.cs index 9268aa0ba..a6bc43228 100644 --- a/src/Services/Catalog/Catalog.API/IntegrationCommands/CommandHandlers/DecrementOrderStockCommandMsgHandler.cs +++ b/src/Services/Catalog/Catalog.API/IntegrationCommands/CommandHandlers/DecrementOrderStockCommandMsgHandler.cs @@ -3,9 +3,6 @@ using BuildingBlocks.EventBus.Abstractions; using System.Threading.Tasks; using Infrastructure; - using global::Catalog.API.Infrastructure.Exceptions; - using global::Catalog.API.IntegrationEvents; - using Model; using Commands; public class DecrementOrderStockCommandMsgHandler : IIntegrationEventHandler diff --git a/src/Services/Catalog/Catalog.API/Startup.cs b/src/Services/Catalog/Catalog.API/Startup.cs index 22dd201d0..1820e1f04 100644 --- a/src/Services/Catalog/Catalog.API/Startup.cs +++ b/src/Services/Catalog/Catalog.API/Startup.cs @@ -121,10 +121,7 @@ return new DefaultRabbitMQPersistentConnection(factory, logger); }); - services.AddSingleton(); - services.AddSingleton(); - services.AddTransient, ConfirmOrderStockCommandMsgHandler>(); - services.AddTransient, DecrementOrderStockCommandMsgHandler>(); + RegisterServiceBus(services); var container = new ContainerBuilder(); container.Populate(services); @@ -188,6 +185,18 @@ } } + private void RegisterServiceBus(IServiceCollection services) + { + services.AddSingleton(); + services.AddSingleton(); + + services.AddTransient, + ConfirmOrderStockCommandMsgHandler>(); + services.AddTransient, + DecrementOrderStockCommandMsgHandler>(); + + } + private void ConfigureEventBus(IApplicationBuilder app) { var eventBus = app.ApplicationServices.GetRequiredService(); diff --git a/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs b/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs index 3ef80a69a..938f80a9c 100644 --- a/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/Commands/CreateOrderCommandHandler.cs @@ -43,7 +43,7 @@ // make sure that consistency is preserved across the whole aggregate var address = new Address(message.Street, message.City, message.State, message.Country, message.ZipCode); var order = new Order(message.UserId, address, message.CardTypeId, message.CardNumber, message.CardSecurityNumber, message.CardHolderName, message.CardExpiration); - order.SetSubmitedStatus(); + foreach (var item in message.OrderItems) { order.AddOrderItem(item.ProductId, item.ProductName, item.UnitPrice, item.Discount, item.PictureUrl, item.Units); 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 d6906705f..06fd8bcfe 100644 --- a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderGracePeriodConfirmed/OrderStatusChangedToAwaitingValidationDomainEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderGracePeriodConfirmed/OrderStatusChangedToAwaitingValidationDomainEventHandler.cs @@ -28,8 +28,6 @@ public async Task Handle(OrderStatusChangedToAwaitingValidationDomainEvent orderStatusChangedToAwaitingValidationDomainEvent) { - await _orderRepository.UnitOfWork.SaveEntitiesAsync(); - _logger.CreateLogger(nameof(OrderStatusChangedToAwaitingValidationDomainEvent)) .LogTrace($"Order with Id: {orderStatusChangedToAwaitingValidationDomainEvent.OrderId} has been successfully updated with " + $"a status order id: {OrderStatus.AwaitingValidation.Id}"); 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 3aff01578..ade15b149 100644 --- a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderPaid/OrderStatusChangedToPaidDomainEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderPaid/OrderStatusChangedToPaidDomainEventHandler.cs @@ -28,8 +28,6 @@ public async Task Handle(OrderStatusChangedToPaidDomainEvent orderStatusChangedToPaidDomainEvent) { - await _orderRepository.UnitOfWork.SaveEntitiesAsync(); - _logger.CreateLogger(nameof(OrderStatusChangedToPaidDomainEventHandler)) .LogTrace($"Order with Id: {orderStatusChangedToPaidDomainEvent.OrderId} has been successfully updated with " + $"a status order id: {OrderStatus.Paid.Id}"); 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 2de4f5095..55b6d6c9a 100644 --- a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmed/OrderStatusChangedToStockConfirmedDomainEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmed/OrderStatusChangedToStockConfirmedDomainEventHandler.cs @@ -27,8 +27,6 @@ public async Task Handle(OrderStatusChangedToStockConfirmedDomainEvent orderStatusChangedToStockConfirmedDomainEvent) { - await _orderRepository.UnitOfWork.SaveEntitiesAsync(); - _logger.CreateLogger(nameof(OrderStatusChangedToStockConfirmedDomainEventHandler)) .LogTrace($"Order with Id: {orderStatusChangedToStockConfirmedDomainEvent.OrderId} has been successfully updated with " + $"a status order id: {OrderStatus.StockConfirmed.Id}"); 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 49b252e4a..3f81f8f67 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs @@ -21,7 +21,7 @@ orderToUpdate.SetCancelledStatus(); - await _orderRepository.UnitOfWork.SaveChangesAsync(); + await _orderRepository.UnitOfWork.SaveEntitiesAsync(); } } } 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 fc813ff2d..80144d0ed 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs @@ -21,7 +21,7 @@ orderToUpdate.SetPaidStatus(); - await _orderRepository.UnitOfWork.SaveChangesAsync(); + await _orderRepository.UnitOfWork.SaveEntitiesAsync(); } } } \ No newline at end of file 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 172444932..ac4bc5936 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs @@ -21,7 +21,7 @@ orderToUpdate.SetStockConfirmedStatus(); - await _orderRepository.UnitOfWork.SaveChangesAsync(); + await _orderRepository.UnitOfWork.SaveEntitiesAsync(); } } } \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs index 918e4f094..f3003c0a4 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs @@ -27,7 +27,7 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling orderToUpdate.SetStockConfirmedStatus(orderStockNotConfirmedItems); - await _orderRepository.UnitOfWork.SaveChangesAsync(); + await _orderRepository.UnitOfWork.SaveEntitiesAsync(); } } } \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs b/src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs index f230ba6fb..08b699a1f 100644 --- a/src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs +++ b/src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs @@ -48,11 +48,8 @@ namespace Ordering.API.Application.Sagas var orderSaga = FindSagaById(command.OrderId); CheckValidSagaId(orderSaga); - if (orderSaga.OrderStatus != OrderStatus.Cancelled) - { - orderSaga.SetAwaitingValidationStatus(); - await SaveChangesAsync(); - } + orderSaga.SetAwaitingValidationStatus(); + await SaveChangesAsync(); } /// @@ -67,14 +64,9 @@ namespace Ordering.API.Application.Sagas var orderSaga = FindSagaById(command.OrderNumber); CheckValidSagaId(orderSaga); - // Not possible to cancel order when - // it has already been shipped - if (orderSaga.GetOrderStatusId() != OrderStatus.Cancelled.Id - || orderSaga.GetOrderStatusId() != OrderStatus.Shipped.Id) - { - orderSaga.SetCancelledStatus(); - result = await SaveChangesAsync(); - } + orderSaga.SetCancelledStatus(); + result = await SaveChangesAsync(); + return result; } @@ -90,13 +82,9 @@ namespace Ordering.API.Application.Sagas var orderSaga = FindSagaById(command.OrderNumber); CheckValidSagaId(orderSaga); - // Only ship order when - // its status is paid - if (orderSaga.GetOrderStatusId() == OrderStatus.Paid.Id) - { - orderSaga.SetShippedStatus(); - result = await SaveChangesAsync(); - } + orderSaga.SetShippedStatus(); + result = await SaveChangesAsync(); + return result; } diff --git a/src/Services/Ordering/Ordering.API/Startup.cs b/src/Services/Ordering/Ordering.API/Startup.cs index e2b0734f7..6f7828721 100644 --- a/src/Services/Ordering/Ordering.API/Startup.cs +++ b/src/Services/Ordering/Ordering.API/Startup.cs @@ -8,8 +8,6 @@ using global::Ordering.API.Application.IntegrationEvents.Events; using global::Ordering.API.Application.Sagas; using global::Ordering.API.Infrastructure.Middlewares; - using global::Ordering.API.Application.IntegrationCommands.Commands; - using global::Ordering.API.Application.Sagas; using Infrastructure; using Infrastructure.Auth; using Infrastructure.AutofacModules; @@ -126,18 +124,7 @@ return new DefaultRabbitMQPersistentConnection(factory, logger); }); - services.AddSingleton(); - services.AddSingleton(); - services.AddTransient>(); - services.AddTransient, OrderProcessSaga>(); - services.AddTransient, - OrderStockConfirmedIntegrationEventHandler>(); - services.AddTransient, - OrderStockNotConfirmedIntegrationEventHandler>(); - services.AddTransient, - OrderPaymentFailedIntegrationEventHandler>(); - services.AddTransient, - OrderPaymentSuccededIntegrationEventHandler>(); + RegisterServiceBus(services); services.AddOptions(); //configure autofac @@ -178,6 +165,23 @@ } + private void RegisterServiceBus(IServiceCollection services) + { + 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) { var eventBus = app.ApplicationServices.GetRequiredService(); diff --git a/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs b/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs index 51c201e6b..eaecba5f9 100644 --- a/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs +++ b/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs @@ -95,14 +95,11 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O } #region Status Changes - public void SetSubmitedStatus() - { - _orderStatusId = OrderStatus.Submited.Id; - } public void SetAwaitingValidationStatus() { - if (_orderStatusId != OrderStatus.Submited.Id) + if (_orderStatusId != OrderStatus.Submited.Id && + _orderStatusId != OrderStatus.Cancelled.Id) { StatusChangeException(); } @@ -167,7 +164,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O { if (_orderStatusId == OrderStatus.Submited.Id) { - _description = "The order was cancelled before the grace period was confirm."; + _description = "The order was cancelled before the grace period was confirmed."; } else if (_orderStatusId == OrderStatus.AwaitingValidation.Id) { @@ -191,11 +188,6 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O #endregion - public int GetOrderStatusId() - { - return _orderStatusId; - } - private void AddOrderStartedDomainEvent(string userId, int cardTypeId, string cardNumber, string cardSecurityNumber, string cardHolderName, DateTime cardExpiration) { diff --git a/src/Services/SagaManager/SagaManager/IntegrationEvents/Events/ConfirmGracePeriodCommandMsg.cs b/src/Services/SagaManager/SagaManager/IntegrationEvents/Events/ConfirmGracePeriodCommandMsg.cs index fc47f44e5..6521cd200 100644 --- a/src/Services/SagaManager/SagaManager/IntegrationEvents/Events/ConfirmGracePeriodCommandMsg.cs +++ b/src/Services/SagaManager/SagaManager/IntegrationEvents/Events/ConfirmGracePeriodCommandMsg.cs @@ -2,13 +2,10 @@ { using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - // Integration Events notes: - // An Event is “something that has happened in the past”, therefore its name has to be - // An Integration Event is an event that can cause side effects to other microsrvices, Bounded-Contexts or external systems. public class ConfirmGracePeriodCommandMsg : IntegrationEvent { public int OrderId { get;} public ConfirmGracePeriodCommandMsg(int orderId) => OrderId = orderId; } -} +} \ No newline at end of file diff --git a/src/Services/SagaManager/SagaManager/IntegrationEvents/SagaManagerIntegrationEventService.cs b/src/Services/SagaManager/SagaManager/IntegrationEvents/SagaManagerIntegrationEventService.cs index 3643c0530..5107881ef 100644 --- a/src/Services/SagaManager/SagaManager/IntegrationEvents/SagaManagerIntegrationEventService.cs +++ b/src/Services/SagaManager/SagaManager/IntegrationEvents/SagaManagerIntegrationEventService.cs @@ -9,13 +9,9 @@ private readonly IEventBus _eventBus; public SagaManagerIntegrationEventService(IEventBus eventBus) - { - _eventBus = eventBus ?? throw new ArgumentNullException(nameof(eventBus)); - } + => _eventBus = eventBus ?? throw new ArgumentNullException(nameof(eventBus)); - public void PublishThroughEventBus(IntegrationEvent evt) - { - _eventBus.Publish(evt); - } + + 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 95105b23b..08eb95157 100644 --- a/src/Services/SagaManager/SagaManager/Program.cs +++ b/src/Services/SagaManager/SagaManager/Program.cs @@ -1,34 +1,27 @@ -using System.Reflection; -using Microsoft.eShopOnContainers.BuildingBlocks.EventBus; -using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure; -using Microsoft.EntityFrameworkCore; -using SagaManager.IntegrationEvents; - -namespace SagaManager +namespace SagaManager { using System.IO; using System; + using System.Threading.Tasks; + using Autofac.Extensions.DependencyInjection; + using Autofac; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Microsoft.eShopOnContainers.BuildingBlocks.EventBusRabbitMQ; + using Microsoft.eShopOnContainers.BuildingBlocks.EventBus; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using RabbitMQ.Client; using Services; - using Autofac.Extensions.DependencyInjection; - using Autofac; - using System.Threading.Tasks; + using IntegrationEvents; public class Program { public static IConfigurationRoot Configuration { get; set; } - public static void Main(string[] args) - { - MainAsync().Wait(); - } - + public static void Main(string[] args) => MainAsync().Wait(); + static async Task MainAsync() { StartUp(); @@ -45,7 +38,7 @@ namespace SagaManager while (true) { sagaManagerService.CheckFinishedGracePeriodOrders(); - await Task.Delay(30000); + await Task.Delay(300000); } } @@ -66,8 +59,7 @@ namespace SagaManager .Configure(Configuration) .AddSingleton() .AddSingleton() - .AddSingleton() - .AddSingleton() + .AddSingleton(sp => { var settings = sp.GetRequiredService>().Value; @@ -78,8 +70,7 @@ namespace SagaManager }; return new DefaultRabbitMQPersistentConnection(factory, logger); - }) - .AddSingleton(); + }); RegisterServiceBus(services); diff --git a/src/Services/SagaManager/SagaManager/Services/SagaManagerService.cs b/src/Services/SagaManager/SagaManager/Services/SagaManagerService.cs index b40c2fb08..7ef5ae25a 100644 --- a/src/Services/SagaManager/SagaManager/Services/SagaManagerService.cs +++ b/src/Services/SagaManager/SagaManager/Services/SagaManagerService.cs @@ -1,12 +1,9 @@ -using System; -using System.Threading.Tasks; -using Microsoft.Extensions.Logging; - -namespace SagaManager.Services +namespace SagaManager.Services { using System.Collections.Generic; using System.Data.SqlClient; using Microsoft.Extensions.Options; + using Microsoft.Extensions.Logging; using Dapper; using IntegrationEvents; using IntegrationEvents.Events; @@ -32,7 +29,9 @@ namespace SagaManager.Services foreach (var orderId in orderIds) { - Publish(orderId); + var confirmGracePeriodEvent = new ConfirmGracePeriodCommandMsg(orderId); + + _sagaManagerIntegrationEventService.PublishThroughEventBus(confirmGracePeriodEvent); } } @@ -60,13 +59,5 @@ namespace SagaManager.Services return orderIds; } - - private void Publish(int orderId) - { - var confirmGracePeriodEvent = new ConfirmGracePeriodCommandMsg(orderId); - - // Publish through the Event Bus - _sagaManagerIntegrationEventService.PublishThroughEventBus(confirmGracePeriodEvent); - } } } \ No newline at end of file