Add LogContext to IntegrationEventHandlers
This commit is contained in:
parent
6ccd7b6096
commit
122fab5108
@ -78,20 +78,17 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.Controllers
|
|||||||
// Once basket is checkout, sends an integration event to
|
// Once basket is checkout, sends an integration event to
|
||||||
// ordering.api to convert basket to order and proceeds with
|
// ordering.api to convert basket to order and proceeds with
|
||||||
// order creation process
|
// order creation process
|
||||||
using (LogContext.PushProperty("IntegrationEventId", eventMessage.Id))
|
try
|
||||||
{
|
{
|
||||||
try
|
_logger.LogInformation("----- Publishing integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", eventMessage.Id, Program.AppShortName, eventMessage);
|
||||||
{
|
|
||||||
_logger.LogInformation("----- Publishing integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", eventMessage.Id, Program.AppShortName, eventMessage);
|
|
||||||
|
|
||||||
_eventBus.Publish(eventMessage);
|
_eventBus.Publish(eventMessage);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "----- ERROR Publishing integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", eventMessage.Id, Program.AppShortName, eventMessage);
|
_logger.LogError(ex, "----- ERROR Publishing integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", eventMessage.Id, Program.AppShortName, eventMessage);
|
||||||
|
|
||||||
throw;
|
throw;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return Accepted();
|
return Accepted();
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
using Basket.API.IntegrationEvents.Events;
|
using Basket.API.IntegrationEvents.Events;
|
||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
||||||
|
using Microsoft.eShopOnContainers.Services.Basket.API;
|
||||||
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
|
using Microsoft.eShopOnContainers.Services.Basket.API.Model;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Serilog.Context;
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@ -9,15 +12,24 @@ namespace Basket.API.IntegrationEvents.EventHandling
|
|||||||
public class OrderStartedIntegrationEventHandler : IIntegrationEventHandler<OrderStartedIntegrationEvent>
|
public class OrderStartedIntegrationEventHandler : IIntegrationEventHandler<OrderStartedIntegrationEvent>
|
||||||
{
|
{
|
||||||
private readonly IBasketRepository _repository;
|
private readonly IBasketRepository _repository;
|
||||||
|
private readonly ILogger<OrderStartedIntegrationEventHandler> _logger;
|
||||||
|
|
||||||
public OrderStartedIntegrationEventHandler(IBasketRepository repository)
|
public OrderStartedIntegrationEventHandler(
|
||||||
|
IBasketRepository repository,
|
||||||
|
ILogger<OrderStartedIntegrationEventHandler> logger)
|
||||||
{
|
{
|
||||||
_repository = repository ?? throw new ArgumentNullException(nameof(repository));
|
_repository = repository ?? throw new ArgumentNullException(nameof(repository));
|
||||||
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Handle(OrderStartedIntegrationEvent @event)
|
public async Task Handle(OrderStartedIntegrationEvent @event)
|
||||||
{
|
{
|
||||||
await _repository.DeleteBasketAsync(@event.UserId.ToString());
|
using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id))
|
||||||
|
{
|
||||||
|
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);
|
||||||
|
|
||||||
|
await _repository.DeleteBasketAsync(@event.UserId.ToString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ namespace Microsoft.eShopOnContainers.Services.Basket.API.IntegrationEvents.Even
|
|||||||
|
|
||||||
public async Task Handle(ProductPriceChangedIntegrationEvent @event)
|
public async Task Handle(ProductPriceChangedIntegrationEvent @event)
|
||||||
{
|
{
|
||||||
using (LogContext.PushProperty("IntegrationEventId", @event.Id))
|
using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id))
|
||||||
{
|
{
|
||||||
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);
|
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);
|
||||||
|
|
||||||
|
@ -37,39 +37,33 @@ namespace Catalog.API.IntegrationEvents
|
|||||||
|
|
||||||
public async Task PublishThroughEventBusAsync(IntegrationEvent evt)
|
public async Task PublishThroughEventBusAsync(IntegrationEvent evt)
|
||||||
{
|
{
|
||||||
using (LogContext.PushProperty("IntegrationEventId", evt.Id))
|
try
|
||||||
{
|
{
|
||||||
try
|
_logger.LogInformation("----- Publishing integration event: {IntegrationEventId_published} at {AppShortName} - ({@IntegrationEvent})", evt.Id, Program.AppShortName, evt);
|
||||||
{
|
|
||||||
_logger.LogInformation("----- Publishing integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", evt.Id, Program.AppShortName, evt);
|
|
||||||
|
|
||||||
await _eventLogService.MarkEventAsInProgressAsync(evt.Id);
|
await _eventLogService.MarkEventAsInProgressAsync(evt.Id);
|
||||||
_eventBus.Publish(evt);
|
_eventBus.Publish(evt);
|
||||||
await _eventLogService.MarkEventAsPublishedAsync(evt.Id);
|
await _eventLogService.MarkEventAsPublishedAsync(evt.Id);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
_logger.LogError(ex, "----- ERROR Publishing integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", evt.Id, Program.AppShortName, evt);
|
_logger.LogError(ex, "----- ERROR Publishing integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", evt.Id, Program.AppShortName, evt);
|
||||||
await _eventLogService.MarkEventAsFailedAsync(evt.Id);
|
await _eventLogService.MarkEventAsFailedAsync(evt.Id);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task SaveEventAndCatalogContextChangesAsync(IntegrationEvent evt)
|
public async Task SaveEventAndCatalogContextChangesAsync(IntegrationEvent evt)
|
||||||
{
|
{
|
||||||
using (LogContext.PushProperty("IntegrationEventId", evt.Id))
|
_logger.LogInformation("----- CatalogIntegrationEventService - Saving changes and integrationEvent: {IntegrationEventId}", evt.Id);
|
||||||
{
|
|
||||||
_logger.LogInformation("----- CatalogIntegrationEventService - Saving changes and integrationEvent: {IntegrationEventId}", evt.Id);
|
|
||||||
|
|
||||||
//Use of an EF Core resiliency strategy when using multiple DbContexts within an explicit BeginTransaction():
|
//Use of an EF Core resiliency strategy when using multiple DbContexts within an explicit BeginTransaction():
|
||||||
//See: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
|
//See: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
|
||||||
await ResilientTransaction.New(_catalogContext).ExecuteAsync(async () =>
|
await ResilientTransaction.New(_catalogContext).ExecuteAsync(async () =>
|
||||||
{
|
{
|
||||||
// Achieving atomicity between original catalog database operation and the IntegrationEventLog thanks to a local transaction
|
// Achieving atomicity between original catalog database operation and the IntegrationEventLog thanks to a local transaction
|
||||||
await _catalogContext.SaveChangesAsync();
|
await _catalogContext.SaveChangesAsync();
|
||||||
await _eventLogService.SaveEventAsync(evt, _catalogContext.Database.CurrentTransaction.GetDbTransaction());
|
await _eventLogService.SaveEventAsync(evt, _catalogContext.Database.CurrentTransaction.GetDbTransaction());
|
||||||
});
|
});
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,39 +8,51 @@
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using global::Catalog.API.IntegrationEvents;
|
using global::Catalog.API.IntegrationEvents;
|
||||||
using IntegrationEvents.Events;
|
using IntegrationEvents.Events;
|
||||||
|
using Serilog.Context;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
public class OrderStatusChangedToAwaitingValidationIntegrationEventHandler :
|
public class OrderStatusChangedToAwaitingValidationIntegrationEventHandler :
|
||||||
IIntegrationEventHandler<OrderStatusChangedToAwaitingValidationIntegrationEvent>
|
IIntegrationEventHandler<OrderStatusChangedToAwaitingValidationIntegrationEvent>
|
||||||
{
|
{
|
||||||
private readonly CatalogContext _catalogContext;
|
private readonly CatalogContext _catalogContext;
|
||||||
private readonly ICatalogIntegrationEventService _catalogIntegrationEventService;
|
private readonly ICatalogIntegrationEventService _catalogIntegrationEventService;
|
||||||
|
private readonly ILogger<OrderStatusChangedToAwaitingValidationIntegrationEventHandler> _logger;
|
||||||
|
|
||||||
public OrderStatusChangedToAwaitingValidationIntegrationEventHandler(CatalogContext catalogContext,
|
public OrderStatusChangedToAwaitingValidationIntegrationEventHandler(
|
||||||
ICatalogIntegrationEventService catalogIntegrationEventService)
|
CatalogContext catalogContext,
|
||||||
|
ICatalogIntegrationEventService catalogIntegrationEventService,
|
||||||
|
ILogger<OrderStatusChangedToAwaitingValidationIntegrationEventHandler> logger)
|
||||||
{
|
{
|
||||||
_catalogContext = catalogContext;
|
_catalogContext = catalogContext;
|
||||||
_catalogIntegrationEventService = catalogIntegrationEventService;
|
_catalogIntegrationEventService = catalogIntegrationEventService;
|
||||||
|
_logger = logger ?? throw new System.ArgumentNullException(nameof(logger));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Handle(OrderStatusChangedToAwaitingValidationIntegrationEvent command)
|
public async Task Handle(OrderStatusChangedToAwaitingValidationIntegrationEvent @event)
|
||||||
{
|
{
|
||||||
var confirmedOrderStockItems = new List<ConfirmedOrderStockItem>();
|
using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id))
|
||||||
|
|
||||||
foreach (var orderStockItem in command.OrderStockItems)
|
|
||||||
{
|
{
|
||||||
var catalogItem = _catalogContext.CatalogItems.Find(orderStockItem.ProductId);
|
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);
|
||||||
var hasStock = catalogItem.AvailableStock >= orderStockItem.Units;
|
|
||||||
var confirmedOrderStockItem = new ConfirmedOrderStockItem(catalogItem.Id, hasStock);
|
var confirmedOrderStockItems = new List<ConfirmedOrderStockItem>();
|
||||||
|
|
||||||
|
foreach (var orderStockItem in @event.OrderStockItems)
|
||||||
|
{
|
||||||
|
var catalogItem = _catalogContext.CatalogItems.Find(orderStockItem.ProductId);
|
||||||
|
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 OrderStockRejectedIntegrationEvent(@event.OrderId, confirmedOrderStockItems)
|
||||||
|
: new OrderStockConfirmedIntegrationEvent(@event.OrderId);
|
||||||
|
|
||||||
|
await _catalogIntegrationEventService.SaveEventAndCatalogContextChangesAsync(confirmedIntegrationEvent);
|
||||||
|
await _catalogIntegrationEventService.PublishThroughEventBusAsync(confirmedIntegrationEvent);
|
||||||
|
|
||||||
confirmedOrderStockItems.Add(confirmedOrderStockItem);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var confirmedIntegrationEvent = confirmedOrderStockItems.Any(c => !c.HasStock)
|
|
||||||
? (IntegrationEvent) new OrderStockRejectedIntegrationEvent(command.OrderId, confirmedOrderStockItems)
|
|
||||||
: new OrderStockConfirmedIntegrationEvent(command.OrderId);
|
|
||||||
|
|
||||||
await _catalogIntegrationEventService.SaveEventAndCatalogContextChangesAsync(confirmedIntegrationEvent);
|
|
||||||
await _catalogIntegrationEventService.PublishThroughEventBusAsync(confirmedIntegrationEvent);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -4,28 +4,40 @@
|
|||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Infrastructure;
|
using Infrastructure;
|
||||||
using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.Events;
|
using Microsoft.eShopOnContainers.Services.Catalog.API.IntegrationEvents.Events;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Serilog.Context;
|
||||||
|
|
||||||
public class OrderStatusChangedToPaidIntegrationEventHandler :
|
public class OrderStatusChangedToPaidIntegrationEventHandler :
|
||||||
IIntegrationEventHandler<OrderStatusChangedToPaidIntegrationEvent>
|
IIntegrationEventHandler<OrderStatusChangedToPaidIntegrationEvent>
|
||||||
{
|
{
|
||||||
private readonly CatalogContext _catalogContext;
|
private readonly CatalogContext _catalogContext;
|
||||||
|
private readonly ILogger<OrderStatusChangedToPaidIntegrationEventHandler> _logger;
|
||||||
|
|
||||||
public OrderStatusChangedToPaidIntegrationEventHandler(CatalogContext catalogContext)
|
public OrderStatusChangedToPaidIntegrationEventHandler(
|
||||||
|
CatalogContext catalogContext,
|
||||||
|
ILogger<OrderStatusChangedToPaidIntegrationEventHandler> logger)
|
||||||
{
|
{
|
||||||
_catalogContext = catalogContext;
|
_catalogContext = catalogContext;
|
||||||
|
_logger = logger ?? throw new System.ArgumentNullException(nameof(logger));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Handle(OrderStatusChangedToPaidIntegrationEvent command)
|
public async Task Handle(OrderStatusChangedToPaidIntegrationEvent @event)
|
||||||
{
|
{
|
||||||
//we're not blocking stock/inventory
|
using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id))
|
||||||
foreach (var orderStockItem in command.OrderStockItems)
|
|
||||||
{
|
{
|
||||||
var catalogItem = _catalogContext.CatalogItems.Find(orderStockItem.ProductId);
|
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);
|
||||||
|
|
||||||
|
//we're not blocking stock/inventory
|
||||||
|
foreach (var orderStockItem in @event.OrderStockItems)
|
||||||
|
{
|
||||||
|
var catalogItem = _catalogContext.CatalogItems.Find(orderStockItem.ProductId);
|
||||||
|
|
||||||
|
catalogItem.RemoveStock(orderStockItem.Units);
|
||||||
|
}
|
||||||
|
|
||||||
|
await _catalogContext.SaveChangesAsync();
|
||||||
|
|
||||||
catalogItem.RemoveStock(orderStockItem.Units);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await _catalogContext.SaveChangesAsync();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -4,6 +4,8 @@
|
|||||||
using Marketing.API.Model;
|
using Marketing.API.Model;
|
||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
||||||
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Repositories;
|
using Microsoft.eShopOnContainers.Services.Marketing.API.Infrastructure.Repositories;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Serilog.Context;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -12,20 +14,29 @@
|
|||||||
: IIntegrationEventHandler<UserLocationUpdatedIntegrationEvent>
|
: IIntegrationEventHandler<UserLocationUpdatedIntegrationEvent>
|
||||||
{
|
{
|
||||||
private readonly IMarketingDataRepository _marketingDataRepository;
|
private readonly IMarketingDataRepository _marketingDataRepository;
|
||||||
|
private readonly ILogger<UserLocationUpdatedIntegrationEventHandler> _logger;
|
||||||
|
|
||||||
public UserLocationUpdatedIntegrationEventHandler(IMarketingDataRepository repository)
|
public UserLocationUpdatedIntegrationEventHandler(
|
||||||
|
IMarketingDataRepository repository,
|
||||||
|
ILogger<UserLocationUpdatedIntegrationEventHandler> logger)
|
||||||
{
|
{
|
||||||
_marketingDataRepository = repository ?? throw new ArgumentNullException(nameof(repository));
|
_marketingDataRepository = repository ?? throw new ArgumentNullException(nameof(repository));
|
||||||
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Handle(UserLocationUpdatedIntegrationEvent @event)
|
public async Task Handle(UserLocationUpdatedIntegrationEvent @event)
|
||||||
{
|
{
|
||||||
var userMarketingData = await _marketingDataRepository.GetAsync(@event.UserId);
|
using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id))
|
||||||
userMarketingData = userMarketingData ??
|
{
|
||||||
new MarketingData() { UserId = @event.UserId };
|
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);
|
||||||
|
|
||||||
userMarketingData.Locations = MapUpdatedUserLocations(@event.LocationList);
|
var userMarketingData = await _marketingDataRepository.GetAsync(@event.UserId);
|
||||||
await _marketingDataRepository.UpdateLocationAsync(userMarketingData);
|
userMarketingData = userMarketingData ??
|
||||||
|
new MarketingData() { UserId = @event.UserId };
|
||||||
|
|
||||||
|
userMarketingData.Locations = MapUpdatedUserLocations(@event.LocationList);
|
||||||
|
await _marketingDataRepository.UpdateLocationAsync(userMarketingData);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Location> MapUpdatedUserLocations(List<UserLocationDetails> newUserLocations)
|
private List<Location> MapUpdatedUserLocations(List<UserLocationDetails> newUserLocations)
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
public class Program
|
public class Program
|
||||||
{
|
{
|
||||||
public static readonly string AppName = typeof(Program).Namespace;
|
public static readonly string AppName = typeof(Program).Namespace;
|
||||||
public static readonly string ShortAppName = AppName.Substring(AppName.LastIndexOf('.', AppName.LastIndexOf('.') - 1) + 1);
|
public static readonly string AppShortName = AppName.Substring(AppName.LastIndexOf('.', AppName.LastIndexOf('.') - 1) + 1);
|
||||||
|
|
||||||
public static int Main(string[] args)
|
public static int Main(string[] args)
|
||||||
{
|
{
|
||||||
|
@ -7,7 +7,7 @@ namespace Ordering.API.Application.Behaviors
|
|||||||
{
|
{
|
||||||
internal static class BehaviorsHelperExtensions
|
internal static class BehaviorsHelperExtensions
|
||||||
{
|
{
|
||||||
internal static string GetTypeName(this object @object)
|
internal static string GetGenericTypeName(this object @object)
|
||||||
{
|
{
|
||||||
var typeName = string.Empty;
|
var typeName = string.Empty;
|
||||||
var type = @object.GetType();
|
var type = @object.GetType();
|
||||||
|
@ -13,9 +13,9 @@ namespace Ordering.API.Application.Behaviors
|
|||||||
|
|
||||||
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
|
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("----- Handling command {CommandName} ({@Command})", request.GetTypeName(), request);
|
_logger.LogInformation("----- Handling command {CommandName} ({@Command})", request.GetGenericTypeName(), request);
|
||||||
var response = await next();
|
var response = await next();
|
||||||
_logger.LogInformation("----- Command {CommandName} handled - response: {@Response}", request.GetTypeName(), response);
|
_logger.LogInformation("----- Command {CommandName} handled - response: {@Response}", request.GetGenericTypeName(), response);
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,7 @@ namespace Ordering.API.Application.Behaviors
|
|||||||
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
|
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
|
||||||
{
|
{
|
||||||
TResponse response = default(TResponse);
|
TResponse response = default(TResponse);
|
||||||
var typeName = request.GetTypeName();
|
var typeName = request.GetGenericTypeName();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -21,7 +21,7 @@ namespace Ordering.API.Application.Behaviors
|
|||||||
|
|
||||||
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
|
public async Task<TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate<TResponse> next)
|
||||||
{
|
{
|
||||||
var typeName = request.GetTypeName();
|
var typeName = request.GetGenericTypeName();
|
||||||
|
|
||||||
_logger.LogInformation("----- Validating command {CommandType}", typeName);
|
_logger.LogInformation("----- Validating command {CommandType}", typeName);
|
||||||
|
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
using MediatR;
|
using MediatR;
|
||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
||||||
|
using Microsoft.eShopOnContainers.Services.Ordering.API;
|
||||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using Ordering.API.Application.Commands;
|
using Ordering.API.Application.Commands;
|
||||||
using Ordering.API.Application.IntegrationEvents.Events;
|
using Ordering.API.Application.IntegrationEvents.Events;
|
||||||
|
using Serilog.Context;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Ordering.API.Application.IntegrationEvents.EventHandling
|
namespace Ordering.API.Application.IntegrationEvents.EventHandling
|
||||||
@ -10,10 +13,14 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling
|
|||||||
public class GracePeriodConfirmedIntegrationEventHandler : IIntegrationEventHandler<GracePeriodConfirmedIntegrationEvent>
|
public class GracePeriodConfirmedIntegrationEventHandler : IIntegrationEventHandler<GracePeriodConfirmedIntegrationEvent>
|
||||||
{
|
{
|
||||||
private readonly IMediator _mediator;
|
private readonly IMediator _mediator;
|
||||||
|
private readonly ILogger<GracePeriodConfirmedIntegrationEventHandler> _logger;
|
||||||
|
|
||||||
public GracePeriodConfirmedIntegrationEventHandler(IMediator mediator)
|
public GracePeriodConfirmedIntegrationEventHandler(
|
||||||
|
IMediator mediator,
|
||||||
|
ILogger<GracePeriodConfirmedIntegrationEventHandler> logger)
|
||||||
{
|
{
|
||||||
_mediator = mediator;
|
_mediator = mediator;
|
||||||
|
_logger = logger ?? throw new System.ArgumentNullException(nameof(logger));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -26,8 +33,13 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task Handle(GracePeriodConfirmedIntegrationEvent @event)
|
public async Task Handle(GracePeriodConfirmedIntegrationEvent @event)
|
||||||
{
|
{
|
||||||
var command = new SetAwaitingValidationOrderStatusCommand(@event.OrderId);
|
using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id))
|
||||||
await _mediator.Send(command);
|
{
|
||||||
|
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);
|
||||||
|
|
||||||
|
var command = new SetAwaitingValidationOrderStatusCommand(@event.OrderId);
|
||||||
|
await _mediator.Send(command);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,12 @@
|
|||||||
{
|
{
|
||||||
using MediatR;
|
using MediatR;
|
||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
||||||
|
using Microsoft.eShopOnContainers.Services.Ordering.API;
|
||||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using Ordering.API.Application.Commands;
|
using Ordering.API.Application.Commands;
|
||||||
using Ordering.API.Application.IntegrationEvents.Events;
|
using Ordering.API.Application.IntegrationEvents.Events;
|
||||||
|
using Serilog.Context;
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@ -12,16 +15,25 @@
|
|||||||
IIntegrationEventHandler<OrderPaymentFailedIntegrationEvent>
|
IIntegrationEventHandler<OrderPaymentFailedIntegrationEvent>
|
||||||
{
|
{
|
||||||
private readonly IMediator _mediator;
|
private readonly IMediator _mediator;
|
||||||
|
private readonly ILogger<OrderPaymentFailedIntegrationEventHandler> _logger;
|
||||||
|
|
||||||
public OrderPaymentFailedIntegrationEventHandler(IMediator mediator)
|
public OrderPaymentFailedIntegrationEventHandler(
|
||||||
|
IMediator mediator,
|
||||||
|
ILogger<OrderPaymentFailedIntegrationEventHandler> logger)
|
||||||
{
|
{
|
||||||
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
|
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
|
||||||
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Handle(OrderPaymentFailedIntegrationEvent @event)
|
public async Task Handle(OrderPaymentFailedIntegrationEvent @event)
|
||||||
{
|
{
|
||||||
var command = new CancelOrderCommand(@event.OrderId);
|
using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id))
|
||||||
await _mediator.Send(command);
|
{
|
||||||
|
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);
|
||||||
|
|
||||||
|
var command = new CancelOrderCommand(@event.OrderId);
|
||||||
|
await _mediator.Send(command);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,12 @@
|
|||||||
{
|
{
|
||||||
using MediatR;
|
using MediatR;
|
||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
||||||
|
using Microsoft.eShopOnContainers.Services.Ordering.API;
|
||||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using Ordering.API.Application.Commands;
|
using Ordering.API.Application.Commands;
|
||||||
using Ordering.API.Application.IntegrationEvents.Events;
|
using Ordering.API.Application.IntegrationEvents.Events;
|
||||||
|
using Serilog.Context;
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@ -12,16 +15,25 @@
|
|||||||
IIntegrationEventHandler<OrderPaymentSuccededIntegrationEvent>
|
IIntegrationEventHandler<OrderPaymentSuccededIntegrationEvent>
|
||||||
{
|
{
|
||||||
private readonly IMediator _mediator;
|
private readonly IMediator _mediator;
|
||||||
|
private readonly ILogger<OrderPaymentSuccededIntegrationEventHandler> _logger;
|
||||||
|
|
||||||
public OrderPaymentSuccededIntegrationEventHandler(IMediator mediator)
|
public OrderPaymentSuccededIntegrationEventHandler(
|
||||||
|
IMediator mediator,
|
||||||
|
ILogger<OrderPaymentSuccededIntegrationEventHandler> logger)
|
||||||
{
|
{
|
||||||
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
|
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
|
||||||
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Handle(OrderPaymentSuccededIntegrationEvent @event)
|
public async Task Handle(OrderPaymentSuccededIntegrationEvent @event)
|
||||||
{
|
{
|
||||||
var command = new SetPaidOrderStatusCommand(@event.OrderId);
|
using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id))
|
||||||
await _mediator.Send(command);
|
{
|
||||||
|
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);
|
||||||
|
|
||||||
|
var command = new SetPaidOrderStatusCommand(@event.OrderId);
|
||||||
|
await _mediator.Send(command);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -7,21 +7,33 @@
|
|||||||
using MediatR;
|
using MediatR;
|
||||||
using System;
|
using System;
|
||||||
using Ordering.API.Application.Commands;
|
using Ordering.API.Application.Commands;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Serilog.Context;
|
||||||
|
using Microsoft.eShopOnContainers.Services.Ordering.API;
|
||||||
|
|
||||||
public class OrderStockConfirmedIntegrationEventHandler :
|
public class OrderStockConfirmedIntegrationEventHandler :
|
||||||
IIntegrationEventHandler<OrderStockConfirmedIntegrationEvent>
|
IIntegrationEventHandler<OrderStockConfirmedIntegrationEvent>
|
||||||
{
|
{
|
||||||
private readonly IMediator _mediator;
|
private readonly IMediator _mediator;
|
||||||
|
private readonly ILogger<OrderStockConfirmedIntegrationEventHandler> _logger;
|
||||||
|
|
||||||
public OrderStockConfirmedIntegrationEventHandler(IMediator mediator)
|
public OrderStockConfirmedIntegrationEventHandler(
|
||||||
|
IMediator mediator,
|
||||||
|
ILogger<OrderStockConfirmedIntegrationEventHandler> logger)
|
||||||
{
|
{
|
||||||
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
|
_mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
|
||||||
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Handle(OrderStockConfirmedIntegrationEvent @event)
|
public async Task Handle(OrderStockConfirmedIntegrationEvent @event)
|
||||||
{
|
{
|
||||||
var command = new SetStockConfirmedOrderStatusCommand(@event.OrderId);
|
using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id))
|
||||||
await _mediator.Send(command);
|
{
|
||||||
|
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);
|
||||||
|
|
||||||
|
var command = new SetStockConfirmedOrderStatusCommand(@event.OrderId);
|
||||||
|
await _mediator.Send(command);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -7,25 +7,37 @@
|
|||||||
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
||||||
using MediatR;
|
using MediatR;
|
||||||
using Ordering.API.Application.Commands;
|
using Ordering.API.Application.Commands;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Serilog.Context;
|
||||||
|
using Microsoft.eShopOnContainers.Services.Ordering.API;
|
||||||
|
|
||||||
public class OrderStockRejectedIntegrationEventHandler : IIntegrationEventHandler<OrderStockRejectedIntegrationEvent>
|
public class OrderStockRejectedIntegrationEventHandler : IIntegrationEventHandler<OrderStockRejectedIntegrationEvent>
|
||||||
{
|
{
|
||||||
private readonly IMediator _mediator;
|
private readonly IMediator _mediator;
|
||||||
|
private readonly ILogger<OrderStockRejectedIntegrationEventHandler> _logger;
|
||||||
|
|
||||||
public OrderStockRejectedIntegrationEventHandler(IMediator mediator)
|
public OrderStockRejectedIntegrationEventHandler(
|
||||||
|
IMediator mediator,
|
||||||
|
ILogger<OrderStockRejectedIntegrationEventHandler> logger)
|
||||||
{
|
{
|
||||||
_mediator = mediator;
|
_mediator = mediator;
|
||||||
|
_logger = logger ?? throw new System.ArgumentNullException(nameof(logger));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Handle(OrderStockRejectedIntegrationEvent @event)
|
public async Task Handle(OrderStockRejectedIntegrationEvent @event)
|
||||||
{
|
{
|
||||||
var orderStockRejectedItems = @event.OrderStockItems
|
using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id))
|
||||||
.FindAll(c => !c.HasStock)
|
{
|
||||||
.Select(c => c.ProductId)
|
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);
|
||||||
.ToList();
|
|
||||||
|
|
||||||
var command = new SetStockRejectedOrderStatusCommand(@event.OrderId, orderStockRejectedItems);
|
var orderStockRejectedItems = @event.OrderStockItems
|
||||||
await _mediator.Send(command);
|
.FindAll(c => !c.HasStock)
|
||||||
|
.Select(c => c.ProductId)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
var command = new SetStockRejectedOrderStatusCommand(@event.OrderId, orderStockRejectedItems);
|
||||||
|
await _mediator.Send(command);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -26,30 +26,30 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Integration event handler which starts the create order process
|
/// Integration event handler which starts the create order process
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="eventMsg">
|
/// <param name="@event">
|
||||||
/// Integration event message which is sent by the
|
/// Integration event message which is sent by the
|
||||||
/// basket.api once it has successfully process the
|
/// basket.api once it has successfully process the
|
||||||
/// order items.
|
/// order items.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task Handle(UserCheckoutAcceptedIntegrationEvent eventMsg)
|
public async Task Handle(UserCheckoutAcceptedIntegrationEvent @event)
|
||||||
{
|
{
|
||||||
using (LogContext.PushProperty("IntegrationEventId", eventMsg.Id))
|
using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id))
|
||||||
{
|
{
|
||||||
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", eventMsg.Id, Program.AppShortName, eventMsg);
|
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);
|
||||||
|
|
||||||
var result = false;
|
var result = false;
|
||||||
|
|
||||||
if (eventMsg.RequestId != Guid.Empty)
|
if (@event.RequestId != Guid.Empty)
|
||||||
{
|
{
|
||||||
using (LogContext.PushProperty("IdentifiedCommandId", eventMsg.RequestId))
|
using (LogContext.PushProperty("IdentifiedCommandId", @event.RequestId))
|
||||||
{
|
{
|
||||||
var createOrderCommand = new CreateOrderCommand(eventMsg.Basket.Items, eventMsg.UserId, eventMsg.UserName, eventMsg.City, eventMsg.Street,
|
var createOrderCommand = new CreateOrderCommand(@event.Basket.Items, @event.UserId, @event.UserName, @event.City, @event.Street,
|
||||||
eventMsg.State, eventMsg.Country, eventMsg.ZipCode,
|
@event.State, @event.Country, @event.ZipCode,
|
||||||
eventMsg.CardNumber, eventMsg.CardHolderName, eventMsg.CardExpiration,
|
@event.CardNumber, @event.CardHolderName, @event.CardExpiration,
|
||||||
eventMsg.CardSecurityNumber, eventMsg.CardTypeId);
|
@event.CardSecurityNumber, @event.CardTypeId);
|
||||||
|
|
||||||
var requestCreateOrder = new IdentifiedCommand<CreateOrderCommand, bool>(createOrderCommand, eventMsg.RequestId);
|
var requestCreateOrder = new IdentifiedCommand<CreateOrderCommand, bool>(createOrderCommand, @event.RequestId);
|
||||||
|
|
||||||
_logger.LogInformation("----- IdentifiedCreateOrderCommand: {@IdentifiedCreateOrderCommand}", requestCreateOrder);
|
_logger.LogInformation("----- IdentifiedCreateOrderCommand: {@IdentifiedCreateOrderCommand}", requestCreateOrder);
|
||||||
|
|
||||||
@ -57,17 +57,17 @@ namespace Ordering.API.Application.IntegrationEvents.EventHandling
|
|||||||
|
|
||||||
if (result)
|
if (result)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("----- CreateOrderCommand suceeded - RequestId: {RequestId}", eventMsg.RequestId);
|
_logger.LogInformation("----- CreateOrderCommand suceeded - RequestId: {RequestId}", @event.RequestId);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_logger.LogWarning("----- CreateOrderCommand failed - RequestId: {RequestId}", eventMsg.RequestId);
|
_logger.LogWarning("----- CreateOrderCommand failed - RequestId: {RequestId}", @event.RequestId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_logger.LogWarning("----- Invalid IntegrationEvent - RequestId is missing - {@IntegrationEvent}", eventMsg);
|
_logger.LogWarning("----- Invalid IntegrationEvent - RequestId is missing - {@IntegrationEvent}", @event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
|||||||
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.BuildingBlocks.IntegrationEventLogEF.Utilities;
|
using Microsoft.eShopOnContainers.BuildingBlocks.IntegrationEventLogEF.Utilities;
|
||||||
|
using Microsoft.eShopOnContainers.Services.Ordering.API;
|
||||||
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure;
|
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
using System;
|
using System;
|
||||||
@ -44,7 +45,7 @@ namespace Ordering.API.Application.IntegrationEvents
|
|||||||
|
|
||||||
foreach (var logEvt in pendindLogEvents)
|
foreach (var logEvt in pendindLogEvents)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("----- Publishing integration event {IntegrationEventId} ({@IntegrationEvent})", logEvt.EventId, logEvt);
|
_logger.LogInformation("----- Publishing integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", logEvt.EventId, Program.AppShortName, logEvt);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
using Microsoft.AspNetCore.SignalR;
|
using Microsoft.AspNetCore.SignalR;
|
||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using Ordering.SignalrHub.IntegrationEvents.Events;
|
using Ordering.SignalrHub.IntegrationEvents.Events;
|
||||||
|
using Serilog.Context;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -11,18 +13,27 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling
|
|||||||
public class OrderStatusChangedToCancelledIntegrationEventHandler : IIntegrationEventHandler<OrderStatusChangedToCancelledIntegrationEvent>
|
public class OrderStatusChangedToCancelledIntegrationEventHandler : IIntegrationEventHandler<OrderStatusChangedToCancelledIntegrationEvent>
|
||||||
{
|
{
|
||||||
private readonly IHubContext<NotificationsHub> _hubContext;
|
private readonly IHubContext<NotificationsHub> _hubContext;
|
||||||
|
private readonly ILogger<OrderStatusChangedToCancelledIntegrationEventHandler> _logger;
|
||||||
|
|
||||||
public OrderStatusChangedToCancelledIntegrationEventHandler(IHubContext<NotificationsHub> hubContext)
|
public OrderStatusChangedToCancelledIntegrationEventHandler(
|
||||||
|
IHubContext<NotificationsHub> hubContext,
|
||||||
|
ILogger<OrderStatusChangedToCancelledIntegrationEventHandler> logger)
|
||||||
{
|
{
|
||||||
_hubContext = hubContext ?? throw new ArgumentNullException(nameof(hubContext));
|
_hubContext = hubContext ?? throw new ArgumentNullException(nameof(hubContext));
|
||||||
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public async Task Handle(OrderStatusChangedToCancelledIntegrationEvent @event)
|
public async Task Handle(OrderStatusChangedToCancelledIntegrationEvent @event)
|
||||||
{
|
{
|
||||||
await _hubContext.Clients
|
using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id))
|
||||||
.Group(@event.BuyerName)
|
{
|
||||||
.SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus });
|
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);
|
||||||
|
|
||||||
|
await _hubContext.Clients
|
||||||
|
.Group(@event.BuyerName)
|
||||||
|
.SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,6 +1,8 @@
|
|||||||
using Microsoft.AspNetCore.SignalR;
|
using Microsoft.AspNetCore.SignalR;
|
||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using Ordering.SignalrHub.IntegrationEvents.Events;
|
using Ordering.SignalrHub.IntegrationEvents.Events;
|
||||||
|
using Serilog.Context;
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
@ -9,18 +11,27 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling
|
|||||||
public class OrderStatusChangedToPaidIntegrationEventHandler : IIntegrationEventHandler<OrderStatusChangedToPaidIntegrationEvent>
|
public class OrderStatusChangedToPaidIntegrationEventHandler : IIntegrationEventHandler<OrderStatusChangedToPaidIntegrationEvent>
|
||||||
{
|
{
|
||||||
private readonly IHubContext<NotificationsHub> _hubContext;
|
private readonly IHubContext<NotificationsHub> _hubContext;
|
||||||
|
private readonly ILogger<OrderStatusChangedToPaidIntegrationEventHandler> _logger;
|
||||||
|
|
||||||
public OrderStatusChangedToPaidIntegrationEventHandler(IHubContext<NotificationsHub> hubContext)
|
public OrderStatusChangedToPaidIntegrationEventHandler(
|
||||||
|
IHubContext<NotificationsHub> hubContext,
|
||||||
|
ILogger<OrderStatusChangedToPaidIntegrationEventHandler> logger)
|
||||||
{
|
{
|
||||||
_hubContext = hubContext ?? throw new ArgumentNullException(nameof(hubContext));
|
_hubContext = hubContext ?? throw new ArgumentNullException(nameof(hubContext));
|
||||||
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public async Task Handle(OrderStatusChangedToPaidIntegrationEvent @event)
|
public async Task Handle(OrderStatusChangedToPaidIntegrationEvent @event)
|
||||||
{
|
{
|
||||||
await _hubContext.Clients
|
using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id))
|
||||||
.Group(@event.BuyerName)
|
{
|
||||||
.SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus });
|
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);
|
||||||
|
|
||||||
|
await _hubContext.Clients
|
||||||
|
.Group(@event.BuyerName)
|
||||||
|
.SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
using Microsoft.AspNetCore.SignalR;
|
using Microsoft.AspNetCore.SignalR;
|
||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using Ordering.SignalrHub.IntegrationEvents.Events;
|
using Ordering.SignalrHub.IntegrationEvents.Events;
|
||||||
|
using Serilog.Context;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -11,18 +13,27 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling
|
|||||||
public class OrderStatusChangedToShippedIntegrationEventHandler : IIntegrationEventHandler<OrderStatusChangedToShippedIntegrationEvent>
|
public class OrderStatusChangedToShippedIntegrationEventHandler : IIntegrationEventHandler<OrderStatusChangedToShippedIntegrationEvent>
|
||||||
{
|
{
|
||||||
private readonly IHubContext<NotificationsHub> _hubContext;
|
private readonly IHubContext<NotificationsHub> _hubContext;
|
||||||
|
private readonly ILogger<OrderStatusChangedToShippedIntegrationEventHandler> _logger;
|
||||||
|
|
||||||
public OrderStatusChangedToShippedIntegrationEventHandler(IHubContext<NotificationsHub> hubContext)
|
public OrderStatusChangedToShippedIntegrationEventHandler(
|
||||||
|
IHubContext<NotificationsHub> hubContext,
|
||||||
|
ILogger<OrderStatusChangedToShippedIntegrationEventHandler> logger)
|
||||||
{
|
{
|
||||||
_hubContext = hubContext ?? throw new ArgumentNullException(nameof(hubContext));
|
_hubContext = hubContext ?? throw new ArgumentNullException(nameof(hubContext));
|
||||||
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public async Task Handle(OrderStatusChangedToShippedIntegrationEvent @event)
|
public async Task Handle(OrderStatusChangedToShippedIntegrationEvent @event)
|
||||||
{
|
{
|
||||||
await _hubContext.Clients
|
using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id))
|
||||||
.Group(@event.BuyerName)
|
{
|
||||||
.SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus });
|
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);
|
||||||
|
|
||||||
|
await _hubContext.Clients
|
||||||
|
.Group(@event.BuyerName)
|
||||||
|
.SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
using Microsoft.AspNetCore.SignalR;
|
using Microsoft.AspNetCore.SignalR;
|
||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using Ordering.SignalrHub.IntegrationEvents.Events;
|
using Ordering.SignalrHub.IntegrationEvents.Events;
|
||||||
|
using Serilog.Context;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -12,18 +14,27 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling
|
|||||||
IIntegrationEventHandler<OrderStatusChangedToStockConfirmedIntegrationEvent>
|
IIntegrationEventHandler<OrderStatusChangedToStockConfirmedIntegrationEvent>
|
||||||
{
|
{
|
||||||
private readonly IHubContext<NotificationsHub> _hubContext;
|
private readonly IHubContext<NotificationsHub> _hubContext;
|
||||||
|
private readonly ILogger<OrderStatusChangedToStockConfirmedIntegrationEventHandler> _logger;
|
||||||
|
|
||||||
public OrderStatusChangedToStockConfirmedIntegrationEventHandler(IHubContext<NotificationsHub> hubContext)
|
public OrderStatusChangedToStockConfirmedIntegrationEventHandler(
|
||||||
|
IHubContext<NotificationsHub> hubContext,
|
||||||
|
ILogger<OrderStatusChangedToStockConfirmedIntegrationEventHandler> logger)
|
||||||
{
|
{
|
||||||
_hubContext = hubContext ?? throw new ArgumentNullException(nameof(hubContext));
|
_hubContext = hubContext ?? throw new ArgumentNullException(nameof(hubContext));
|
||||||
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public async Task Handle(OrderStatusChangedToStockConfirmedIntegrationEvent @event)
|
public async Task Handle(OrderStatusChangedToStockConfirmedIntegrationEvent @event)
|
||||||
{
|
{
|
||||||
await _hubContext.Clients
|
using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id))
|
||||||
.Group(@event.BuyerName)
|
{
|
||||||
.SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus });
|
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);
|
||||||
|
|
||||||
|
await _hubContext.Clients
|
||||||
|
.Group(@event.BuyerName)
|
||||||
|
.SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
using Microsoft.AspNetCore.SignalR;
|
using Microsoft.AspNetCore.SignalR;
|
||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using Ordering.SignalrHub.IntegrationEvents.Events;
|
using Ordering.SignalrHub.IntegrationEvents.Events;
|
||||||
|
using Serilog.Context;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -12,18 +14,27 @@ namespace Ordering.SignalrHub.IntegrationEvents.EventHandling
|
|||||||
IIntegrationEventHandler<OrderStatusChangedToSubmittedIntegrationEvent>
|
IIntegrationEventHandler<OrderStatusChangedToSubmittedIntegrationEvent>
|
||||||
{
|
{
|
||||||
private readonly IHubContext<NotificationsHub> _hubContext;
|
private readonly IHubContext<NotificationsHub> _hubContext;
|
||||||
|
private readonly ILogger<OrderStatusChangedToSubmittedIntegrationEventHandler> _logger;
|
||||||
|
|
||||||
public OrderStatusChangedToSubmittedIntegrationEventHandler(IHubContext<NotificationsHub> hubContext)
|
public OrderStatusChangedToSubmittedIntegrationEventHandler(
|
||||||
|
IHubContext<NotificationsHub> hubContext,
|
||||||
|
ILogger<OrderStatusChangedToSubmittedIntegrationEventHandler> logger)
|
||||||
{
|
{
|
||||||
_hubContext = hubContext ?? throw new ArgumentNullException(nameof(hubContext));
|
_hubContext = hubContext ?? throw new ArgumentNullException(nameof(hubContext));
|
||||||
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public async Task Handle(OrderStatusChangedToSubmittedIntegrationEvent @event)
|
public async Task Handle(OrderStatusChangedToSubmittedIntegrationEvent @event)
|
||||||
{
|
{
|
||||||
await _hubContext.Clients
|
using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id))
|
||||||
.Group(@event.BuyerName)
|
{
|
||||||
.SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus });
|
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);
|
||||||
|
|
||||||
|
await _hubContext.Clients
|
||||||
|
.Group(@event.BuyerName)
|
||||||
|
.SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,5 +1,7 @@
|
|||||||
using Microsoft.AspNetCore.SignalR;
|
using Microsoft.AspNetCore.SignalR;
|
||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Serilog.Context;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -10,18 +12,27 @@ namespace Ordering.SignalrHub.IntegrationEvents
|
|||||||
public class OrderStatusChangedToAwaitingValidationIntegrationEventHandler : IIntegrationEventHandler<OrderStatusChangedToAwaitingValidationIntegrationEvent>
|
public class OrderStatusChangedToAwaitingValidationIntegrationEventHandler : IIntegrationEventHandler<OrderStatusChangedToAwaitingValidationIntegrationEvent>
|
||||||
{
|
{
|
||||||
private readonly IHubContext<NotificationsHub> _hubContext;
|
private readonly IHubContext<NotificationsHub> _hubContext;
|
||||||
|
private readonly ILogger<OrderStatusChangedToAwaitingValidationIntegrationEventHandler> _logger;
|
||||||
|
|
||||||
public OrderStatusChangedToAwaitingValidationIntegrationEventHandler(IHubContext<NotificationsHub> hubContext)
|
public OrderStatusChangedToAwaitingValidationIntegrationEventHandler(
|
||||||
|
IHubContext<NotificationsHub> hubContext,
|
||||||
|
ILogger<OrderStatusChangedToAwaitingValidationIntegrationEventHandler> logger)
|
||||||
{
|
{
|
||||||
_hubContext = hubContext ?? throw new ArgumentNullException(nameof(hubContext));
|
_hubContext = hubContext ?? throw new ArgumentNullException(nameof(hubContext));
|
||||||
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public async Task Handle(OrderStatusChangedToAwaitingValidationIntegrationEvent @event)
|
public async Task Handle(OrderStatusChangedToAwaitingValidationIntegrationEvent @event)
|
||||||
{
|
{
|
||||||
await _hubContext.Clients
|
using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id))
|
||||||
.Group(@event.BuyerName)
|
{
|
||||||
.SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus });
|
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);
|
||||||
|
|
||||||
|
await _hubContext.Clients
|
||||||
|
.Group(@event.BuyerName)
|
||||||
|
.SendAsync("UpdatedOrderState", new { OrderId = @event.OrderId, Status = @event.OrderStatus });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,10 @@
|
|||||||
{
|
{
|
||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Abstractions;
|
||||||
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
using Microsoft.eShopOnContainers.BuildingBlocks.EventBus.Events;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using Payment.API.IntegrationEvents.Events;
|
using Payment.API.IntegrationEvents.Events;
|
||||||
|
using Serilog.Context;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
public class OrderStatusChangedToStockConfirmedIntegrationEventHandler :
|
public class OrderStatusChangedToStockConfirmedIntegrationEventHandler :
|
||||||
@ -11,36 +13,45 @@
|
|||||||
{
|
{
|
||||||
private readonly IEventBus _eventBus;
|
private readonly IEventBus _eventBus;
|
||||||
private readonly PaymentSettings _settings;
|
private readonly PaymentSettings _settings;
|
||||||
|
private readonly ILogger<OrderStatusChangedToStockConfirmedIntegrationEventHandler> _logger;
|
||||||
|
|
||||||
public OrderStatusChangedToStockConfirmedIntegrationEventHandler(IEventBus eventBus,
|
public OrderStatusChangedToStockConfirmedIntegrationEventHandler(
|
||||||
IOptionsSnapshot<PaymentSettings> settings)
|
IEventBus eventBus,
|
||||||
|
IOptionsSnapshot<PaymentSettings> settings,
|
||||||
|
ILogger<OrderStatusChangedToStockConfirmedIntegrationEventHandler> logger)
|
||||||
{
|
{
|
||||||
_eventBus = eventBus;
|
_eventBus = eventBus;
|
||||||
_settings = settings.Value;
|
_settings = settings.Value;
|
||||||
|
_logger = logger ?? throw new System.ArgumentNullException(nameof(logger));
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task Handle(OrderStatusChangedToStockConfirmedIntegrationEvent @event)
|
public async Task Handle(OrderStatusChangedToStockConfirmedIntegrationEvent @event)
|
||||||
{
|
{
|
||||||
IntegrationEvent orderPaymentIntegrationEvent;
|
using (LogContext.PushProperty("IntegrationEventId_Context", @event.Id))
|
||||||
|
|
||||||
//Business feature comment:
|
|
||||||
// When OrderStatusChangedToStockConfirmed Integration Event is handled.
|
|
||||||
// Here we're simulating that we'd be performing the payment against any payment gateway
|
|
||||||
// Instead of a real payment we just take the env. var to simulate the payment
|
|
||||||
// The payment can be successful or it can fail
|
|
||||||
|
|
||||||
if (_settings.PaymentSucceded)
|
|
||||||
{
|
{
|
||||||
orderPaymentIntegrationEvent = new OrderPaymentSuccededIntegrationEvent(@event.OrderId);
|
_logger.LogInformation("----- Handling integration event: {IntegrationEventId} at {AppShortName} - ({@IntegrationEvent})", @event.Id, Program.AppShortName, @event);
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
orderPaymentIntegrationEvent = new OrderPaymentFailedIntegrationEvent(@event.OrderId);
|
|
||||||
}
|
|
||||||
|
|
||||||
_eventBus.Publish(orderPaymentIntegrationEvent);
|
IntegrationEvent orderPaymentIntegrationEvent;
|
||||||
|
|
||||||
await Task.CompletedTask;
|
//Business feature comment:
|
||||||
|
// When OrderStatusChangedToStockConfirmed Integration Event is handled.
|
||||||
|
// Here we're simulating that we'd be performing the payment against any payment gateway
|
||||||
|
// Instead of a real payment we just take the env. var to simulate the payment
|
||||||
|
// The payment can be successful or it can fail
|
||||||
|
|
||||||
|
if (_settings.PaymentSucceded)
|
||||||
|
{
|
||||||
|
orderPaymentIntegrationEvent = new OrderPaymentSuccededIntegrationEvent(@event.OrderId);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
orderPaymentIntegrationEvent = new OrderPaymentFailedIntegrationEvent(@event.OrderId);
|
||||||
|
}
|
||||||
|
|
||||||
|
_eventBus.Publish(orderPaymentIntegrationEvent);
|
||||||
|
|
||||||
|
await Task.CompletedTask;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -12,7 +12,7 @@ namespace Payment.API
|
|||||||
public class Program
|
public class Program
|
||||||
{
|
{
|
||||||
public static readonly string AppName = typeof(Program).Namespace;
|
public static readonly string AppName = typeof(Program).Namespace;
|
||||||
public static readonly string ShortAppName = AppName.Substring(AppName.LastIndexOf('.', AppName.LastIndexOf('.') - 1) + 1);
|
public static readonly string AppShortName = AppName.Substring(AppName.LastIndexOf('.', AppName.LastIndexOf('.') - 1) + 1);
|
||||||
|
|
||||||
public static int Main(string[] args)
|
public static int Main(string[] args)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user