diff --git a/docker-compose.yml b/docker-compose.yml index 75177c5db..55d84e39e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -90,3 +90,5 @@ services: build: context: ./src/Services/Payment/Payment.API dockerfile: Dockerfile + depends_on: + - rabbitmq diff --git a/src/Services/Basket/Basket.API/Startup.cs b/src/Services/Basket/Basket.API/Startup.cs index 953406f18..33553e6db 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(); eventBus.Subscribe(); diff --git a/src/Services/Catalog/Catalog.API/Infrastructure/CatalogContextSeed.cs b/src/Services/Catalog/Catalog.API/Infrastructure/CatalogContextSeed.cs index 5f2370fb6..9752b46db 100644 --- a/src/Services/Catalog/Catalog.API/Infrastructure/CatalogContextSeed.cs +++ b/src/Services/Catalog/Catalog.API/Infrastructure/CatalogContextSeed.cs @@ -70,18 +70,18 @@ { return new List() { - new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Bot Black Hoodie", Name = ".NET Bot Black Hoodie", Price = 19.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/1" }, - new CatalogItem() { CatalogTypeId=1,CatalogBrandId=2, Description = ".NET Black & White Mug", Name = ".NET Black & White Mug", Price= 8.50M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/2" }, - new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/3" }, - new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Foundation T-shirt", Name = ".NET Foundation T-shirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/4" }, - new CatalogItem() { CatalogTypeId=3,CatalogBrandId=5, Description = "Roslyn Red Sheet", Name = "Roslyn Red Sheet", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/5" }, - new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Blue Hoodie", Name = ".NET Blue Hoodie", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/6" }, - new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/7" }, - new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Kudu Purple Hoodie", Name = "Kudu Purple Hoodie", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/8" }, - new CatalogItem() { CatalogTypeId=1,CatalogBrandId=5, Description = "Cup White Mug", Name = "Cup White Mug", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/9" }, - new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = ".NET Foundation Sheet", Name = ".NET Foundation Sheet", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/10" }, - new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = "Cup Sheet", Name = "Cup Sheet", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/11" }, - new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White TShirt", Name = "Prism White TShirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/12" } + new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Bot Black Hoodie", Name = ".NET Bot Black Hoodie", Price = 19.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/1", AvailableStock = 1}, + new CatalogItem() { CatalogTypeId=1,CatalogBrandId=2, Description = ".NET Black & White Mug", Name = ".NET Black & White Mug", Price= 8.50M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/2", AvailableStock = 1 }, + new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White T-Shirt", Name = "Prism White T-Shirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/3", AvailableStock = 1 }, + new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Foundation T-shirt", Name = ".NET Foundation T-shirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/4", AvailableStock = 1 }, + new CatalogItem() { CatalogTypeId=3,CatalogBrandId=5, Description = "Roslyn Red Sheet", Name = "Roslyn Red Sheet", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/5", AvailableStock = 1 }, + new CatalogItem() { CatalogTypeId=2,CatalogBrandId=2, Description = ".NET Blue Hoodie", Name = ".NET Blue Hoodie", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/6", AvailableStock = 1 }, + new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Roslyn Red T-Shirt", Name = "Roslyn Red T-Shirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/7", AvailableStock = 1 }, + new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Kudu Purple Hoodie", Name = "Kudu Purple Hoodie", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/8", AvailableStock = 1 }, + new CatalogItem() { CatalogTypeId=1,CatalogBrandId=5, Description = "Cup White Mug", Name = "Cup White Mug", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/9", AvailableStock = 1 }, + new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = ".NET Foundation Sheet", Name = ".NET Foundation Sheet", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/10", AvailableStock = 1 }, + new CatalogItem() { CatalogTypeId=3,CatalogBrandId=2, Description = "Cup Sheet", Name = "Cup Sheet", Price = 8.5M, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/11", AvailableStock = 1 }, + new CatalogItem() { CatalogTypeId=2,CatalogBrandId=5, Description = "Prism White TShirt", Name = "Prism White TShirt", Price = 12, PictureUri = "http://externalcatalogbaseurltobereplaced/api/v1/pic/12", AvailableStock = 1 } }; } } diff --git a/src/Services/Catalog/Catalog.API/IntegrationCommands/CommandHandlers/ConfirmOrderStockCommandMsgHandler.cs b/src/Services/Catalog/Catalog.API/IntegrationCommands/CommandHandlers/ConfirmOrderStockCommandHandler.cs similarity index 80% rename from src/Services/Catalog/Catalog.API/IntegrationCommands/CommandHandlers/ConfirmOrderStockCommandMsgHandler.cs rename to src/Services/Catalog/Catalog.API/IntegrationCommands/CommandHandlers/ConfirmOrderStockCommandHandler.cs index beb23bbaf..7edfd5cf7 100644 --- a/src/Services/Catalog/Catalog.API/IntegrationCommands/CommandHandlers/ConfirmOrderStockCommandMsgHandler.cs +++ b/src/Services/Catalog/Catalog.API/IntegrationCommands/CommandHandlers/ConfirmOrderStockCommandHandler.cs @@ -12,23 +12,23 @@ using Commands; using IntegrationEvents.Events; - public class ConfirmOrderStockCommandMsgHandler : IIntegrationEventHandler + public class ConfirmOrderStockCommandHandler : IIntegrationEventHandler { private readonly CatalogContext _catalogContext; private readonly ICatalogIntegrationEventService _catalogIntegrationEventService; - public ConfirmOrderStockCommandMsgHandler(CatalogContext catalogContext, + public ConfirmOrderStockCommandHandler(CatalogContext catalogContext, ICatalogIntegrationEventService catalogIntegrationEventService) { _catalogContext = catalogContext; _catalogIntegrationEventService = catalogIntegrationEventService; } - public async Task Handle(ConfirmOrderStockCommandMsg @event) + public async Task Handle(ConfirmOrderStockCommand command) { var confirmedOrderStockItems = new List(); - foreach (var orderStockItem in @event.OrderStockItems) + foreach (var orderStockItem in command.OrderStockItems) { var catalogItem = _catalogContext.CatalogItems.Find(orderStockItem.ProductId); CheckValidcatalogItemId(catalogItem); @@ -39,9 +39,9 @@ confirmedOrderStockItems.Add(confirmedOrderStockItem); } - var confirmedIntegrationEvent = confirmedOrderStockItems.Any(c => !c.Confirmed) - ? (IntegrationEvent) new OrderStockNotConfirmedIntegrationEvent(@event.OrderId, confirmedOrderStockItems) - : new OrderStockConfirmedIntegrationEvent(@event.OrderId); + var confirmedIntegrationEvent = confirmedOrderStockItems.Any(c => !c.HasStock) + ? (IntegrationEvent) new OrderStockNotConfirmedIntegrationEvent(command.OrderId, confirmedOrderStockItems) + : new OrderStockConfirmedIntegrationEvent(command.OrderId); await _catalogIntegrationEventService.SaveEventAndCatalogContextChangesAsync(confirmedIntegrationEvent); await _catalogIntegrationEventService.PublishThroughEventBusAsync(confirmedIntegrationEvent); diff --git a/src/Services/Catalog/Catalog.API/IntegrationCommands/CommandHandlers/DecrementOrderStockCommandMsgHandler.cs b/src/Services/Catalog/Catalog.API/IntegrationCommands/CommandHandlers/DecrementOrderStockCommandHandler.cs similarity index 59% rename from src/Services/Catalog/Catalog.API/IntegrationCommands/CommandHandlers/DecrementOrderStockCommandMsgHandler.cs rename to src/Services/Catalog/Catalog.API/IntegrationCommands/CommandHandlers/DecrementOrderStockCommandHandler.cs index 9268aa0ba..1c7d1b9a3 100644 --- a/src/Services/Catalog/Catalog.API/IntegrationCommands/CommandHandlers/DecrementOrderStockCommandMsgHandler.cs +++ b/src/Services/Catalog/Catalog.API/IntegrationCommands/CommandHandlers/DecrementOrderStockCommandHandler.cs @@ -3,24 +3,21 @@ 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 + public class DecrementOrderStockCommandHandler : IIntegrationEventHandler { private readonly CatalogContext _catalogContext; - public DecrementOrderStockCommandMsgHandler(CatalogContext catalogContext) + public DecrementOrderStockCommandHandler(CatalogContext catalogContext) { _catalogContext = catalogContext; } - public async Task Handle(DecrementOrderStockCommandMsg @event) + public async Task Handle(DecrementOrderStockCommand command) { //we're not blocking stock/inventory - foreach (var orderStockItem in @event.OrderStockItems) + foreach (var orderStockItem in command.OrderStockItems) { var catalogItem = _catalogContext.CatalogItems.Find(orderStockItem.ProductId); diff --git a/src/Services/Catalog/Catalog.API/IntegrationCommands/Commands/ConfirmOrderStockCommandMsg.cs b/src/Services/Catalog/Catalog.API/IntegrationCommands/Commands/ConfirmOrderStockCommand.cs similarity index 85% rename from src/Services/Catalog/Catalog.API/IntegrationCommands/Commands/ConfirmOrderStockCommandMsg.cs rename to src/Services/Catalog/Catalog.API/IntegrationCommands/Commands/ConfirmOrderStockCommand.cs index f5fc805b2..2c5296776 100644 --- a/src/Services/Catalog/Catalog.API/IntegrationCommands/Commands/ConfirmOrderStockCommandMsg.cs +++ b/src/Services/Catalog/Catalog.API/IntegrationCommands/Commands/ConfirmOrderStockCommand.cs @@ -3,12 +3,12 @@ using BuildingBlocks.EventBus.Events; using System.Collections.Generic; - public class ConfirmOrderStockCommandMsg : IntegrationEvent + public class ConfirmOrderStockCommand : IntegrationEvent { public int OrderId { get; } public IEnumerable OrderStockItems { get; } - public ConfirmOrderStockCommandMsg(int orderId, + public ConfirmOrderStockCommand(int orderId, IEnumerable orderStockItems) { OrderId = orderId; diff --git a/src/Services/Catalog/Catalog.API/IntegrationCommands/Commands/DecrementOrderStockCommandMsg.cs b/src/Services/Catalog/Catalog.API/IntegrationCommands/Commands/DecrementOrderStockCommand.cs similarity index 79% rename from src/Services/Catalog/Catalog.API/IntegrationCommands/Commands/DecrementOrderStockCommandMsg.cs rename to src/Services/Catalog/Catalog.API/IntegrationCommands/Commands/DecrementOrderStockCommand.cs index 94bab1aa6..ca241d6fd 100644 --- a/src/Services/Catalog/Catalog.API/IntegrationCommands/Commands/DecrementOrderStockCommandMsg.cs +++ b/src/Services/Catalog/Catalog.API/IntegrationCommands/Commands/DecrementOrderStockCommand.cs @@ -3,12 +3,12 @@ using System.Collections.Generic; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - public class DecrementOrderStockCommandMsg : IntegrationEvent + public class DecrementOrderStockCommand : IntegrationEvent { public int OrderId { get; } public IEnumerable OrderStockItems { get; } - public DecrementOrderStockCommandMsg(int orderId, + public DecrementOrderStockCommand(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/OrderStockNotConfirmedIntegrationEvent.cs index 8186f8a13..b32b0dff5 100644 --- a/src/Services/Catalog/Catalog.API/IntegrationEvents/Events/OrderStockNotConfirmedIntegrationEvent.cs +++ b/src/Services/Catalog/Catalog.API/IntegrationEvents/Events/OrderStockNotConfirmedIntegrationEvent.cs @@ -7,25 +7,25 @@ { public int OrderId { get; } - public IEnumerable OrderStockItem { get; } + public List OrderStockItems { get; } - public OrderStockNotConfirmedIntegrationEvent(int orderId, - IEnumerable orderStockItem) + public OrderStockNotConfirmedIntegrationEvent(int orderId, + List orderStockItems) { OrderId = orderId; - OrderStockItem = orderStockItem; + OrderStockItems = orderStockItems; } } public class ConfirmedOrderStockItem { public int ProductId { get; } - public bool Confirmed { get; } + public bool HasStock { get; } - public ConfirmedOrderStockItem(int productId, bool confirmed) + public ConfirmedOrderStockItem(int productId, bool hasStock) { ProductId = productId; - Confirmed = confirmed; + HasStock = hasStock; } } } \ No newline at end of file diff --git a/src/Services/Catalog/Catalog.API/Startup.cs b/src/Services/Catalog/Catalog.API/Startup.cs index 22dd201d0..1f65e6a91 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,12 +185,24 @@ } } + private void RegisterServiceBus(IServiceCollection services) + { + services.AddSingleton(); + services.AddSingleton(); + + services.AddTransient, + ConfirmOrderStockCommandHandler>(); + services.AddTransient, + DecrementOrderStockCommandHandler>(); + + } + private void ConfigureEventBus(IApplicationBuilder app) { var eventBus = app.ApplicationServices.GetRequiredService(); - eventBus.Subscribe>(); - eventBus.Subscribe>(); + eventBus.Subscribe>(); + eventBus.Subscribe>(); } } } 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..9a74d6a90 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}"); @@ -37,10 +35,10 @@ var orderStockList = orderStatusChangedToAwaitingValidationDomainEvent.OrderItems .Select(orderItem => new OrderStockItem(orderItem.ProductId, orderItem.GetUnits())); - var confirmOrderStockEvent = new ConfirmOrderStockCommandMsg(orderStatusChangedToAwaitingValidationDomainEvent.OrderId, + var confirmOrderStockCommand = new ConfirmOrderStockCommand(orderStatusChangedToAwaitingValidationDomainEvent.OrderId, orderStockList); - await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(confirmOrderStockEvent); - await _orderingIntegrationEventService.PublishThroughEventBusAsync(confirmOrderStockEvent); + await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(confirmOrderStockCommand); + await _orderingIntegrationEventService.PublishThroughEventBusAsync(confirmOrderStockCommand); } } } \ 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 1eb4a8953..300d5d75f 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}"); @@ -37,15 +35,10 @@ var orderStockList = orderStatusChangedToPaidDomainEvent.OrderItems .Select(orderItem => new OrderStockItem(orderItem.ProductId, orderItem.GetUnits())); - var decrementOrderStockCommandMsg = new DecrementOrderStockCommandMsg(orderStatusChangedToPaidDomainEvent.OrderId, + var decrementOrderStockCommand = new DecrementOrderStockCommand(orderStatusChangedToPaidDomainEvent.OrderId, orderStockList); - await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(decrementOrderStockCommandMsg); - await _orderingIntegrationEventService.PublishThroughEventBusAsync(decrementOrderStockCommandMsg); - - //is it necessary get a DecrementOrderStockSuccessIntegrationEvent/DecrementOrderStockFailedIntegrationEvent before to call ShipOrderCommandMsg??? - var shipOrderCommandMsg = new ShipOrderCommandMsg(orderStatusChangedToPaidDomainEvent.OrderId); - await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(shipOrderCommandMsg); - await _orderingIntegrationEventService.PublishThroughEventBusAsync(shipOrderCommandMsg); + await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(decrementOrderStockCommand); + await _orderingIntegrationEventService.PublishThroughEventBusAsync(decrementOrderStockCommand); } } } \ 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 2de4f5095..272cbb061 100644 --- a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmed/OrderStatusChangedToStockConfirmedDomainEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderStockConfirmed/OrderStatusChangedToStockConfirmedDomainEventHandler.cs @@ -27,15 +27,13 @@ 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}"); - var payOrderCommandMsg = new PayOrderCommandMsg(orderStatusChangedToStockConfirmedDomainEvent.OrderId); - await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommandMsg); - await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommandMsg); + var payOrderCommand = new PayOrderCommand(orderStatusChangedToStockConfirmedDomainEvent.OrderId); + await _orderingIntegrationEventService.SaveEventAndOrderingContextChangesAsync(payOrderCommand); + await _orderingIntegrationEventService.PublishThroughEventBusAsync(payOrderCommand); } } } \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ConfirmGracePeriodCommandMsg.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ConfirmGracePeriodCommand.cs similarity index 63% rename from src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ConfirmGracePeriodCommandMsg.cs rename to src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ConfirmGracePeriodCommand.cs index f9d8b8923..33b509f16 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ConfirmGracePeriodCommandMsg.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ConfirmGracePeriodCommand.cs @@ -2,11 +2,11 @@ namespace Ordering.API.Application.IntegrationCommands.Commands { - public class ConfirmGracePeriodCommandMsg : IntegrationEvent + public class ConfirmGracePeriodCommand : IntegrationEvent { public int OrderId { get; } - public ConfirmGracePeriodCommandMsg(int orderId) => + public ConfirmGracePeriodCommand(int orderId) => OrderId = orderId; } } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ConfirmOrderStockCommandMsg.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ConfirmOrderStockCommand.cs similarity index 85% rename from src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ConfirmOrderStockCommandMsg.cs rename to src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ConfirmOrderStockCommand.cs index 6e78598f1..b3f73617c 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ConfirmOrderStockCommandMsg.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ConfirmOrderStockCommand.cs @@ -3,12 +3,12 @@ using System.Collections.Generic; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - public class ConfirmOrderStockCommandMsg : IntegrationEvent + public class ConfirmOrderStockCommand : IntegrationEvent { public int OrderId { get; } public IEnumerable OrderStockItems { get; } - public ConfirmOrderStockCommandMsg(int orderId, + public ConfirmOrderStockCommand(int orderId, IEnumerable orderStockItems) { OrderId = orderId; diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/DecrementOrderStockCommandMsg.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/DecrementOrderStockCommand.cs similarity index 78% rename from src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/DecrementOrderStockCommandMsg.cs rename to src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/DecrementOrderStockCommand.cs index e05a20c48..3d0457ee8 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/DecrementOrderStockCommandMsg.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/DecrementOrderStockCommand.cs @@ -3,12 +3,12 @@ using System.Collections.Generic; using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - public class DecrementOrderStockCommandMsg : IntegrationEvent + public class DecrementOrderStockCommand : IntegrationEvent { public int OrderId { get; } public IEnumerable OrderStockItems { get; } - public DecrementOrderStockCommandMsg(int orderId, + public DecrementOrderStockCommand(int orderId, IEnumerable orderStockItems) { OrderId = orderId; diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/PayOrderCommandMsg.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/PayOrderCommand.cs similarity index 69% rename from src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/PayOrderCommandMsg.cs rename to src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/PayOrderCommand.cs index 749ec882e..8a3ca665c 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/PayOrderCommandMsg.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/PayOrderCommand.cs @@ -2,11 +2,11 @@ { using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - public class PayOrderCommandMsg : IntegrationEvent + public class PayOrderCommand : IntegrationEvent { public int OrderId { get; } - public PayOrderCommandMsg(int orderId) + public PayOrderCommand(int orderId) { OrderId = orderId; } diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ShipOrderCommandMsg.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ShipOrderCommandMsg.cs deleted file mode 100644 index 5a8695ae9..000000000 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationCommands/Commands/ShipOrderCommandMsg.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace Ordering.API.Application.IntegrationCommands.Commands -{ - using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - - public class ShipOrderCommandMsg : IntegrationEvent - { - public int OrderId { get; } - - public ShipOrderCommandMsg(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 1e4e79978..3f81f8f67 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentFailedIntegrationEventHandler.cs @@ -17,9 +17,11 @@ public async Task Handle(OrderPaymentFailedIntegrationEvent @event) { - var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId); + var orderToUpdate = await _orderRepository.GetWithDependenciesAsync(@event.OrderId); orderToUpdate.SetCancelledStatus(); + + 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 f3075eeec..80144d0ed 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderPaymentSuccededIntegrationEventHandler.cs @@ -17,9 +17,11 @@ public async Task Handle(OrderPaymentSuccededIntegrationEvent @event) { - var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId); + var orderToUpdate = await _orderRepository.GetWithDependenciesAsync(@event.OrderId); orderToUpdate.SetPaidStatus(); + + 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 60c8bb65d..ac4bc5936 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs @@ -17,9 +17,11 @@ public async Task Handle(OrderStockConfirmedIntegrationEvent @event) { - var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId); + var orderToUpdate = await _orderRepository.GetWithDependenciesAsync(@event.OrderId); orderToUpdate.SetStockConfirmedStatus(); + + 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 68a89885c..49708fe92 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs @@ -19,13 +19,15 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling public async Task Handle(OrderStockNotConfirmedIntegrationEvent @event) { - var orderToUpdate = await _orderRepository.GetAsync(@event.OrderId); + var orderToUpdate = await _orderRepository.GetWithDependenciesAsync(@event.OrderId); var orderStockNotConfirmedItems = @event.OrderStockItems - .FindAll(c => !c.Confirmed) + .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/Events/OrderStockNotConfirmedIntegrationEvent.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStockNotConfirmedIntegrationEvent.cs index 5337c5eae..6bdb38ab2 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStockNotConfirmedIntegrationEvent.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStockNotConfirmedIntegrationEvent.cs @@ -21,12 +21,12 @@ namespace Ordering.API.Application.IntegrationEvents.Events public class ConfirmedOrderStockItem { public int ProductId { get; } - public bool Confirmed { get; } + public bool HasStock { get; } - public ConfirmedOrderStockItem(int productId, bool confirmed) + public ConfirmedOrderStockItem(int productId, bool hasStock) { ProductId = productId; - Confirmed = confirmed; + HasStock = hasStock; } } } \ 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 12e6a3fa2..a769c7e4c 100644 --- a/src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs +++ b/src/Services/Ordering/Ordering.API/Application/Sagas/OrderProcessSaga.cs @@ -21,7 +21,7 @@ namespace Ordering.API.Application.Sagas /// with the validations. /// public class OrderProcessSaga : OrderSaga, - IIntegrationEventHandler, + IIntegrationEventHandler, IAsyncRequestHandler, IAsyncRequestHandler { @@ -43,17 +43,13 @@ namespace Ordering.API.Application.Sagas /// period has completed. /// /// - public async Task Handle(ConfirmGracePeriodCommandMsg @event) + public async Task Handle(ConfirmGracePeriodCommand command) { - var orderSaga = FindSagaById(@event.OrderId); + var orderSaga = FindSagaById(command.OrderId); CheckValidSagaId(orderSaga); - if (orderSaga.OrderStatus != OrderStatus.Cancelled) - { - orderSaga.SetAwaitingValidationStatus(); - - await SaveChangesAsync(); - } + orderSaga.SetAwaitingValidationStatus(); + await SaveChangesAsync(); } /// @@ -68,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; } @@ -91,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/Application/Sagas/OrderSaga.cs b/src/Services/Ordering/Ordering.API/Application/Sagas/OrderSaga.cs index dd8e46ac7..babc66188 100644 --- a/src/Services/Ordering/Ordering.API/Application/Sagas/OrderSaga.cs +++ b/src/Services/Ordering/Ordering.API/Application/Sagas/OrderSaga.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; +using System.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate; @@ -20,10 +18,9 @@ namespace Ordering.API.Application.Sagas public override Order FindSagaById(int id) { var order = _orderingContext.Orders - .Single(c => c.Id == id); - - _orderingContext.Entry(order) - .Member("OrderStatus"); + .Include(c => c.OrderStatus) + .Include(c => c.OrderItems) + .Single(c => c.Id == id); return order; } diff --git a/src/Services/Ordering/Ordering.API/Startup.cs b/src/Services/Ordering/Ordering.API/Startup.cs index a6d20eb64..932984387 100644 --- a/src/Services/Ordering/Ordering.API/Startup.cs +++ b/src/Services/Ordering/Ordering.API/Startup.cs @@ -31,6 +31,7 @@ using System; using System.Data.Common; using System.Reflection; + using global::Ordering.API.Application.IntegrationEvents.EventHandling; public class Startup { @@ -124,6 +125,7 @@ return new DefaultRabbitMQPersistentConnection(factory, logger); }); + RegisterServiceBus(services); services.AddSingleton(); services.AddSingleton(); services.AddTransient(); @@ -170,10 +172,33 @@ } + 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(); + 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/IOrderRepository.cs b/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/IOrderRepository.cs index d7346ee4f..368410d62 100644 --- a/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/IOrderRepository.cs +++ b/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/IOrderRepository.cs @@ -13,5 +13,7 @@ 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 64bb2d21d..eaecba5f9 100644 --- a/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs +++ b/src/Services/Ordering/Ordering.Domain/AggregatesModel/OrderAggregate/Order.cs @@ -30,7 +30,7 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O // but only through the method OrderAggrergateRoot.AddOrderItem() which includes behaviour. private readonly List _orderItems; - public IEnumerable OrderItems => _orderItems.AsReadOnly(); + public IReadOnlyCollection OrderItems => _orderItems; // Using List<>.AsReadOnly() // This will create a read only wrapper around the private list so is protected against "external updates". // It's much cheaper than .ToList() because it will not have to copy all items in a new collection. (Just one heap alloc for the wrapper instance) @@ -95,21 +95,18 @@ 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(); } - _orderStatusId = OrderStatus.AwaitingValidation.Id; + AddDomainEvent(new OrderStatusChangedToAwaitingValidationDomainEvent(Id, _orderItems)); - AddDomainEvent(new OrderStatusChangedToAwaitingValidationDomainEvent(Id, OrderItems)); + _orderStatusId = OrderStatus.AwaitingValidation.Id; } public void SetStockConfirmedStatus(IEnumerable orderStockNotConfirmedItems = null) @@ -121,15 +118,14 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O if (orderStockNotConfirmedItems is null) { - OrderStatus = OrderStatus.StockConfirmed; + AddDomainEvent(new OrderStatusChangedToStockConfirmedDomainEvent(Id)); + _orderStatusId = OrderStatus.StockConfirmed.Id; _description = "All the items were confirmed with available stock."; - - AddDomainEvent(new OrderStatusChangedToStockConfirmedDomainEvent(Id)); } else { - OrderStatus = OrderStatus.Cancelled; + _orderStatusId = OrderStatus.Cancelled.Id; var itemsStockNotConfirmedProductNames = OrderItems .Where(c => orderStockNotConfirmedItems.Contains(c.ProductId)) @@ -147,10 +143,10 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O StatusChangeException(); } + AddDomainEvent(new OrderStatusChangedToPaidDomainEvent(Id, OrderItems)); + _orderStatusId = OrderStatus.Paid.Id; _description = "The payment was performed at a simulated \"American Bank checking bank account endinf on XX35071\""; - - AddDomainEvent(new OrderStatusChangedToPaidDomainEvent(Id, OrderItems)); } public void SetShippedStatus() @@ -162,15 +158,13 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.O _orderStatusId = OrderStatus.Shipped.Id; _description = ""; - - //Call Domain Event } public void SetCancelledStatus() { 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) { @@ -194,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/Ordering/Ordering.Infrastructure/Repositories/OrderRepository.cs b/src/Services/Ordering/Ordering.Infrastructure/Repositories/OrderRepository.cs index 84ef46dfb..df7b07aaa 100644 --- a/src/Services/Ordering/Ordering.Infrastructure/Repositories/OrderRepository.cs +++ b/src/Services/Ordering/Ordering.Infrastructure/Repositories/OrderRepository.cs @@ -36,6 +36,14 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Repositor return await _context.Orders.FindAsync(orderId); } + public async Task GetWithDependenciesAsync(int orderId) + { + return await _context.Orders + .Include(c => c.OrderStatus) + .Include(c => c.OrderItems) + .SingleAsync(c => c.Id == orderId); + } + public void Update(Order order) { _context.Entry(order).State = EntityState.Modified; diff --git a/src/Services/Payment/Payment.API/IntegrationCommands/CommandHandlers/PayOrderCommandMsgHandler.cs b/src/Services/Payment/Payment.API/IntegrationCommands/CommandHandlers/PayOrderCommandHandler.cs similarity index 78% rename from src/Services/Payment/Payment.API/IntegrationCommands/CommandHandlers/PayOrderCommandMsgHandler.cs rename to src/Services/Payment/Payment.API/IntegrationCommands/CommandHandlers/PayOrderCommandHandler.cs index c0023909c..ea1398cf8 100644 --- a/src/Services/Payment/Payment.API/IntegrationCommands/CommandHandlers/PayOrderCommandMsgHandler.cs +++ b/src/Services/Payment/Payment.API/IntegrationCommands/CommandHandlers/PayOrderCommandHandler.cs @@ -3,18 +3,17 @@ using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions; using Payment.API.IntegrationCommands.Commands; using System.Threading.Tasks; - using System; using Payment.API.IntegrationEvents; using Payment.API.IntegrationEvents.Events; - public class PayOrderCommandMsgHandler : IIntegrationEventHandler + public class PayOrderCommandHandler : IIntegrationEventHandler { private readonly IPaymentIntegrationEventService _paymentIntegrationEventService; - public PayOrderCommandMsgHandler(IPaymentIntegrationEventService paymentIntegrationEventService) + public PayOrderCommandHandler(IPaymentIntegrationEventService paymentIntegrationEventService) => _paymentIntegrationEventService = paymentIntegrationEventService; - public async Task Handle(PayOrderCommandMsg @event) + public async Task Handle(PayOrderCommand @event) { //PAYMENT SUCCESSED var orderPaymentSuccededIntegrationEvent = new OrderPaymentSuccededIntegrationEvent(@event.OrderId); @@ -25,4 +24,4 @@ //_paymentIntegrationEventService.PublishThroughEventBus(orderPaymentFailedIntegrationEvent); } } -} +} \ No newline at end of file diff --git a/src/Services/Payment/Payment.API/IntegrationCommands/Commands/PayOrderCommandMsg.cs b/src/Services/Payment/Payment.API/IntegrationCommands/Commands/PayOrderCommand.cs similarity index 58% rename from src/Services/Payment/Payment.API/IntegrationCommands/Commands/PayOrderCommandMsg.cs rename to src/Services/Payment/Payment.API/IntegrationCommands/Commands/PayOrderCommand.cs index 360f40606..d6476fa6e 100644 --- a/src/Services/Payment/Payment.API/IntegrationCommands/Commands/PayOrderCommandMsg.cs +++ b/src/Services/Payment/Payment.API/IntegrationCommands/Commands/PayOrderCommand.cs @@ -2,10 +2,10 @@ { using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; - public class PayOrderCommandMsg : IntegrationEvent + public class PayOrderCommand : IntegrationEvent { public int OrderId { get; } - public PayOrderCommandMsg(int orderId) => OrderId = orderId; + public PayOrderCommand(int orderId) => OrderId = orderId; } } \ No newline at end of file diff --git a/src/Services/Payment/Payment.API/Payment.API.csproj b/src/Services/Payment/Payment.API/Payment.API.csproj index 7f741fa53..de3636585 100644 --- a/src/Services/Payment/Payment.API/Payment.API.csproj +++ b/src/Services/Payment/Payment.API/Payment.API.csproj @@ -29,6 +29,9 @@ + + + diff --git a/src/Services/Payment/Payment.API/Startup.cs b/src/Services/Payment/Payment.API/Startup.cs index 7899d9aa6..e7a47f766 100644 --- a/src/Services/Payment/Payment.API/Startup.cs +++ b/src/Services/Payment/Payment.API/Startup.cs @@ -11,6 +11,8 @@ 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; namespace Payment.API { @@ -34,6 +36,7 @@ namespace Payment.API // Add framework services. services.AddMvc(); + services.AddTransient(); services.AddSingleton(sp => { var logger = sp.GetRequiredService>(); @@ -46,8 +49,7 @@ namespace Payment.API return new DefaultRabbitMQPersistentConnection(factory, logger); }); - services.AddSingleton(); - services.AddSingleton(); + RegisterServiceBus(services); services.AddSwaggerGen(); services.ConfigureSwaggerGen(options => @@ -81,10 +83,18 @@ namespace Payment.API ConfigureEventBus(app); } + private void RegisterServiceBus(IServiceCollection services) + { + services.AddSingleton(); + services.AddSingleton(); + + services.AddTransient, PayOrderCommandHandler>(); + } + private void ConfigureEventBus(IApplicationBuilder app) { var eventBus = app.ApplicationServices.GetRequiredService(); - eventBus.Subscribe>(); + eventBus.Subscribe>(); } } } diff --git a/src/Services/SagaManager/SagaManager/IntegrationEvents/Events/ConfirmGracePeriodCommand.cs b/src/Services/SagaManager/SagaManager/IntegrationEvents/Events/ConfirmGracePeriodCommand.cs new file mode 100644 index 000000000..fd507a265 --- /dev/null +++ b/src/Services/SagaManager/SagaManager/IntegrationEvents/Events/ConfirmGracePeriodCommand.cs @@ -0,0 +1,11 @@ +namespace SagaManager.IntegrationEvents.Events +{ + using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; + + public class ConfirmGracePeriodCommand : IntegrationEvent + { + public int OrderId { get;} + + public ConfirmGracePeriodCommand(int orderId) => OrderId = orderId; + } +} \ No newline at end of file diff --git a/src/Services/SagaManager/SagaManager/IntegrationEvents/Events/ConfirmGracePeriodCommandMsg.cs b/src/Services/SagaManager/SagaManager/IntegrationEvents/Events/ConfirmGracePeriodCommandMsg.cs deleted file mode 100644 index fc47f44e5..000000000 --- a/src/Services/SagaManager/SagaManager/IntegrationEvents/Events/ConfirmGracePeriodCommandMsg.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace SagaManager.IntegrationEvents.Events -{ - 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; - } -} 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..97784fc85 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(); @@ -44,8 +37,8 @@ namespace SagaManager while (true) { - sagaManagerService.CheckFinishedGracePeriodOrders(); - await Task.Delay(30000); + sagaManagerService.CheckConfirmedGracePeriodOrders(); + await Task.Delay(90000); } } @@ -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/ISagaManagerService.cs b/src/Services/SagaManager/SagaManager/Services/ISagaManagerService.cs index dc027b29c..6ee012c37 100644 --- a/src/Services/SagaManager/SagaManager/Services/ISagaManagerService.cs +++ b/src/Services/SagaManager/SagaManager/Services/ISagaManagerService.cs @@ -2,6 +2,6 @@ { public interface ISagaManagerService { - void CheckFinishedGracePeriodOrders(); + void CheckConfirmedGracePeriodOrders(); } } \ 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 b40c2fb08..f7455e27c 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; @@ -26,17 +23,18 @@ namespace SagaManager.Services _logger = logger; } - public void CheckFinishedGracePeriodOrders() + public void CheckConfirmedGracePeriodOrders() { - var orderIds = GetFinishedGracePeriodOrders(); + var orderIds = GetConfirmedGracePeriodOrders(); foreach (var orderId in orderIds) { - Publish(orderId); + var confirmGracePeriodEvent = new ConfirmGracePeriodCommand(orderId); + _sagaManagerIntegrationEventService.PublishThroughEventBus(confirmGracePeriodEvent); } } - private IEnumerable GetFinishedGracePeriodOrders() + private IEnumerable GetConfirmedGracePeriodOrders() { IEnumerable orderIds = new List(); using (var conn = new SqlConnection(_settings.ConnectionString)) @@ -60,13 +58,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