using Microsoft.EntityFrameworkCore;

namespace Netina.Repository.Handlers.Discounts;

public class GetDiscountQueryHandler : IRequestHandler<GetDiscountQuery, DiscountLDto>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public GetDiscountQueryHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }
    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;
    }
}