Api-PWA/DocuMed.Repository/Handlers/Patients/GetPatientQueryHandler.cs

37 lines
1.6 KiB
C#

namespace DocuMed.Repository.Handlers.Patients;
public class GetPatientQueryHandler(IRepositoryWrapper repositoryWrapper,UserManager<ApplicationUser> userManager) : IRequestHandler<GetPatientQuery, PatientSDto>
{
public async Task<PatientSDto> Handle(GetPatientQuery request, CancellationToken cancellationToken)
{
if (request.Id != null)
{
var ent = await repositoryWrapper.SetRepository<Patient>()
.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<Patient>()
.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");
}
}