namespace Netina.Repository.Handlers.Discounts;

public class GetDiscountQueryHandler(IRepositoryWrapper repositoryWrapper)
    : IRequestHandler<GetDiscountQuery, DiscountLDto>
{
    public async Task<DiscountLDto> Handle(GetDiscountQuery request, CancellationToken cancellationToken)
    {
        var ent = await repositoryWrapper.SetRepository<Discount>().TableNoTracking
            .Where(b => b.Id == request.Id)
            .Select(DiscountMapper.ProjectToLDto)
            .FirstOrDefaultAsync(cancellationToken);
        if (ent == null)
            throw new AppException("Discount NotFound", ApiResultStatusCode.NotFound);

        return ent;
    }
}