using MediatR; using ZeroFramework.DeviceCenter.Application.Infrastructure; using ZeroFramework.DeviceCenter.Domain.Aggregates.OrderAggregate; using ZeroFramework.DeviceCenter.Infrastructure.Idempotency; namespace ZeroFramework.DeviceCenter.Application.Commands.Ordering { public class CancelOrderCommandHandler(IOrderRepository orderRepository) : IRequestHandler { private readonly IOrderRepository _orderRepository = orderRepository; /// /// Handler which processes the command when customer executes cancel order from app /// public async Task Handle(CancelOrderCommand command, CancellationToken cancellationToken) { var orderToUpdate = await _orderRepository.GetAsync(command.OrderId); if (orderToUpdate != null) { orderToUpdate.SetCancelledStatus(); await _orderRepository.UnitOfWork.SaveChangesAsync(cancellationToken); } return await Task.FromResult(true); } } /// /// Use for Idempotency in Command process /// public class CancelOrderIdentifiedCommandHandler(IMediator mediator, IRequestManager requestManager) : IdentifiedCommandHandler(mediator, requestManager) { protected override bool CreateResultForDuplicateRequest() { return true; // Ignore duplicate requests for processing order. } } }