50 lines
1.7 KiB
C#
50 lines
1.7 KiB
C#
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;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Ordering.API.Application.Commands
|
|
{
|
|
// Regular CommandHandler
|
|
public class CancelOrderCommandHandler : IAsyncRequestHandler<CancelOrderCommand, bool>
|
|
{
|
|
private readonly IOrderRepository _orderRepository;
|
|
|
|
public CancelOrderCommandHandler(IOrderRepository orderRepository)
|
|
{
|
|
_orderRepository = orderRepository;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Handler which processes the command when
|
|
/// customer executes cancel order from app
|
|
/// </summary>
|
|
/// <param name="command"></param>
|
|
/// <returns></returns>
|
|
public async Task<bool> Handle(CancelOrderCommand command)
|
|
{
|
|
var orderToUpdate = await _orderRepository.GetAsync(command.OrderNumber);
|
|
orderToUpdate.SetCancelledStatus();
|
|
return await _orderRepository.UnitOfWork.SaveEntitiesAsync();
|
|
}
|
|
}
|
|
|
|
|
|
// Use for Idempotency in Command process
|
|
public class CancelOrderIdentifiedCommandHandler : IdentifiedCommandHandler<CancelOrderCommand, bool>
|
|
{
|
|
public CancelOrderIdentifiedCommandHandler(IMediator mediator, IRequestManager requestManager) : base(mediator, requestManager)
|
|
{
|
|
}
|
|
|
|
protected override bool CreateResultForDuplicateRequest()
|
|
{
|
|
return true; // Ignore duplicate requests for processing order.
|
|
}
|
|
}
|
|
}
|