- Change Integration Command to Integration Events
- Rename OrderStockNotConfirmedIntegrationEvent to OrderStockRejectedIntegrationEvent
This commit is contained in:
parent
55a82c24f4
commit
dc7b1098dd
@ -74,4 +74,9 @@
|
|||||||
</None>
|
</None>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Folder Include="IntegrationCommands\CommandHandlers\" />
|
||||||
|
<Folder Include="IntegrationCommands\Commands\" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationCommands.CommandHandlers
|
namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.EventHandling
|
||||||
{
|
{
|
||||||
using BuildingBlocks.EventBus.Abstractions;
|
using BuildingBlocks.EventBus.Abstractions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -6,53 +6,41 @@
|
|||||||
using Infrastructure;
|
using Infrastructure;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using global::Catalog.API.Infrastructure.Exceptions;
|
|
||||||
using global::Catalog.API.IntegrationEvents;
|
using global::Catalog.API.IntegrationEvents;
|
||||||
using Model;
|
|
||||||
using Commands;
|
|
||||||
using IntegrationEvents.Events;
|
using IntegrationEvents.Events;
|
||||||
|
|
||||||
public class ConfirmOrderStockCommandHandler : IIntegrationEventHandler<ConfirmOrderStockCommand>
|
public class OrderStatusChangedToAwaitingValidationIntegrationEventHandler :
|
||||||
|
IIntegrationEventHandler<OrderStatusChangedToAwaitingValidationIntegrationEvent>
|
||||||
{
|
{
|
||||||
private readonly CatalogContext _catalogContext;
|
private readonly CatalogContext _catalogContext;
|
||||||
private readonly ICatalogIntegrationEventService _catalogIntegrationEventService;
|
private readonly ICatalogIntegrationEventService _catalogIntegrationEventService;
|
||||||
|
|
||||||
public ConfirmOrderStockCommandHandler(CatalogContext catalogContext,
|
public OrderStatusChangedToAwaitingValidationIntegrationEventHandler(CatalogContext catalogContext,
|
||||||
ICatalogIntegrationEventService catalogIntegrationEventService)
|
ICatalogIntegrationEventService catalogIntegrationEventService)
|
||||||
{
|
{
|
||||||
_catalogContext = catalogContext;
|
_catalogContext = catalogContext;
|
||||||
_catalogIntegrationEventService = catalogIntegrationEventService;
|
_catalogIntegrationEventService = catalogIntegrationEventService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Handle(ConfirmOrderStockCommand command)
|
public async Task Handle(OrderStatusChangedToAwaitingValidationIntegrationEvent command)
|
||||||
{
|
{
|
||||||
var confirmedOrderStockItems = new List<ConfirmedOrderStockItem>();
|
var confirmedOrderStockItems = new List<ConfirmedOrderStockItem>();
|
||||||
|
|
||||||
foreach (var orderStockItem in command.OrderStockItems)
|
foreach (var orderStockItem in command.OrderStockItems)
|
||||||
{
|
{
|
||||||
var catalogItem = _catalogContext.CatalogItems.Find(orderStockItem.ProductId);
|
var catalogItem = _catalogContext.CatalogItems.Find(orderStockItem.ProductId);
|
||||||
CheckValidcatalogItemId(catalogItem);
|
var hasStock = catalogItem.AvailableStock >= orderStockItem.Units;
|
||||||
|
var confirmedOrderStockItem = new ConfirmedOrderStockItem(catalogItem.Id, hasStock);
|
||||||
var confirmedOrderStockItem = new ConfirmedOrderStockItem(catalogItem.Id,
|
|
||||||
catalogItem.AvailableStock >= orderStockItem.Units);
|
|
||||||
|
|
||||||
confirmedOrderStockItems.Add(confirmedOrderStockItem);
|
confirmedOrderStockItems.Add(confirmedOrderStockItem);
|
||||||
}
|
}
|
||||||
|
|
||||||
var confirmedIntegrationEvent = confirmedOrderStockItems.Any(c => !c.HasStock)
|
var confirmedIntegrationEvent = confirmedOrderStockItems.Any(c => !c.HasStock)
|
||||||
? (IntegrationEvent) new OrderStockNotConfirmedIntegrationEvent(command.OrderId, confirmedOrderStockItems)
|
? (IntegrationEvent) new OrderStockRejectedIntegrationEvent(command.OrderId, confirmedOrderStockItems)
|
||||||
: new OrderStockConfirmedIntegrationEvent(command.OrderId);
|
: new OrderStockConfirmedIntegrationEvent(command.OrderId);
|
||||||
|
|
||||||
await _catalogIntegrationEventService.SaveEventAndCatalogContextChangesAsync(confirmedIntegrationEvent);
|
await _catalogIntegrationEventService.SaveEventAndCatalogContextChangesAsync(confirmedIntegrationEvent);
|
||||||
await _catalogIntegrationEventService.PublishThroughEventBusAsync(confirmedIntegrationEvent);
|
await _catalogIntegrationEventService.PublishThroughEventBusAsync(confirmedIntegrationEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CheckValidcatalogItemId(CatalogItem catalogItem)
|
|
||||||
{
|
|
||||||
if (catalogItem is null)
|
|
||||||
{
|
|
||||||
throw new CatalogDomainException("Not able to process catalog event. Reason: no valid catalogItemId");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,20 +1,21 @@
|
|||||||
namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationCommands.CommandHandlers
|
namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.EventHandling
|
||||||
{
|
{
|
||||||
using BuildingBlocks.EventBus.Abstractions;
|
using BuildingBlocks.EventBus.Abstractions;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Infrastructure;
|
using Infrastructure;
|
||||||
using Commands;
|
using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.Events;
|
||||||
|
|
||||||
public class DecrementOrderStockCommandHandler : IIntegrationEventHandler<DecrementOrderStockCommand>
|
public class OrderStatusChangedToPaidIntegrationEventHandler :
|
||||||
|
IIntegrationEventHandler<OrderStatusChangedToPaidIntegrationEvent>
|
||||||
{
|
{
|
||||||
private readonly CatalogContext _catalogContext;
|
private readonly CatalogContext _catalogContext;
|
||||||
|
|
||||||
public DecrementOrderStockCommandHandler(CatalogContext catalogContext)
|
public OrderStatusChangedToPaidIntegrationEventHandler(CatalogContext catalogContext)
|
||||||
{
|
{
|
||||||
_catalogContext = catalogContext;
|
_catalogContext = catalogContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Handle(DecrementOrderStockCommand command)
|
public async Task Handle(OrderStatusChangedToPaidIntegrationEvent command)
|
||||||
{
|
{
|
||||||
//we're not blocking stock/inventory
|
//we're not blocking stock/inventory
|
||||||
foreach (var orderStockItem in command.OrderStockItems)
|
foreach (var orderStockItem in command.OrderStockItems)
|
@ -1,14 +1,14 @@
|
|||||||
namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationCommands.Commands
|
namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.Events
|
||||||
{
|
{
|
||||||
using BuildingBlocks.EventBus.Events;
|
using BuildingBlocks.EventBus.Events;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
public class ConfirmOrderStockCommand : IntegrationEvent
|
public class OrderStatusChangedToAwaitingValidationIntegrationEvent : IntegrationEvent
|
||||||
{
|
{
|
||||||
public int OrderId { get; }
|
public int OrderId { get; }
|
||||||
public IEnumerable<OrderStockItem> OrderStockItems { get; }
|
public IEnumerable<OrderStockItem> OrderStockItems { get; }
|
||||||
|
|
||||||
public ConfirmOrderStockCommand(int orderId,
|
public OrderStatusChangedToAwaitingValidationIntegrationEvent(int orderId,
|
||||||
IEnumerable<OrderStockItem> orderStockItems)
|
IEnumerable<OrderStockItem> orderStockItems)
|
||||||
{
|
{
|
||||||
OrderId = orderId;
|
OrderId = orderId;
|
@ -1,14 +1,14 @@
|
|||||||
namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationCommands.Commands
|
namespace Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.Events
|
||||||
{
|
{
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
||||||
|
|
||||||
public class DecrementOrderStockCommand : IntegrationEvent
|
public class OrderStatusChangedToPaidIntegrationEvent : IntegrationEvent
|
||||||
{
|
{
|
||||||
public int OrderId { get; }
|
public int OrderId { get; }
|
||||||
public IEnumerable<OrderStockItem> OrderStockItems { get; }
|
public IEnumerable<OrderStockItem> OrderStockItems { get; }
|
||||||
|
|
||||||
public DecrementOrderStockCommand(int orderId,
|
public OrderStatusChangedToPaidIntegrationEvent(int orderId,
|
||||||
IEnumerable<OrderStockItem> orderStockItems)
|
IEnumerable<OrderStockItem> orderStockItems)
|
||||||
{
|
{
|
||||||
OrderId = orderId;
|
OrderId = orderId;
|
@ -3,13 +3,13 @@
|
|||||||
using BuildingBlocks.EventBus.Events;
|
using BuildingBlocks.EventBus.Events;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
|
||||||
public class OrderStockNotConfirmedIntegrationEvent : IntegrationEvent
|
public class OrderStockRejectedIntegrationEvent : IntegrationEvent
|
||||||
{
|
{
|
||||||
public int OrderId { get; }
|
public int OrderId { get; }
|
||||||
|
|
||||||
public List<ConfirmedOrderStockItem> OrderStockItems { get; }
|
public List<ConfirmedOrderStockItem> OrderStockItems { get; }
|
||||||
|
|
||||||
public OrderStockNotConfirmedIntegrationEvent(int orderId,
|
public OrderStockRejectedIntegrationEvent(int orderId,
|
||||||
List<ConfirmedOrderStockItem> orderStockItems)
|
List<ConfirmedOrderStockItem> orderStockItems)
|
||||||
{
|
{
|
||||||
OrderId = orderId;
|
OrderId = orderId;
|
@ -14,9 +14,8 @@
|
|||||||
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF;
|
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF;
|
||||||
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services;
|
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services;
|
||||||
using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure;
|
using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure;
|
||||||
using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationCommands.Commands;
|
using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.EventHandling;
|
||||||
using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents;
|
using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.Events;
|
||||||
using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationCommands.CommandHandlers;
|
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.HealthChecks;
|
using Microsoft.Extensions.HealthChecks;
|
||||||
@ -27,7 +26,7 @@
|
|||||||
using System.Data.Common;
|
using System.Data.Common;
|
||||||
using System.Data.SqlClient;
|
using System.Data.SqlClient;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
|
||||||
public class Startup
|
public class Startup
|
||||||
{
|
{
|
||||||
public IConfigurationRoot Configuration { get; }
|
public IConfigurationRoot Configuration { get; }
|
||||||
@ -190,19 +189,20 @@
|
|||||||
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
|
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
|
||||||
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
|
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
|
||||||
|
|
||||||
services.AddTransient<IIntegrationEventHandler<ConfirmOrderStockCommand>,
|
services.AddTransient<IIntegrationEventHandler<OrderStatusChangedToAwaitingValidationIntegrationEvent>,
|
||||||
ConfirmOrderStockCommandHandler>();
|
OrderStatusChangedToAwaitingValidationIntegrationEventHandler>();
|
||||||
services.AddTransient<IIntegrationEventHandler<DecrementOrderStockCommand>,
|
services.AddTransient<IIntegrationEventHandler<OrderStatusChangedToPaidIntegrationEvent>,
|
||||||
DecrementOrderStockCommandHandler>();
|
OrderStatusChangedToPaidIntegrationEventHandler>();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ConfigureEventBus(IApplicationBuilder app)
|
private void ConfigureEventBus(IApplicationBuilder app)
|
||||||
{
|
{
|
||||||
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
|
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
|
||||||
|
|
||||||
eventBus.Subscribe<ConfirmOrderStockCommand, IIntegrationEventHandler<ConfirmOrderStockCommand>>();
|
eventBus.Subscribe<OrderStatusChangedToAwaitingValidationIntegrationEvent,
|
||||||
eventBus.Subscribe<DecrementOrderStockCommand, IIntegrationEventHandler<DecrementOrderStockCommand>>();
|
IIntegrationEventHandler<OrderStatusChangedToAwaitingValidationIntegrationEvent>>();
|
||||||
|
eventBus.Subscribe<OrderStatusChangedToPaidIntegrationEvent,
|
||||||
|
IIntegrationEventHandler<OrderStatusChangedToPaidIntegrationEvent>>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user