using Netina.Domain.Entities.Orders;

namespace Netina.Repository.Handlers.Orders;

public class DeleteOrderCommandHandler(IRepositoryWrapper repositoryWrapper) : IRequestHandler<DeleteOrderCommand, bool>
{
    public async Task<bool> Handle(DeleteOrderCommand 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);
        repositoryWrapper.SetRepository<Order>().Delete(order);
        await repositoryWrapper.SaveChangesAsync(cancellationToken);
        return true;
    }
}