diff --git a/api/src/main/java/org/openmrs/module/emrapi/db/HibernateVisitDAO.java b/api/src/main/java/org/openmrs/module/emrapi/db/HibernateVisitDAO.java index afcde80c..7d13c4db 100644 --- a/api/src/main/java/org/openmrs/module/emrapi/db/HibernateVisitDAO.java +++ b/api/src/main/java/org/openmrs/module/emrapi/db/HibernateVisitDAO.java @@ -17,13 +17,6 @@ public class HibernateVisitDAO implements VisitDAO { @Autowired private SessionFactory sessionFactory; - -// public void setSessionFactory(SessionFactory sessionFactory) { -// this.sessionFactory = sessionFactory; -// } - - - @Override public List getVisitsByPatientId(Patient patient) { String visitNoteEncounterTypeUuid = "d7151f82-c1f3-4152-a605-2f9ea7414a79"; @@ -62,22 +55,11 @@ public List getVisitsByPatientId(Patient patient) { .add(diagnosis); } - // List visitWithDiagnoses = visits.stream() .sorted(Comparator.comparing(Visit::getStartDatetime).reversed()) .map(visit -> new VisitWithDiagnoses(visit, visitToDiagnosesMap.getOrDefault(visit, new HashSet<>()))) .collect(Collectors.toList()); return visitWithDiagnoses; - - -// String queryString = "FROM Visit v WHERE v.patient.id = :patient_id"; -// -// Query query = sessionFactory.getCurrentSession().createQuery(queryString); -// query.setParameter("patient_id", patient.getId()); -// -// List list = query.list(); -// return list; - } } diff --git a/api/src/main/java/org/openmrs/module/emrapi/visit/VisitWithDiagnosesService.java b/api/src/main/java/org/openmrs/module/emrapi/visit/VisitWithDiagnosesService.java index f26b2842..91d35e23 100644 --- a/api/src/main/java/org/openmrs/module/emrapi/visit/VisitWithDiagnosesService.java +++ b/api/src/main/java/org/openmrs/module/emrapi/visit/VisitWithDiagnosesService.java @@ -6,8 +6,8 @@ public interface VisitWithDiagnosesService { /** * Fetches visits of a patient by patient ID. * - * @param patientId the ID of the patient + * @param patientUuid the UUID of the patient * @return a list of visits */ - List getVisitsByPatientId(Integer patientId); -} \ No newline at end of file + List getVisitsByPatientId(String patientUuid); +} diff --git a/api/src/main/java/org/openmrs/module/emrapi/visit/VisitWithDiagnosesServiceImpl.java b/api/src/main/java/org/openmrs/module/emrapi/visit/VisitWithDiagnosesServiceImpl.java index df36422e..c44804fb 100644 --- a/api/src/main/java/org/openmrs/module/emrapi/visit/VisitWithDiagnosesServiceImpl.java +++ b/api/src/main/java/org/openmrs/module/emrapi/visit/VisitWithDiagnosesServiceImpl.java @@ -1,5 +1,6 @@ package org.openmrs.module.emrapi.visit; +import org.hibernate.ObjectNotFoundException; import org.openmrs.Patient; import org.openmrs.api.PatientService; import org.openmrs.api.impl.BaseOpenmrsService; @@ -11,7 +12,6 @@ @Service public class VisitWithDiagnosesServiceImpl extends BaseOpenmrsService implements VisitWithDiagnosesService { - // Existing methods and dependencies @Autowired PatientService patientService; @@ -20,11 +20,14 @@ public class VisitWithDiagnosesServiceImpl extends BaseOpenmrsService implements VisitDAO visitDAO; @Override - public List getVisitsByPatientId(Integer patientId) { + public List getVisitsByPatientId(String patientUuid) { - Patient patient = patientService.getPatient(patientId); + Patient patient = patientService.getPatientByUuid(patientUuid); + + if (patient == null) { + throw new ObjectNotFoundException("No patient found with uuid " + patientUuid, Patient.class.getName()); + } return visitDAO.getVisitsByPatientId(patient); -// return null; } -} \ No newline at end of file +} diff --git a/api/src/test/resources/metadata/pastVisitSetup.xml b/api/src/test/resources/metadata/pastVisitSetup.xml index 4515a044..3ed333d3 100644 --- a/api/src/test/resources/metadata/pastVisitSetup.xml +++ b/api/src/test/resources/metadata/pastVisitSetup.xml @@ -2,15 +2,6 @@ - - - - - - - - - diff --git a/omod/src/main/java/org/openmrs/module/emrapi/web/controller/VisitController.java b/omod/src/main/java/org/openmrs/module/emrapi/web/controller/VisitController.java index fd9419a1..cdd38590 100644 --- a/omod/src/main/java/org/openmrs/module/emrapi/web/controller/VisitController.java +++ b/omod/src/main/java/org/openmrs/module/emrapi/web/controller/VisitController.java @@ -1,8 +1,10 @@ package org.openmrs.module.emrapi.web.controller; +import org.hibernate.ObjectNotFoundException; import org.openmrs.module.emrapi.visit.VisitWithDiagnoses; import org.openmrs.module.emrapi.visit.VisitWithDiagnosesService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; @@ -15,12 +17,17 @@ public class VisitController { @Autowired - VisitWithDiagnosesService customVisitService; + VisitWithDiagnosesService visitWithDiagnosesService; - @RequestMapping(method = RequestMethod.GET, value = "/patient/{patientId}") - public ResponseEntity> getVisitsByPatientId(@PathVariable Integer patientId) { - List visits = customVisitService.getVisitsByPatientId(patientId); + @RequestMapping(method = RequestMethod.GET, value = "/rest/**/emrapi/patient/{patientUuid}/visit") + public ResponseEntity getVisitsByPatientId(@PathVariable String patientUuid) { + List visits; + try { + visits = visitWithDiagnosesService.getVisitsByPatientId(patientUuid); + } catch (ObjectNotFoundException e) { + return ResponseEntity.badRequest().body(e.getMessage()); + } return ResponseEntity.ok(visits); } } \ No newline at end of file diff --git a/omod/src/test/java/org/openmrs/module/emrapi/web/controller/VisitControllerTest.java b/omod/src/test/java/org/openmrs/module/emrapi/web/controller/VisitControllerTest.java index d1df7865..d41f8455 100644 --- a/omod/src/test/java/org/openmrs/module/emrapi/web/controller/VisitControllerTest.java +++ b/omod/src/test/java/org/openmrs/module/emrapi/web/controller/VisitControllerTest.java @@ -2,6 +2,7 @@ import org.junit.Before; import org.junit.Test; +import org.openmrs.Diagnosis; import org.openmrs.Encounter; import org.openmrs.api.PatientService; import org.openmrs.module.emrapi.visit.VisitWithDiagnoses; @@ -13,6 +14,7 @@ import java.util.List; import java.util.Set; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; public class VisitControllerTest extends BaseModuleWebContextSensitiveTest { @@ -37,15 +39,49 @@ public void setUp() throws Exception { public void shouldGetVisitsByPatientId() { String visitNoteEncounterTypeUuid = "d7151f82-c1f3-4152-a605-2f9ea7414a79"; + String patientUuid = "8604d42e-3ca8-11e3-bf2b-0d0c09861e97"; + + ResponseEntity> visitsResponse = (ResponseEntity>) visitController.getVisitsByPatientId(patientUuid); + assertNotNull(visitsResponse); + + List visits = visitsResponse.getBody(); - ResponseEntity> visits = visitController.getVisitsByPatientId(109); assertNotNull(visits); - assert visits.getBody().size() == 1; - Set encounters = visits.getBody().get(0).getEncounters(); + assert visits.size() == 2; + + VisitWithDiagnoses firstVisit = visits.get(1); + Set firstVisitEncounters = firstVisit.getEncounters(); + Set firstVisitDiagnoses = firstVisit.getDiagnoses(); + + assert firstVisit.getId() == 1014; + assertEquals(firstVisit.getPatient().getUuid(), patientUuid); + assert firstVisitEncounters.size() == 2; + assert firstVisitDiagnoses.size() == 3; - for (Encounter encounter : encounters) { - System.out.println(encounter.getEncounterType().getUuid()); + for (Encounter encounter : firstVisitEncounters) { assert encounter.getEncounterType().getUuid().equals(visitNoteEncounterTypeUuid); } + + VisitWithDiagnoses secondVisit = visits.get(0); + Set secondVisitEncounters = secondVisit.getEncounters(); + Set secondVisitDiagnoses = secondVisit.getDiagnoses(); + + assert secondVisit.getId() == 1015; + assertEquals(secondVisit.getPatient().getUuid(), patientUuid); + assert secondVisitEncounters.size() == 1; + assert secondVisitDiagnoses.size() == 2; + + for (Encounter encounter : secondVisitEncounters) { + assert encounter.getEncounterType().getUuid().equals(visitNoteEncounterTypeUuid); + } + + } + + @Test + public void shouldThrowExceptionWhenPatientUuidIsInvalid() { + String invalidPatientUuid = "invalid-uuid"; + ResponseEntity visitsResponse = visitController.getVisitsByPatientId(invalidPatientUuid); + assertNotNull(visitsResponse); + assert visitsResponse.getStatusCode().is4xxClientError(); } } \ No newline at end of file diff --git a/omod/src/test/resources/pastVisitSetup.xml b/omod/src/test/resources/pastVisitSetup.xml index 4515a044..03ad5d6a 100644 --- a/omod/src/test/resources/pastVisitSetup.xml +++ b/omod/src/test/resources/pastVisitSetup.xml @@ -2,43 +2,58 @@ - - - - - - - - - + - - - - - + - + + + - + - + - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/omod/src/test/resources/test-hibernate.cfg.xml b/omod/src/test/resources/test-hibernate.cfg.xml index 83264f49..7ad80b99 100644 --- a/omod/src/test/resources/test-hibernate.cfg.xml +++ b/omod/src/test/resources/test-hibernate.cfg.xml @@ -5,17 +5,6 @@ - true - - - true - - - true - - - TRACE -