public async Task <ActionResult <PatientViewModel> > PostPatient(PatientViewModel patient) { var p = patient.ConvertToPatient(); var newPatient = await _healthService.CreatePatient(p); //_context.Patients.Add(newPatient); //await _context.SaveChangesAsync(); //newPatient.PatientAilments = new List<PatientAilments>(); //newPatient.PatientMedications = new List<PatientMedications>(); //foreach (var ailment in patient.Ailments) //{ // var Ailment = await _context.Ailments.FirstOrDefaultAsync(a => a.Id == ailment.Id); // var patientAilment = new PatientAilments() {Patient = newPatient, PatientId = newPatient.PatientId, Ailment = Ailment, AilmentId = Ailment.Id }; // newPatient.PatientAilments.Add(patientAilment); //} //foreach (var medication in patient.Medications) //{ // var Medication = await _context.Medications.FirstOrDefaultAsync(m => m.Id == medication.Id); // var patientMedication = new PatientMedications() { Patient = newPatient, PatientId = newPatient.PatientId, Medication = Medication, MedicationId = Medication.Id }; // newPatient.PatientMedications.Add(patientMedication); //} //_context.Patients.Update(newPatient); //await _context.SaveChangesAsync(); return(new PatientViewModel(newPatient)); //CreatedAtAction("GetPatient", new { id = patient.PatientId }, patient); }