Api-PWA/DocuMed.Repository/Handlers/Patients/UpdatePatientSectionCommand...

38 lines
1.5 KiB
C#

namespace DocuMed.Repository.Handlers.Patients;
public class UpdatePatientSectionCommandHandler(IRepositoryWrapper repositoryWrapper) : IRequestHandler<UpdatePatientSectionCommand,Guid>
{
public async Task<Guid> Handle(UpdatePatientSectionCommand request, CancellationToken cancellationToken)
{
var patient = await repositoryWrapper.SetRepository<Patient>()
.TableNoTracking
.Where(f => f.Id == request.Id)
.Select(PatientMapper.ProjectToSDto)
.FirstOrDefaultAsync(cancellationToken);
if (patient == null)
throw new BaseApiException(ApiResultStatusCode.NotFound, "Patient not found");
var section = await repositoryWrapper.SetRepository<Section>()
.TableNoTracking
.FirstOrDefaultAsync(f => f.Id == request.SectionId, cancellationToken);
if (section == null)
throw new BaseApiException(ApiResultStatusCode.NotFound, "Section not found");
if(patient.HospitalId != section.HospitalId)
throw new BaseApiException(ApiResultStatusCode.BadRequest, "Patient and Section hospital is not same");
var newEnt = Patient.Create(
patient.UnitNumber,
patient.Room,
patient.Bed,
patient.AdmissionAt,
section.Id,
patient.HospitalId,
patient.Id);
repositoryWrapper.SetRepository<Patient>().Update(newEnt);
await repositoryWrapper.SaveChangesAsync(cancellationToken);
return newEnt.Id;
}
}