Skip to content

Commit

Permalink
Add pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
jayasanka-sack committed Oct 24, 2024
1 parent 539eb15 commit 612175d
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class HibernateVisitDAO implements VisitDAO {
@Autowired
private SessionFactory sessionFactory;

public List<VisitWithDiagnoses> getVisitsByPatientId(Patient patient) {
public List<VisitWithDiagnoses> getVisitsByPatientId(Patient patient, int startIndex, int limit) {

String visitNoteEncounterTypeUuid = "d7151f82-c1f3-4152-a605-2f9ea7414a79";

Expand All @@ -32,7 +32,8 @@ public List<VisitWithDiagnoses> getVisitsByPatientId(Patient patient) {
"JOIN enc.encounterType et " +
"WHERE v.patient.id = :patientId " +
"AND et.uuid = :encounterTypeUuid " +
"ORDER BY v.startDatetime DESC";
"ORDER BY v.startDatetime DESC "+
"LIMIT :startIndex, :limit";

List<Visit> visits = sessionFactory.getCurrentSession()
.createQuery(hqlVisit)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@

public interface VisitDAO {

List<VisitWithDiagnoses> getVisitsByPatientId(Patient patient);
List<VisitWithDiagnoses> getVisitsByPatientId(Patient patient, int startIndex, int limit);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ public interface VisitWithDiagnosesService {
* @param patientUuid the UUID of the patient
* @return a list of visits
*/
List<VisitWithDiagnoses> getVisitsByPatientId(String patientUuid);
List<VisitWithDiagnoses> getVisitsByPatientId(String patientUuid, int startIndex, int limit);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ public class VisitWithDiagnosesServiceImpl extends BaseOpenmrsService implements
VisitDAO visitDAO;

@Override
public List<VisitWithDiagnoses> getVisitsByPatientId(String patientUuid) {
public List<VisitWithDiagnoses> getVisitsByPatientId(String patientUuid, int startIndex, int limit) {

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 visitDAO.getVisitsByPatientId(patient, startIndex, limit);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import org.hibernate.ObjectNotFoundException;
import org.openmrs.module.emrapi.visit.VisitWithDiagnoses;
import org.openmrs.module.emrapi.visit.VisitWithDiagnosesService;
import org.openmrs.module.webservices.rest.web.RequestContext;
import org.openmrs.module.webservices.rest.web.RestUtil;
import org.openmrs.module.webservices.rest.web.representation.Representation;
import org.openmrs.module.webservices.rest.web.resource.impl.NeedsPaging;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -11,23 +15,30 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class VisitController {

@Autowired
VisitWithDiagnosesService visitWithDiagnosesService;



@RequestMapping(method = RequestMethod.GET, value = "/rest/**/emrapi/patient/{patientUuid}/visit")
public ResponseEntity<?> getVisitsByPatientId(@PathVariable String patientUuid) {
public ResponseEntity<?> getVisitsByPatientId(
HttpServletRequest request,
HttpServletResponse response,
@PathVariable String patientUuid) {
RequestContext context = RestUtil.getRequestContext(request, response, Representation.DEFAULT);
List<VisitWithDiagnoses> visits;
try {
visits = visitWithDiagnosesService.getVisitsByPatientId(patientUuid);
} catch (ObjectNotFoundException e) {
visits = visitWithDiagnosesService.getVisitsByPatientId(patientUuid, context.getStartIndex(), context.getLimit());
}
catch (ObjectNotFoundException e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
return ResponseEntity.ok(visits);

return new ResponseEntity<>(new NeedsPaging<>(visits, context), HttpStatus.OK);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public void shouldGetVisitsByPatientId() {
String visitNoteEncounterTypeUuid = "d7151f82-c1f3-4152-a605-2f9ea7414a79";
String patientUuid = "8604d42e-3ca8-11e3-bf2b-0d0c09861e97";

ResponseEntity<List<VisitWithDiagnoses>> visitsResponse = (ResponseEntity<List<VisitWithDiagnoses>>) visitController.getVisitsByPatientId(patientUuid);
ResponseEntity<List<VisitWithDiagnoses>> visitsResponse = (ResponseEntity<List<VisitWithDiagnoses>>)
visitController.getVisitsByPatientId(patientUuid);
assertNotNull(visitsResponse);

List<VisitWithDiagnoses> visits = visitsResponse.getBody();
Expand Down Expand Up @@ -84,4 +85,4 @@ public void shouldThrowExceptionWhenPatientUuidIsInvalid() {
assertNotNull(visitsResponse);
assert visitsResponse.getStatusCode().is4xxClientError();
}
}
}

0 comments on commit 612175d

Please sign in to comment.