using SIGCM2.Application.Abstractions;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.TiposDeIva.Dtos;
using SIGCM2.Domain.Exceptions;

namespace SIGCM2.Application.TiposDeIva.GetById;

public sealed class GetTipoDeIvaByIdQueryHandler : ICommandHandler<GetTipoDeIvaByIdQuery, TipoDeIvaDto>
{
    private readonly ITipoDeIvaRepository _repo;

    public GetTipoDeIvaByIdQueryHandler(ITipoDeIvaRepository repo)
    {
        _repo = repo;
    }

    public async Task<TipoDeIvaDto> Handle(GetTipoDeIvaByIdQuery query)
    {
        var entity = await _repo.GetByIdAsync(query.Id)
            ?? throw new TipoDeIvaNotFoundException(query.Id);

        return TipoDeIvaMapper.ToDto(entity);
    }
}
