Skip to content

Commit ea5c65b

Browse files
committed
Add OrderStockConfirmed and ProductPriceChanged IntegrationEvent
1 parent b05f019 commit ea5c65b

7 files changed

Lines changed: 90 additions & 21 deletions

File tree

src/Services/Catalog/Catalog.API/IntegrationEvents/EventHandling/ConfirmOrderStockIntegrationEventHandler.cs renamed to src/Services/Catalog/Catalog.API/IntegrationCommands/CommandHandlers/ConfirmOrderStockCommandMsgHandler.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.EventHandling
1+
namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationCommands.CommandHandlers
22
{
33
using BuildingBlocks.EventBus.Abstractions;
44
using System.Threading.Tasks;
@@ -9,21 +9,22 @@
99
using global::Catalog.API.Infrastructure.Exceptions;
1010
using global::Catalog.API.IntegrationEvents;
1111
using Model;
12-
using Events;
12+
using Commands;
13+
using IntegrationEvents.Events;
1314

14-
public class ConfirmOrderStockIntegrationEventHandler : IIntegrationEventHandler<ConfirmOrderStockIntegrationEvent>
15+
public class ConfirmOrderStockCommandMsgHandler : IIntegrationEventHandler<ConfirmOrderStockCommandMsg>
1516
{
1617
private readonly CatalogContext _catalogContext;
1718
private readonly ICatalogIntegrationEventService _catalogIntegrationEventService;
1819

19-
public ConfirmOrderStockIntegrationEventHandler(CatalogContext catalogContext,
20+
public ConfirmOrderStockCommandMsgHandler(CatalogContext catalogContext,
2021
ICatalogIntegrationEventService catalogIntegrationEventService)
2122
{
2223
_catalogContext = catalogContext;
2324
_catalogIntegrationEventService = catalogIntegrationEventService;
2425
}
2526

26-
public async Task Handle(ConfirmOrderStockIntegrationEvent @event)
27+
public async Task Handle(ConfirmOrderStockCommandMsg @event)
2728
{
2829
var confirmedOrderStockItems = new List<ConfirmedOrderStockItem>();
2930

@@ -38,15 +39,11 @@ public async Task Handle(ConfirmOrderStockIntegrationEvent @event)
3839
confirmedOrderStockItems.Add(confirmedOrderStockItem);
3940
}
4041

41-
//Create Integration Event to be published through the Event Bus
4242
var confirmedIntegrationEvent = confirmedOrderStockItems.Any(c => !c.Confirmed)
4343
? (IntegrationEvent) new OrderStockNotConfirmedIntegrationEvent(@event.OrderId, confirmedOrderStockItems)
4444
: new OrderStockConfirmedIntegrationEvent(@event.OrderId);
4545

46-
// Achieving atomicity between original Catalog database operation and the IntegrationEventLog thanks to a local transaction
4746
await _catalogIntegrationEventService.SaveEventAndCatalogContextChangesAsync(confirmedIntegrationEvent);
48-
49-
// Publish through the Event Bus and mark the saved event as published
5047
await _catalogIntegrationEventService.PublishThroughEventBusAsync(confirmedIntegrationEvent);
5148
}
5249

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationCommands.CommandHandlers
2+
{
3+
using BuildingBlocks.EventBus.Abstractions;
4+
using System.Threading.Tasks;
5+
using Infrastructure;
6+
using global::Catalog.API.Infrastructure.Exceptions;
7+
using global::Catalog.API.IntegrationEvents;
8+
using Model;
9+
using Commands;
10+
11+
public class DecrementOrderStockCommandMsgHandler : IIntegrationEventHandler<DecrementOrderStockCommandMsg>
12+
{
13+
private readonly CatalogContext _catalogContext;
14+
15+
public DecrementOrderStockCommandMsgHandler(CatalogContext catalogContext)
16+
{
17+
_catalogContext = catalogContext;
18+
}
19+
20+
public async Task Handle(DecrementOrderStockCommandMsg @event)
21+
{
22+
//we're not blocking stock/inventory
23+
foreach (var orderStockItem in @event.OrderStockItems)
24+
{
25+
var catalogItem = _catalogContext.CatalogItems.Find(orderStockItem.ProductId);
26+
CheckValidcatalogItemId(catalogItem);
27+
28+
catalogItem.RemoveStock(orderStockItem.Units);
29+
}
30+
31+
await _catalogContext.SaveChangesAsync();
32+
}
33+
34+
private void CheckValidcatalogItemId(CatalogItem catalogItem)
35+
{
36+
if (catalogItem is null)
37+
{
38+
throw new CatalogDomainException("Not able to process catalog event. Reason: no valid catalogItemId");
39+
}
40+
}
41+
}
42+
}

src/Services/Catalog/Catalog.API/IntegrationEvents/Events/ConfirmOrderStockIntegrationEvent.cs renamed to src/Services/Catalog/Catalog.API/IntegrationCommands/Commands/ConfirmOrderStockCommandMsg.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.Events
1+
namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationCommands.Commands
22
{
33
using BuildingBlocks.EventBus.Events;
44
using System.Collections.Generic;
55

6-
public class ConfirmOrderStockIntegrationEvent : IntegrationEvent
6+
public class ConfirmOrderStockCommandMsg : IntegrationEvent
77
{
88
public int OrderId { get; }
99
public IEnumerable<OrderStockItem> OrderStockItems { get; }
1010

11-
public ConfirmOrderStockIntegrationEvent(int orderId,
11+
public ConfirmOrderStockCommandMsg(int orderId,
1212
IEnumerable<OrderStockItem> orderStockItems)
1313
{
1414
OrderId = orderId;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationCommands.Commands
2+
{
3+
using System.Collections.Generic;
4+
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
5+
6+
public class DecrementOrderStockCommandMsg : IntegrationEvent
7+
{
8+
public int OrderId { get; }
9+
public IEnumerable<OrderStockItem> OrderStockItems { get; }
10+
11+
public DecrementOrderStockCommandMsg(int orderId,
12+
IEnumerable<OrderStockItem> orderStockItems)
13+
{
14+
OrderId = orderId;
15+
OrderStockItems = orderStockItems;
16+
}
17+
}
18+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.Events
22
{
3-
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
3+
using BuildingBlocks.EventBus.Events;
44

55
public class OrderStockConfirmedIntegrationEvent : IntegrationEvent
66
{

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
2-
using System;
3-
using System.Collections.Generic;
4-
using System.Text;
5-
6-
namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.Events
1+
namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.Events
72
{
3+
using BuildingBlocks.EventBus.Events;
4+
85
// Integration Events notes:
96
// An Event is “something that has happened in the past”, therefore its name has to be
107
// An Integration Event is an event that can cause side effects to other microsrvices, Bounded-Contexts or external systems.
@@ -23,4 +20,4 @@ public ProductPriceChangedIntegrationEvent(int productId, decimal newPrice, deci
2320
OldPrice = oldPrice;
2421
}
2522
}
26-
}
23+
}

src/Services/Catalog/Catalog.API/Startup.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF;
1515
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services;
1616
using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure;
17+
using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationCommands.Commands;
18+
using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents;
19+
using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationCommands.CommandsHandlers;
1720
using Microsoft.Extensions.Configuration;
1821
using Microsoft.Extensions.DependencyInjection;
1922
using Microsoft.Extensions.HealthChecks;
@@ -24,7 +27,7 @@
2427
using System.Data.Common;
2528
using System.Data.SqlClient;
2629
using System.Reflection;
27-
30+
2831
public class Startup
2932
{
3033
public IConfigurationRoot Configuration { get; }
@@ -120,6 +123,8 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
120123

121124
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
122125
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
126+
services.AddTransient<IIntegrationEventHandler<ConfirmOrderStockCommandMsg>, ConfirmOrderStockCommandMsgHandler>();
127+
services.AddTransient<IIntegrationEventHandler<DecrementOrderStockCommandMsg>, DecrementOrderStockCommandMsgHandler>();
123128

124129
var container = new ContainerBuilder();
125130
container.Populate(services);
@@ -149,6 +154,8 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
149154
CatalogContextSeed.SeedAsync(app, loggerFactory)
150155
.Wait();
151156

157+
ConfigureEventBus(app);
158+
152159
var integrationEventLogContext = new IntegrationEventLogContext(
153160
new DbContextOptionsBuilder<IntegrationEventLogContext>()
154161
.UseSqlServer(Configuration["ConnectionString"], b => b.MigrationsAssembly("Catalog.API"))
@@ -180,5 +187,13 @@ private void WaitForSqlAvailability(CatalogContext ctx, ILoggerFactory loggerFac
180187
ctx.Database.CloseConnection();
181188
}
182189
}
190+
191+
private void ConfigureEventBus(IApplicationBuilder app)
192+
{
193+
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
194+
195+
eventBus.Subscribe<ConfirmOrderStockCommandMsg, IIntegrationEventHandler<ConfirmOrderStockCommandMsg>>();
196+
eventBus.Subscribe<DecrementOrderStockCommandMsg, IIntegrationEventHandler<DecrementOrderStockCommandMsg>>();
197+
}
183198
}
184199
}

0 commit comments

Comments
 (0)