Search in sources :

Example 1 with NotFoundException

use of io.undertree.symptom.exceptions.NotFoundException in project spring-boot-jpa by ssherwood.

the class PatientRepositoryTests method test_PatientRepository_findByPatientId_ExpectExists.

@Test
public void test_PatientRepository_findByPatientId_ExpectExists() throws Exception {
    Patient patient = entityManager.persistFlushFind(new TestPatientBuilder().build());
    Patient aPatient = patientRepository.findByPatientId(patient.getPatientId()).orElseThrow(NotFoundException::new);
    assertThat(aPatient).isEqualTo(patient);
}
Also used : TestPatientBuilder(io.undertree.symptom.domain.TestPatientBuilder) Patient(io.undertree.symptom.domain.Patient) NotFoundException(io.undertree.symptom.exceptions.NotFoundException) Test(org.junit.Test) DataJpaTest(org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest)

Example 2 with NotFoundException

use of io.undertree.symptom.exceptions.NotFoundException in project spring-boot-jpa by ssherwood.

the class PatientController method getPatientsByExample.

/**
 * Returns a "paged" collection of resources matching the input query params using default
 * matching rules for strings of "contains and ignores case".
 * <p>
 * TODO I'm not exactly happy with the resource name "queryByExample".  Need to research more what
 * other APIs look like for this kind of functionality
 *
 * @param paramMap a Map of fields to use to search with
 * @param pageable a Pageable to restrict results
 * @return A paged result of matching Patients
 */
@GetMapping("/search/by-example")
public Page<Patient> getPatientsByExample(@RequestParam Map<String, Object> paramMap, @PageableDefault(size = DEFAULT_PAGE_SZ) Pageable pageable) {
    // naively copies map entries to matching properties in the Patient POJO
    Patient examplePatient = this.jacksonObjectMapper.convertValue(paramMap, Patient.class);
    Page<Patient> pagedResults = this.patientRepository.findAll(Example.of(examplePatient, DEFAULT_MATCHER), pageable);
    if (!pagedResults.hasContent()) {
        throw new NotFoundException(Patient.RESOURCE_PATH, "No Patients found matching example query " + examplePatient);
    }
    return pagedResults;
}
Also used : Patient(io.undertree.symptom.domain.Patient) NotFoundException(io.undertree.symptom.exceptions.NotFoundException) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Aggregations

Patient (io.undertree.symptom.domain.Patient)2 NotFoundException (io.undertree.symptom.exceptions.NotFoundException)2 TestPatientBuilder (io.undertree.symptom.domain.TestPatientBuilder)1 Test (org.junit.Test)1 DataJpaTest (org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1