2018-10-08 10:14:23 +02:00
|
|
|
|
using MediatR;
|
|
|
|
|
using Microsoft.eShopOnContainers.Services.Ordering.API.Application.Commands;
|
|
|
|
|
using Microsoft.eShopOnContainers.Services.Ordering.Domain.AggregatesModel.OrderAggregate;
|
|
|
|
|
using Microsoft.eShopOnContainers.Services.Ordering.Infrastructure.Idempotency;
|
2019-02-21 15:56:15 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2018-10-08 10:14:23 +02:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Ordering.API.Application.Commands
|
|
|
|
|
{
|
|
|
|
|
// Regular CommandHandler
|
|
|
|
|
public class SetStockRejectedOrderStatusCommandHandler : IRequestHandler<SetStockRejectedOrderStatusCommand, bool>
|
|
|
|
|
{
|
|
|
|
|
private readonly IOrderRepository _orderRepository;
|
|
|
|
|
|
|
|
|
|
public SetStockRejectedOrderStatusCommandHandler(IOrderRepository orderRepository)
|
|
|
|
|
{
|
|
|
|
|
_orderRepository = orderRepository;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handler which processes the command when
|
|
|
|
|
/// Stock service rejects the request
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="command"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public async Task<bool> Handle(SetStockRejectedOrderStatusCommand command, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
// Simulate a work time for rejecting the stock
|
|
|
|
|
await Task.Delay(10000);
|
|
|
|
|
|
|
|
|
|
var orderToUpdate = await _orderRepository.GetAsync(command.OrderNumber);
|
|
|
|
|
if(orderToUpdate == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
orderToUpdate.SetCancelledStatusWhenStockIsRejected(command.OrderStockItems);
|
|
|
|
|
|
|
|
|
|
return await _orderRepository.UnitOfWork.SaveEntitiesAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Use for Idempotency in Command process
|
|
|
|
|
public class SetStockRejectedOrderStatusIdenfifiedCommandHandler : IdentifiedCommandHandler<SetStockRejectedOrderStatusCommand, bool>
|
|
|
|
|
{
|
2019-02-21 15:56:15 +00:00
|
|
|
|
public SetStockRejectedOrderStatusIdenfifiedCommandHandler(
|
|
|
|
|
IMediator mediator,
|
|
|
|
|
IRequestManager requestManager,
|
|
|
|
|
ILogger<IdentifiedCommandHandler<SetStockRejectedOrderStatusCommand, bool>> logger)
|
|
|
|
|
: base(mediator, requestManager, logger)
|
2018-10-08 10:14:23 +02:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool CreateResultForDuplicateRequest()
|
|
|
|
|
{
|
|
|
|
|
return true; // Ignore duplicate requests for processing order.
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|