public ActionResult UpdatePatient(PatientDto patientRequestModel) { if (patientRequestModel != null) { try { if (patientRequestModel.Patient != null) { patientService.UpdatePatient(patientRequestModel.Patient); } else { return(Ok(GetResponse(ResponseType.FAIL, ResponseStatusCode.FAIL, GetError(ErrorCodes.dataNotFound, "Failed", "Patient details are missing")))); } //Update the test results var patientTestResults = testService.GetTestResults().Where(x => x.PatientId == patientRequestModel.Patient.Id).ToList(); if (patientRequestModel.SelectedTestTitles != null && patientRequestModel.SelectedTestTitles.Count > 0) { //Add the tests with if the title is not already present foreach (var title in patientRequestModel.SelectedTestTitles) { var testResults = patientTestResults.FindAll(x => x.TitleId == title.Id); if (testResults != null && testResults.Count == 0) { var otherTests = testService.GetOtherTests().Where(otherTest => otherTest.TestGroupId == title.GroupId && otherTest.TestTitleId == title.Id).ToList(); if (otherTests != null && otherTests.Count > 0) { foreach (var test in otherTests) { testService.InsertTestResult(new TestResults().GetTestResult(test, patientRequestModel.Patient)); } } } } //Delete the remaining test results foreach (var testResult in patientTestResults) { var testTitle = patientRequestModel.SelectedTestTitles.FindAll(x => x.Id == testResult.TitleId); if (testTitle != null && testTitle.Count == 0) { testService.DeleteTestResult(testResult.Id); } } } else { //Delete all the test results for this patient if (patientTestResults != null && patientTestResults.Count > 0) { foreach (var testResult in patientTestResults) { testService.DeleteTestResult(testResult.Id); } } } } catch (Exception e) { Program.Logger.Error(e); return(Ok(GetResponse(ResponseType.FAIL, ResponseStatusCode.FAIL, GetError(ErrorCodes.dataNotFound, "Failed", "Error occurred while updating the test group")))); } return(Ok(GetResponse(ResponseType.ACK, ResponseStatusCode.SUCCESS))); } else { return(BadRequest(GetResponse(ResponseType.ERROR, ResponseStatusCode.ERROR, GetError(ErrorCodes.invalidData, "Invalid input", "Please enter proper patient details")))); } }