namespace Brizco.Repository.Handlers.Section;

public class GetSectionQueryHandler : IRequestHandler<GetSectionQuery, SectionLDto>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public GetSectionQueryHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }
    public async Task<SectionLDto> Handle(GetSectionQuery request, CancellationToken cancellationToken)
    {
        var shift = await _repositoryWrapper.SetRepository<Domain.Entities.Complex.Section>()
            .TableNoTracking
            .Where(s => s.Id == request.Id)
            .Select(SectionMapper.ProjectToLDto)
            .FirstOrDefaultAsync(cancellationToken);

        if (shift == null)
            throw new AppException("Section not found", ApiResultStatusCode.NotFound);
        return shift;
    }
}