namespace Brizco.Repository.Handlers.Position;

public class GetPositionQueryHandler : IRequestHandler<GetPositionQuery, PositionLDto>
{
    private readonly IRepositoryWrapper _repositoryWrapper;

    public GetPositionQueryHandler(IRepositoryWrapper repositoryWrapper)
    {
        _repositoryWrapper = repositoryWrapper;
    }
    public async Task<PositionLDto> Handle(GetPositionQuery request, CancellationToken cancellationToken)
    {
        var shift = await _repositoryWrapper.SetRepository<Domain.Entities.Complex.Position>()
            .TableNoTracking
            .Where(s => s.Id == request.Id)
            .Select(PositionMapper.ProjectToLDto)
            .FirstOrDefaultAsync(cancellationToken);

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