namespace DocuMed.Repository.Handlers.Patients; public class UpdatePatientSectionCommandHandler(IRepositoryWrapper repositoryWrapper) : IRequestHandler { public async Task Handle(UpdatePatientSectionCommand request, CancellationToken cancellationToken) { var patient = await repositoryWrapper.SetRepository() .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
() .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().Update(newEnt); await repositoryWrapper.SaveChangesAsync(cancellationToken); return newEnt.Id; } }