namespace DocuMed.Repository.Handlers.Patients; public class GetPatientQueryHandler(IRepositoryWrapper repositoryWrapper,UserManager userManager) : IRequestHandler { public async Task Handle(GetPatientQuery request, CancellationToken cancellationToken) { if (request.Id != null) { var ent = await repositoryWrapper.SetRepository() .TableNoTracking .Where(f => f.Id == request.Id) .Select(PatientMapper.ProjectToSDto) .FirstOrDefaultAsync(cancellationToken); if(ent == null) throw new BaseApiException(ApiResultStatusCode.NotFound, "Patient not found"); return ent; } else if (request.NationalId != null) { var user = await userManager.Users.FirstOrDefaultAsync(f => f.NationalId == request.NationalId, cancellationToken); if (user == null) throw new BaseApiException(ApiResultStatusCode.NotFound, "User with this national id not found"); var ent = await repositoryWrapper.SetRepository() .TableNoTracking .Where(f => f.UserId == user.Id) .Select(PatientMapper.ProjectToSDto) .FirstOrDefaultAsync(cancellationToken); if (ent == null) throw new BaseApiException(ApiResultStatusCode.NotFound, "Patient not found"); return ent; } throw new BaseApiException(ApiResultStatusCode.BadRequest, "Request is not correct - NationalId and Id is null"); } }