Skip to content

Commit 6296930

Browse files
committed
Add ConfirmOrderStockIntegrationEvent implementation
1 parent 83dadc8 commit 6296930

7 files changed

Lines changed: 70 additions & 23 deletions

File tree

src/Services/Catalog/Catalog.API/IntegrationEvents/EventHandling/ConfirmOrderStockIntegrationEventHandler.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
using Catalog.API.IntegrationEvents;
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Catalog.API.Infrastructure.Exceptions;
4+
using Catalog.API.IntegrationEvents;
5+
using Microsoft.eShopOnContainers.Services.Catalog.API.Model;
26

37
namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.EventHandling
48
{
@@ -23,11 +27,34 @@ public ConfirmOrderStockIntegrationEventHandler(CatalogContext catalogContext,
2327

2428
public async Task Handle(ConfirmOrderStockIntegrationEvent @event)
2529
{
26-
IntegrationEvent integrationEvent = new OrderStockConfirmedIntegrationEvent(@event.OrderId);
30+
var confirmedOrderStockItems = new List<ConfirmedOrderStockItem>();
2731

28-
//TODO: Check the stock products units
32+
foreach (var orderStockItem in @event.OrderStockItems)
33+
{
34+
var catalogItem = _catalogContext.CatalogItems.Find(orderStockItem.ProductId);
35+
CheckValidcatalogItemId(catalogItem);
2936

37+
var confirmedOrderStockItem = new ConfirmedOrderStockItem(catalogItem.Id,
38+
catalogItem.AvailableStock >= orderStockItem.Units);
39+
40+
confirmedOrderStockItems.Add(confirmedOrderStockItem);
41+
}
42+
43+
//Create Integration Event to be published through the Event Bus
44+
var integrationEvent = confirmedOrderStockItems.Any(c => !c.Confirmed)
45+
? (IntegrationEvent) new OrderStockNotConfirmedIntegrationEvent(@event.OrderId, confirmedOrderStockItems)
46+
: new OrderStockConfirmedIntegrationEvent(@event.OrderId);
47+
48+
// Publish through the Event Bus and mark the saved event as published
3049
await _catalogIntegrationEventService.PublishThroughEventBusAsync(integrationEvent);
3150
}
51+
52+
private void CheckValidcatalogItemId(CatalogItem catalogItem)
53+
{
54+
if (catalogItem is null)
55+
{
56+
throw new CatalogDomainException("Not able to process catalog event. Reason: no valid catalogItemId");
57+
}
58+
}
3259
}
3360
}

src/Services/Catalog/Catalog.API/IntegrationEvents/Events/ConfirmOrderStockIntegrationEvent.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
public class ConfirmOrderStockIntegrationEvent : IntegrationEvent
77
{
88
public int OrderId { get; }
9-
public IEnumerable<OrderStockItem> OrderStockItem { get; }
9+
public IEnumerable<OrderStockItem> OrderStockItems { get; }
1010

1111
public ConfirmOrderStockIntegrationEvent(int orderId,
12-
IEnumerable<OrderStockItem> orderStockItem)
12+
IEnumerable<OrderStockItem> orderStockItems)
1313
{
1414
OrderId = orderId;
15-
OrderStockItem = orderStockItem;
15+
OrderStockItems = orderStockItems;
1616
}
1717
}
1818

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
using System.Collections.Generic;
2-
3-
namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.Events
1+
namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.Events
42
{
53
using BuildingBlocks.EventBus.Events;
4+
using System.Collections.Generic;
65

76
public class OrderStockNotConfirmedIntegrationEvent : IntegrationEvent
87
{
@@ -16,19 +15,17 @@ public OrderStockNotConfirmedIntegrationEvent(int orderId,
1615
OrderId = orderId;
1716
OrderStockItem = orderStockItem;
1817
}
18+
}
1919

20-
public class ConfirmedOrderStockItem
21-
{
22-
public int ProductId { get; }
23-
public int Units { get; }
24-
public bool Confirmed { get; }
20+
public class ConfirmedOrderStockItem
21+
{
22+
public int ProductId { get; }
23+
public bool Confirmed { get; }
2524

26-
public ConfirmedOrderStockItem(int productId, int units, bool confirmed)
27-
{
28-
ProductId = productId;
29-
Units = units;
30-
Confirmed = confirmed;
31-
}
25+
public ConfirmedOrderStockItem(int productId, bool confirmed)
26+
{
27+
ProductId = productId;
28+
Confirmed = confirmed;
3229
}
3330
}
3431
}

src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockConfirmedIntegrationEventHandler.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ public class OrderStockConfirmedIntegrationEventHandler : IIntegrationEventHandl
99
{
1010
public async Task Handle(OrderStockConfirmedIntegrationEvent @event)
1111
{
12+
//TODO: 1) Updates the state to "StockValidated" and any meaningful OrderContextDescription message saying that all the items were confirmed with available stock, etc
13+
//TODO: 2) Sends a Command-message (PayOrderCommand msg/bus) to the Payment svc. from Ordering micro (thru Command Bus, as a message, NOT http)
14+
1215
throw new NotImplementedException();
1316
}
1417
}

src/Services/Ordering/Ordering.API/Application/IntegrationEvents/EventHandling/OrderStockNotConfirmedIntegrationEventHandler.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ public class OrderStockNotConfirmedIntegrationEventHandler : IIntegrationEventHa
99
{
1010
public async Task Handle(OrderStockNotConfirmedIntegrationEvent @event)
1111
{
12+
//TODO: must update the order state to cancelled and the CurrentOrderStateContextDescription with the reasons of no-stock confirm
13+
//TODO: for this/that articles which is info coming in that integration event. --> ORDER PROCESS
14+
1215
throw new NotImplementedException();
1316
}
1417
}
Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
1-
namespace Ordering.API.Application.IntegrationEvents.Events
1+
using System.Collections.Generic;
2+
3+
namespace Ordering.API.Application.IntegrationEvents.Events
24
{
35
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
46

57
public class OrderStockNotConfirmedIntegrationEvent : IntegrationEvent
68
{
79
public int OrderId { get; }
810

9-
//public IEnumerable<Item> { get; }
11+
public IEnumerable<ConfirmedOrderStockItem> OrderStockItem { get; }
1012

11-
public OrderStockNotConfirmedIntegrationEvent(int orderId)
13+
public OrderStockNotConfirmedIntegrationEvent(int orderId,
14+
IEnumerable<ConfirmedOrderStockItem> orderStockItem)
1215
{
1316
OrderId = orderId;
17+
OrderStockItem = orderStockItem;
18+
}
19+
}
20+
21+
public class ConfirmedOrderStockItem
22+
{
23+
public int ProductId { get; }
24+
public bool Confirmed { get; }
25+
26+
public ConfirmedOrderStockItem(int productId, bool confirmed)
27+
{
28+
ProductId = productId;
29+
Confirmed = confirmed;
1430
}
1531
}
1632
}

src/Services/Ordering/Ordering.API/Ordering.API.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
</ItemGroup>
8080

8181
<ItemGroup>
82+
<Folder Include="Application\DomainEventHandlers\OrderStockMethodVerified\" />
8283
<Folder Include="Application\IntegrationCommands\CommandHandlers\" />
8384
<Folder Include="Infrastructure\IntegrationEventMigrations\" />
8485
</ItemGroup>

0 commit comments

Comments
 (0)