From b4b9e6c8d613560c16813a59e09a71ee7456774c Mon Sep 17 00:00:00 2001
From: Philipp Theyssen
Date: Sat, 22 Apr 2023 21:06:47 +0200
Subject: [PATCH] Make all catalog product events have same name id. (#10)
* Make all catalog product events have same name id.
This should be useful for stream processing in flink,
because we can then key a stream with these events on the productId
more easily.
* Add product bought event, which is published for each product.
This makes it easier to check whether a product is being oversold.
* Allow for setting the catalog item id through API call.
Previously the entity framework API did have a counter in place,
ignoring the id that was send in the catalog api request.
---
.../Catalog.API/Controllers/CatalogController.cs | 4 +++-
.../CatalogItemEntityTypeConfiguration.cs | 1 -
.../Events/ProductCreatedIntegrationEvent.cs | 2 +-
.../OrderStatusChangedToPaidDomainEventHandler.cs | 9 +++++++++
.../Events/ProductBoughtIntegrationEvent.cs | 13 +++++++++++++
5 files changed, 26 insertions(+), 3 deletions(-)
create mode 100644 src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/ProductBoughtIntegrationEvent.cs
diff --git a/src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs b/src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs
index 9c4c39fd6..19ab13f04 100644
--- a/src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs
+++ b/src/Services/Catalog/Catalog.API/Controllers/CatalogController.cs
@@ -264,11 +264,13 @@ public class CatalogController : ControllerBase
{
var item = new CatalogItem
{
+ Id = product.Id,
CatalogBrandId = product.CatalogBrandId,
CatalogTypeId = product.CatalogTypeId,
Description = product.Description,
Name = product.Name,
PictureFileName = product.PictureFileName,
+ AvailableStock = product.AvailableStock,
Price = product.Price
};
@@ -285,7 +287,7 @@ public class CatalogController : ControllerBase
CatalogType = item.CatalogType,
CatalogTypeId = item.CatalogTypeId,
Description = item.Description,
- Id = item.Id,
+ ProductId = item.Id,
MaxStockThreshold = item.MaxStockThreshold,
Name = item.Name,
OnReorder = item.OnReorder
diff --git a/src/Services/Catalog/Catalog.API/Infrastructure/EntityConfigurations/CatalogItemEntityTypeConfiguration.cs b/src/Services/Catalog/Catalog.API/Infrastructure/EntityConfigurations/CatalogItemEntityTypeConfiguration.cs
index d7cc8b2d5..4274b8d77 100644
--- a/src/Services/Catalog/Catalog.API/Infrastructure/EntityConfigurations/CatalogItemEntityTypeConfiguration.cs
+++ b/src/Services/Catalog/Catalog.API/Infrastructure/EntityConfigurations/CatalogItemEntityTypeConfiguration.cs
@@ -8,7 +8,6 @@ class CatalogItemEntityTypeConfiguration
builder.ToTable("Catalog");
builder.Property(ci => ci.Id)
- .UseHiLo("catalog_hilo")
.IsRequired();
builder.Property(ci => ci.Name)
diff --git a/src/Services/Catalog/Catalog.API/IntegrationEvents/Events/ProductCreatedIntegrationEvent.cs b/src/Services/Catalog/Catalog.API/IntegrationEvents/Events/ProductCreatedIntegrationEvent.cs
index a670da604..5bc724e6d 100644
--- a/src/Services/Catalog/Catalog.API/IntegrationEvents/Events/ProductCreatedIntegrationEvent.cs
+++ b/src/Services/Catalog/Catalog.API/IntegrationEvents/Events/ProductCreatedIntegrationEvent.cs
@@ -2,7 +2,7 @@ namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.Eve
public record ProductCreatedIntegrationEvent : IntegrationEvent
{
- public int Id { get; set; }
+ public int ProductId { get; set; }
public string Name { get; set; }
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 7f0138d71..16e64d2a6 100644
--- a/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderPaid/OrderStatusChangedToPaidDomainEventHandler.cs
+++ b/src/Services/Ordering/Ordering.API/Application/DomainEventHandlers/OrderPaid/OrderStatusChangedToPaidDomainEventHandler.cs
@@ -39,6 +39,15 @@ public class OrderStatusChangedToPaidDomainEventHandler
buyer.Name,
orderStockList);
+ foreach (var orderStockItem in orderStockList)
+ {
+ var productBoughtEvent = new ProductBoughtIntegrationEvent(
+ orderStockItem.ProductId,
+ orderStockItem.Units
+ );
+ await _orderingIntegrationEventService.AddAndSaveEventAsync(productBoughtEvent);
+ }
+
await _orderingIntegrationEventService.AddAndSaveEventAsync(orderStatusChangedToPaidIntegrationEvent);
}
}
diff --git a/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/ProductBoughtIntegrationEvent.cs b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/ProductBoughtIntegrationEvent.cs
new file mode 100644
index 000000000..6b7e0fd71
--- /dev/null
+++ b/src/Services/Ordering/Ordering.API/Application/IntegrationEvents/Events/ProductBoughtIntegrationEvent.cs
@@ -0,0 +1,13 @@
+namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.IntegrationEvents.Events;
+
+public record ProductBoughtIntegrationEvent : IntegrationEvent
+{
+ public int ProductId { get; }
+ public int Units { get; }
+
+ public ProductBoughtIntegrationEvent(int productId, int units)
+ {
+ ProductId = productId;
+ Units = units;
+ }
+}
\ No newline at end of file