From 629693043b7bd943e5c950b1ec8dee0a7356897d Mon Sep 17 00:00:00 2001 From: Christian Arenas Date: Tue, 9 May 2017 18:35:59 +0200 Subject: [PATCH] Add ConfirmOrderStockIntegrationEvent implementation --- ...onfirmOrderStockIntegrationEventHandler.cs | 33 +++++++++++++++++-- .../ConfirmOrderStockIntegrationEvent.cs | 6 ++-- .../OrderStockNotConfirmedIntegrationEvent.cs | 25 +++++++------- ...erStockConfirmedIntegrationEventHandler.cs | 3 ++ ...tockNotConfirmedIntegrationEventHandler.cs | 3 ++ .../OrderStockNotConfirmedIntegrationEvent.cs | 22 +++++++++++-- .../Ordering/Ordering.API/Ordering.API.csproj | 1 + 7 files changed, 70 insertions(+), 23 deletions(-) diff --git a/src/Services/Catalog/Catalog.API/IntegrationEvents/EventHandling/ConfirmOrderStockIntegrationEventHandler.cs b/src/Services/Catalog/Catalog.API/IntegrationEvents/EventHandling/ConfirmOrderStockIntegrationEventHandler.cs index 4bbec576d..062263a33 100644 --- a/src/Services/Catalog/Catalog.API/IntegrationEvents/EventHandling/ConfirmOrderStockIntegrationEventHandler.cs +++ b/src/Services/Catalog/Catalog.API/IntegrationEvents/EventHandling/ConfirmOrderStockIntegrationEventHandler.cs @@ -1,4 +1,8 @@ -using Catalog.API.IntegrationEvents; +using System.Collections.Generic; +using System.Linq; +using Catalog.API.Infrastructure.Exceptions; +using Catalog.API.IntegrationEvents; +using Microsoft.eShopOnContainers.Services.Catalog.API.Model; namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.EventHandling { @@ -23,11 +27,34 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.Eve public async Task Handle(ConfirmOrderStockIntegrationEvent @event) { - IntegrationEvent integrationEvent = new OrderStockConfirmedIntegrationEvent(@event.OrderId); + var confirmedOrderStockItems = new List(); - //TODO: Check the stock products units + foreach (var orderStockItem in @event.OrderStockItems) + { + var catalogItem = _catalogContext.CatalogItems.Find(orderStockItem.ProductId); + CheckValidcatalogItemId(catalogItem); + var confirmedOrderStockItem = new ConfirmedOrderStockItem(catalogItem.Id, + catalogItem.AvailableStock >= orderStockItem.Units); + + confirmedOrderStockItems.Add(confirmedOrderStockItem); + } + + //Create Integration Event to be published through the Event Bus + var integrationEvent = confirmedOrderStockItems.Any(c => !c.Confirmed) + ? (IntegrationEvent) new OrderStockNotConfirmedIntegrationEvent(@event.OrderId, confirmedOrderStockItems) + : new OrderStockConfirmedIntegrationEvent(@event.OrderId); + + // Publish through the Event Bus and mark the saved event as published await _catalogIntegrationEventService.PublishThroughEventBusAsync(integrationEvent); } + + 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/IntegrationEvents/Events/ConfirmOrderStockIntegrationEvent.cs b/src/Services/Catalog/Catalog.API/IntegrationEvents/Events/ConfirmOrderStockIntegrationEvent.cs index d8dc37163..366c8f854 100644 --- a/src/Services/Catalog/Catalog.API/IntegrationEvents/Events/ConfirmOrderStockIntegrationEvent.cs +++ b/src/Services/Catalog/Catalog.API/IntegrationEvents/Events/ConfirmOrderStockIntegrationEvent.cs @@ -6,13 +6,13 @@ public class ConfirmOrderStockIntegrationEvent : IntegrationEvent { public int OrderId { get; } - public IEnumerable OrderStockItem { get; } + public IEnumerable OrderStockItems { get; } public ConfirmOrderStockIntegrationEvent(int orderId, - IEnumerable orderStockItem) + IEnumerable orderStockItems) { OrderId = orderId; - OrderStockItem = orderStockItem; + OrderStockItems = orderStockItems; } } diff --git a/src/Services/Catalog/Catalog.API/IntegrationEvents/Events/OrderStockNotConfirmedIntegrationEvent.cs b/src/Services/Catalog/Catalog.API/IntegrationEvents/Events/OrderStockNotConfirmedIntegrationEvent.cs index 9af458022..8186f8a13 100644 --- a/src/Services/Catalog/Catalog.API/IntegrationEvents/Events/OrderStockNotConfirmedIntegrationEvent.cs +++ b/src/Services/Catalog/Catalog.API/IntegrationEvents/Events/OrderStockNotConfirmedIntegrationEvent.cs @@ -1,8 +1,7 @@ -using System.Collections.Generic; - -namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.Events +namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.Events { using BuildingBlocks.EventBus.Events; + using System.Collections.Generic; public class OrderStockNotConfirmedIntegrationEvent : IntegrationEvent { @@ -16,19 +15,17 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.Eve OrderId = orderId; OrderStockItem = orderStockItem; } + } - public class ConfirmedOrderStockItem - { - public int ProductId { get; } - public int Units { get; } - public bool Confirmed { get; } + public class ConfirmedOrderStockItem + { + public int ProductId { get; } + public bool Confirmed { get; } - public ConfirmedOrderStockItem(int productId, int units, bool confirmed) - { - ProductId = productId; - Units = units; - Confirmed = confirmed; - } + public ConfirmedOrderStockItem(int productId, bool confirmed) + { + ProductId = productId; + Confirmed = confirmed; } } } \ 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 4b52c25cd..819755e00 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs @@ -9,6 +9,9 @@ { public async Task Handle(OrderStockConfirmedIntegrationEvent @event) { + //TODO: 1) Updates the state to "StockValidated" and any meaningful OrderContextDescription message saying that all the items were confirmed with available stock, etc + //TODO: 2) Sends a Command-message (PayOrderCommand msg/bus) to the Payment svc. from Ordering micro (thru Command Bus, as a message, NOT http) + throw new NotImplementedException(); } } 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 b7c9e7ee8..c82cdd6eb 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs @@ -9,6 +9,9 @@ { public async Task Handle(OrderStockNotConfirmedIntegrationEvent @event) { + //TODO: must update the order state to cancelled and the CurrentOrderStateContextDescription with the reasons of no-stock confirm + //TODO: for this/that articles which is info coming in that integration event. --> ORDER PROCESS + throw new NotImplementedException(); } } 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 bd2055fef..13e3279bc 100644 --- a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStockNotConfirmedIntegrationEvent.cs +++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/OrderStockNotConfirmedIntegrationEvent.cs @@ -1,4 +1,6 @@ -namespace Ordering.API.Application.IntegrationEvents.Events +using System.Collections.Generic; + +namespace Ordering.API.Application.IntegrationEvents.Events { using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events; @@ -6,11 +8,25 @@ { public int OrderId { get; } - //public IEnumerable { get; } + public IEnumerable OrderStockItem { get; } - public OrderStockNotConfirmedIntegrationEvent(int orderId) + public OrderStockNotConfirmedIntegrationEvent(int orderId, + IEnumerable orderStockItem) { OrderId = orderId; + OrderStockItem = orderStockItem; + } + } + + public class ConfirmedOrderStockItem + { + public int ProductId { get; } + public bool Confirmed { get; } + + public ConfirmedOrderStockItem(int productId, bool confirmed) + { + ProductId = productId; + Confirmed = confirmed; } } } \ No newline at end of file diff --git a/src/Services/Ordering/Ordering.API/Ordering.API.csproj b/src/Services/Ordering/Ordering.API/Ordering.API.csproj index d5ef524bb..117551584 100644 --- a/src/Services/Ordering/Ordering.API/Ordering.API.csproj +++ b/src/Services/Ordering/Ordering.API/Ordering.API.csproj @@ -79,6 +79,7 @@ +