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 Microsoft.Extensions.Logging; using System.Threading; using System.Threading.Tasks; namespace Ordering.API.Application.Commands { // Regular CommandHandler public class SetStockConfirmedOrderStatusCommandHandler : IRequestHandler { private readonly IOrderRepository _orderRepository; public SetStockConfirmedOrderStatusCommandHandler(IOrderRepository orderRepository) { _orderRepository = orderRepository; } /// /// Handler which processes the command when /// Stock service confirms the request /// /// /// public async Task Handle(SetStockConfirmedOrderStatusCommand command, CancellationToken cancellationToken) { // Simulate a work time for confirming the stock await Task.Delay(10000); var orderToUpdate = await _orderRepository.GetAsync(command.OrderNumber); if(orderToUpdate == null) { return false; } orderToUpdate.SetStockConfirmedStatus(); return await _orderRepository.UnitOfWork.SaveEntitiesAsync(); } } // Use for Idempotency in Command process public class SetStockConfirmedOrderStatusIdenfifiedCommandHandler : IdentifiedCommandHandler { public SetStockConfirmedOrderStatusIdenfifiedCommandHandler( IMediator mediator, IRequestManager requestManager, ILogger> logger) : base(mediator, requestManager, logger) { } protected override bool CreateResultForDuplicateRequest() { return true; // Ignore duplicate requests for processing order. } } }