|
|
@ -1,5 +1,9 @@ |
|
|
|
using MediatR; |
|
|
|
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency; |
|
|
|
using Microsoft.Extensions.Logging; |
|
|
|
using Ordering.API.Application.Behaviors; |
|
|
|
using Ordering.API.Application.Commands; |
|
|
|
using System; |
|
|
|
using System.Threading; |
|
|
|
using System.Threading.Tasks; |
|
|
|
|
|
|
@ -16,11 +20,16 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands |
|
|
|
{ |
|
|
|
private readonly IMediator _mediator; |
|
|
|
private readonly IRequestManager _requestManager; |
|
|
|
private readonly ILogger<IdentifiedCommandHandler<T, R>> _logger; |
|
|
|
|
|
|
|
public IdentifiedCommandHandler(IMediator mediator, IRequestManager requestManager) |
|
|
|
public IdentifiedCommandHandler( |
|
|
|
IMediator mediator, |
|
|
|
IRequestManager requestManager, |
|
|
|
ILogger<IdentifiedCommandHandler<T, R>> logger) |
|
|
|
{ |
|
|
|
_mediator = mediator; |
|
|
|
_requestManager = requestManager; |
|
|
|
_logger = logger ?? throw new System.ArgumentNullException(nameof(logger)); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
@ -48,16 +57,60 @@ namespace Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands |
|
|
|
else |
|
|
|
{ |
|
|
|
await _requestManager.CreateRequestForCommandAsync<T>(message.Id); |
|
|
|
try |
|
|
|
{ |
|
|
|
// Send the embeded business command to mediator so it runs its related CommandHandler
|
|
|
|
var result = await _mediator.Send(message.Command); |
|
|
|
return result; |
|
|
|
} |
|
|
|
catch |
|
|
|
{ |
|
|
|
return default(R); |
|
|
|
} |
|
|
|
try |
|
|
|
{ |
|
|
|
var command = message.Command; |
|
|
|
var commandName = command.GetGenericTypeName(); |
|
|
|
var idProperty = string.Empty; |
|
|
|
var commandId = string.Empty; |
|
|
|
|
|
|
|
switch (command) |
|
|
|
{ |
|
|
|
case CreateOrderCommand createOrderCommand: |
|
|
|
idProperty = nameof(createOrderCommand.UserId); |
|
|
|
commandId = createOrderCommand.UserId; |
|
|
|
break; |
|
|
|
|
|
|
|
case CancelOrderCommand cancelOrderCommand: |
|
|
|
idProperty = nameof(cancelOrderCommand.OrderNumber); |
|
|
|
commandId = $"{cancelOrderCommand.OrderNumber}"; |
|
|
|
break; |
|
|
|
|
|
|
|
case ShipOrderCommand shipOrderCommand: |
|
|
|
idProperty = nameof(shipOrderCommand.OrderNumber); |
|
|
|
commandId = $"{shipOrderCommand.OrderNumber}"; |
|
|
|
break; |
|
|
|
|
|
|
|
default: |
|
|
|
idProperty = "Id?"; |
|
|
|
commandId = "n/a"; |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
_logger.LogInformation( |
|
|
|
"----- Sending command: {CommandName} - {IdProperty}: {CommandId} ({@Command})", |
|
|
|
commandName, |
|
|
|
idProperty, |
|
|
|
commandId, |
|
|
|
command); |
|
|
|
|
|
|
|
// Send the embeded business command to mediator so it runs its related CommandHandler
|
|
|
|
var result = await _mediator.Send(command); |
|
|
|
|
|
|
|
_logger.LogInformation( |
|
|
|
"----- Command result: {@Result} - {CommandName} - {IdProperty}: {CommandId} ({@Command})", |
|
|
|
result, |
|
|
|
commandName, |
|
|
|
idProperty, |
|
|
|
commandId, |
|
|
|
command); |
|
|
|
|
|
|
|
return result; |
|
|
|
} |
|
|
|
catch |
|
|
|
{ |
|
|
|
return default(R); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |