Browse Source

- Change Integration Command to Integration Events

- Rename OrderStockNotConfirmedIntegrationEvent to OrderStockRejectedIntegrationEvent
pull/809/head
Christian Arenas 7 years ago
parent
commit
dc7b1098dd
7 changed files with 38 additions and 44 deletions
  1. +5
    -0
      src/Services/Catalog/Catalog.API/Catalog.API.csproj
  2. +8
    -20
      src/Services/Catalog/Catalog.API/IntegrationEvents/EventHandling/OrderStatusChangedToAwaitingValidationIntegrationEventHandler.cs
  3. +6
    -5
      src/Services/Catalog/Catalog.API/IntegrationEvents/EventHandling/OrderStatusChangedToPaidIntegrationEventHandler.cs
  4. +3
    -3
      src/Services/Catalog/Catalog.API/IntegrationEvents/Events/OrderStatusChangedToAwaitingValidationIntegrationEvent.cs
  5. +3
    -3
      src/Services/Catalog/Catalog.API/IntegrationEvents/Events/OrderStatusChangedToPaidIntegrationEvent.cs
  6. +2
    -2
      src/Services/Catalog/Catalog.API/IntegrationEvents/Events/OrderStockRejectedIntegrationEvent.cs
  7. +11
    -11
      src/Services/Catalog/Catalog.API/Startup.cs

+ 5
- 0
src/Services/Catalog/Catalog.API/Catalog.API.csproj View File

@ -74,4 +74,9 @@
</None>
</ItemGroup>
<ItemGroup>
<Folder Include="IntegrationCommands\CommandHandlers\" />
<Folder Include="IntegrationCommands\Commands\" />
</ItemGroup>
</Project>

src/Services/Catalog/Catalog.API/IntegrationCommands/CommandHandlers/ConfirmOrderStockCommandHandler.cs → src/Services/Catalog/Catalog.API/IntegrationEvents/EventHandling/OrderStatusChangedToAwaitingValidationIntegrationEventHandler.cs View File

@ -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 System.Threading.Tasks;
@ -6,53 +6,41 @@
using Infrastructure;
using System.Collections.Generic;
using System.Linq;
using global::Catalog.API.Infrastructure.Exceptions;
using global::Catalog.API.IntegrationEvents;
using Model;
using Commands;
using IntegrationEvents.Events;
public class ConfirmOrderStockCommandHandler : IIntegrationEventHandler<ConfirmOrderStockCommand>
public class OrderStatusChangedToAwaitingValidationIntegrationEventHandler :
IIntegrationEventHandler<OrderStatusChangedToAwaitingValidationIntegrationEvent>
{
private readonly CatalogContext _catalogContext;
private readonly ICatalogIntegrationEventService _catalogIntegrationEventService;
public ConfirmOrderStockCommandHandler(CatalogContext catalogContext,
public OrderStatusChangedToAwaitingValidationIntegrationEventHandler(CatalogContext catalogContext,
ICatalogIntegrationEventService catalogIntegrationEventService)
{
_catalogContext = catalogContext;
_catalogIntegrationEventService = catalogIntegrationEventService;
}
public async Task Handle(ConfirmOrderStockCommand command)
public async Task Handle(OrderStatusChangedToAwaitingValidationIntegrationEvent command)
{
var confirmedOrderStockItems = new List<ConfirmedOrderStockItem>();
foreach (var orderStockItem in command.OrderStockItems)
{
var catalogItem = _catalogContext.CatalogItems.Find(orderStockItem.ProductId);
CheckValidcatalogItemId(catalogItem);
var confirmedOrderStockItem = new ConfirmedOrderStockItem(catalogItem.Id,
catalogItem.AvailableStock >= orderStockItem.Units);
var hasStock = catalogItem.AvailableStock >= orderStockItem.Units;
var confirmedOrderStockItem = new ConfirmedOrderStockItem(catalogItem.Id, hasStock);
confirmedOrderStockItems.Add(confirmedOrderStockItem);
}
var confirmedIntegrationEvent = confirmedOrderStockItems.Any(c => !c.HasStock)
? (IntegrationEvent) new OrderStockNotConfirmedIntegrationEvent(command.OrderId, confirmedOrderStockItems)
? (IntegrationEvent) new OrderStockRejectedIntegrationEvent(command.OrderId, confirmedOrderStockItems)
: new OrderStockConfirmedIntegrationEvent(command.OrderId);
await _catalogIntegrationEventService.SaveEventAndCatalogContextChangesAsync(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");
}
}
}
}

src/Services/Catalog/Catalog.API/IntegrationCommands/CommandHandlers/DecrementOrderStockCommandHandler.cs → src/Services/Catalog/Catalog.API/IntegrationEvents/EventHandling/OrderStatusChangedToPaidIntegrationEventHandler.cs View File

