namespace Netina.Core.EntityServices.OrderHandlers;

public class CancelOrderStepCommandHandler (IRepositoryWrapper repositoryWrapper) : IRequestHandler<CancelOrderStepCommand,bool>
{
    public async Task<bool> Handle(CancelOrderStepCommand request, CancellationToken cancellationToken)
    {
        var order = await repositoryWrapper.SetRepository<Order>()
            .TableNoTracking
            .FirstOrDefaultAsync(o => o.Id == request.OrderId, cancellationToken);
        if (order == null)
            throw new AppException("Order not found", ApiResultStatusCode.NotFound);

        order.SetOrderStatus(OrderStatus.Canceled);

        repositoryWrapper.SetRepository<Order>().Update(order);
        await repositoryWrapper.SaveChangesAsync(cancellationToken);

        return true;

    }
}