public async Task <PatientResponse> AddPatientRiskFactor(AddPatientRiskFactorRequest request) { if ((request?.PatientId == null) || (request?.RiskFactorId == null)) { throw new ArgumentNullException(); } Patient patient = await _patientRepository.GetAsync(request.PatientId); RiskFactor riskFactor = await _riskFactorRepository.GetAsync(request.RiskFactorId); PatientRiskFactor patientRiskFactor = new PatientRiskFactor { //Přes Id to funguje, přes celé entity ne!!! PatientId = patient.Id, RiskFactorId = riskFactor.Id }; _patientRiskFactorRepository.Add(patientRiskFactor); await _patientRiskFactorRepository.UnitOfWork.SaveChangesAsync(); //aktualizace pacienta patient = await _patientRepository.GetAsync(request.PatientId); return(_patientMapper.Map(patient)); }
public async Task <PatientResponse> EditPatientAsync(EditPatientRequest request) { var existingRecord = await _patientRepository.GetAsync(request.Id); if (existingRecord == null) { throw new ArgumentException($"Entity with {request.Id} does not exist!"); } var entity = _patientMapper.Map(request); var result = _patientRepository.Update(entity); // Delete old records in joining table for existing record: if (existingRecord.PatientRiskFactors != null) { foreach (PatientRiskFactor patientRiskFactor in existingRecord.PatientRiskFactors) { DeletePatientRiskFactorRequest deletePRF = new DeletePatientRiskFactorRequest { PatientId = patientRiskFactor.PatientId, RiskFactorId = patientRiskFactor.RiskFactorId }; await _patientRiskFactorService.DeletePatientRiskFactor(deletePRF); } } // Create new records in joining table: if (request.RiskFactors != null) { foreach (GetRiskFactorRequest riskFactor in request.RiskFactors) { AddPatientRiskFactorRequest addPRFRequest = new AddPatientRiskFactorRequest { PatientId = request.Id, RiskFactorId = riskFactor.Id }; await _patientRiskFactorService.AddPatientRiskFactor(addPRFRequest); } } // Confirm changes: await _patientRepository.UnitOfWork.SaveChangesAsync(); return(_patientMapper.Map(result)); }
public async Task <PatientResponse> AddPatientAsync(AddPatientRequest request) { var patient = _patientMapper.Map(request); Patient result = _patientRepository.Add(patient); await _patientRepository.UnitOfWork.SaveChangesAsync(); // Create new records in joining table: if (request.RiskFactors != null && result != null) { foreach (GetRiskFactorRequest riskFactor in request.RiskFactors) { AddPatientRiskFactorRequest addPRFRequest = new AddPatientRiskFactorRequest { PatientId = result.Id, RiskFactorId = riskFactor.Id }; await _patientRiskFactorService.AddPatientRiskFactor(addPRFRequest); } } return(_patientMapper.Map(result)); }
public async Task <IActionResult> Post(AddPatientRiskFactorRequest request) { return(Ok(await _patientRiskFactorService.AddPatientRiskFactor(request))); }