@ -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 System.Threading.Tasks;
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;
public DecrementOrderStockCommandHandler(CatalogContext catalogContext)
public OrderStatusChangedToPaidIntegrationEventHandler(CatalogContext catalogContext)
{
_catalogContext = catalogContext;
}
public async Task Handle(DecrementOrderStockCommand command)
public async Task Handle(OrderStatusChangedToPaidIntegrationEvent command)
{
//we're not blocking stock/inventory
foreach (var orderStockItem in command.OrderStockItems)

src/Services/Catalog/Catalog.API/IntegrationCommands/Commands/ConfirmOrderStockCommand.cs → src/Services/Catalog/Catalog.API/IntegrationEvents/Events/OrderStatusChangedToAwaitingValidationIntegrationEvent.cs View File

@ -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 System.Collections.Generic;
public class ConfirmOrderStockCommand : IntegrationEvent
public class OrderStatusChangedToAwaitingValidationIntegrationEvent : IntegrationEvent
{
public int OrderId { get; }
public IEnumerable<OrderStockItem> OrderStockItems { get; }
public ConfirmOrderStockCommand(int orderId,
public OrderStatusChangedToAwaitingValidationIntegrationEvent(int orderId,
IEnumerable<OrderStockItem> orderStockItems)
{
OrderId = orderId;

src/Services/Catalog/Catalog.API/IntegrationCommands/Commands/DecrementOrderStockCommand.cs → src/Services/Catalog/Catalog.API/IntegrationEvents/Events/OrderStatusChangedToPaidIntegrationEvent.cs View File

@ -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 Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
public class DecrementOrderStockCommand : IntegrationEvent
public class OrderStatusChangedToPaidIntegrationEvent : IntegrationEvent
{
public int OrderId { get; }
public IEnumerable<OrderStockItem> OrderStockItems { get; }
public DecrementOrderStockCommand(int orderId,
public OrderStatusChangedToPaidIntegrationEvent(int orderId,
IEnumerable<OrderStockItem> orderStockItems)
{
OrderId = orderId;

src/Services/Catalog/Catalog.API/IntegrationEvents/Events/OrderStockNotConfirmedIntegrationEvent.cs → src/Services/Catalog/Catalog.API/IntegrationEvents/Events/OrderStockRejectedIntegrationEvent.cs View File

@ -3,13 +3,13 @@
using BuildingBlocks.EventBus.Events;
using System.Collections.Generic;
public class OrderStockNotConfirmedIntegrationEvent : IntegrationEvent
public class OrderStockRejectedIntegrationEvent : IntegrationEvent
{
public int OrderId { get; }
public List<ConfirmedOrderStockItem> OrderStockItems { get; }
public OrderStockNotConfirmedIntegrationEvent(int orderId,
public OrderStockRejectedIntegrationEvent(int orderId,
List<ConfirmedOrderStockItem> orderStockItems)
{
OrderId = orderId;

+ 11
- 11
src/Services/Catalog/Catalog.API/Startup.cs View File

@ -14,9 +14,8 @@
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF;
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Services;
using Microsoft.eShopOnContainers.Services.Catalog.API.Infrastructure;
using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationCommands.Commands;
using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents;
using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationCommands.CommandHandlers;
using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.EventHandling;
using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.Events;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.HealthChecks;
@ -27,7 +26,7 @@
using System.Data.Common;
using System.Data.SqlClient;
using System.Reflection;
public class Startup
{
public IConfigurationRoot Configuration { get; }
@ -190,19 +189,20 @@
services.AddSingleton<IEventBusSubscriptionsManager, InMemoryEventBusSubscriptionsManager>();
services.AddSingleton<IEventBus, EventBusRabbitMQ>();
services.AddTransient<IIntegrationEventHandler<ConfirmOrderStockCommand>,
ConfirmOrderStockCommandHandler>();
services.AddTransient<IIntegrationEventHandler<DecrementOrderStockCommand>,
DecrementOrderStockCommandHandler>();
services.AddTransient<IIntegrationEventHandler<OrderStatusChangedToAwaitingValidationIntegrationEvent>,
OrderStatusChangedToAwaitingValidationIntegrationEventHandler>();
services.AddTransient<IIntegrationEventHandler<OrderStatusChangedToPaidIntegrationEvent>,
OrderStatusChangedToPaidIntegrationEventHandler>();
}
private void ConfigureEventBus(IApplicationBuilder app)
{
var eventBus = app.ApplicationServices.GetRequiredService<IEventBus>();
eventBus.Subscribe<ConfirmOrderStockCommand, IIntegrationEventHandler<ConfirmOrderStockCommand>>();
eventBus.Subscribe<DecrementOrderStockCommand, IIntegrationEventHandler<DecrementOrderStockCommand>>();
eventBus.Subscribe<OrderStatusChangedToAwaitingValidationIntegrationEvent,
IIntegrationEventHandler<OrderStatusChangedToAwaitingValidationIntegrationEvent>>();
eventBus.Subscribe<OrderStatusChangedToPaidIntegrationEvent,
IIntegrationEventHandler<OrderStatusChangedToPaidIntegrationEvent>>();
}
}
}

Loading…
Cancel
Save