@ -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 < ConfirmedOrderStockItem > ( ) ;
//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" ) ;
}
}
}
}