Skip to content

Commit

Permalink
feat: improve search endpoints using union all (V2 versions)
Browse files Browse the repository at this point in the history
  • Loading branch information
BettyB979 committed Dec 6, 2024
1 parent a22798e commit 322d4be
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,26 @@ public Page<SearchContactDto> searchContacts(

}

@GetMapping(path = Constants.API_CONTACTS_SEARCH+"/V2", produces = "application/json")
@Operation(summary = "Search contact by parameter (identifier, email, name, firstName lastName)")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK", content = @Content(array = @ArraySchema(schema = @Schema(implementation = SearchContactDto.class)))),
@ApiResponse(responseCode = "400", description = "Bad Request")
})
public Page<SearchContactDto> searchContactByParam(
@RequestParam(required = true) String searchParam,
@RequestParam(defaultValue = "0") Integer page,
@RequestParam(defaultValue = "10") Integer pageSize) {

log.info(
"Search contact with param = {} page = {} pageSize = {}", searchParam, page, pageSize);

Pageable pageable = PageRequest.of(page, pageSize);

return contactService.searchContactByParam(searchParam.toUpperCase(), pageable);

}


static class ContactPage extends PageImpl<ContactDto> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,26 @@ public SurveyUnitDetailsDto findSurveyUnit(@PathVariable("id") String id) {

}

@Operation(summary = "Multi-criteria search survey-unit")
@GetMapping(value = Constants.API_SURVEY_UNITS_SEARCH+"/V2", produces = "application/json")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "OK", content = @Content(array = @ArraySchema(schema = @Schema(implementation = SearchSurveyUnitDto.class)))),
@ApiResponse(responseCode = "404", description = "Not found"),
@ApiResponse(responseCode = "400", description = "Bad Request")
})
public Page<SearchSurveyUnitDto> searchSurveyUnitsByParam(
@RequestParam(required = true) String searchParam,
@RequestParam(defaultValue = "0") Integer page,
@RequestParam(defaultValue = "20") Integer pageSize) {
log.info(
"Search surveyUnit ith param = {} page = {} pageSize = {}", searchParam, page, pageSize);

Pageable pageable = PageRequest.of(page, pageSize);

return surveyUnitService.findByParameter(searchParam, pageable);
}


@Operation(summary = "Create or update a survey unit")
@PutMapping(value = Constants.API_SURVEY_UNITS_ID, produces = "application/json", consumes = "application/json")
@ApiResponses(value = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
@AutoConfigureMockMvc
@SpringBootTest
@ActiveProfiles("test")
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
class SurveyUnitControllerTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,52 +18,107 @@ public interface ContactRepository extends PagingAndSortingRepository<Contact, S

@Query(nativeQuery = true, value = "SELECT identifier FROM contact TABLESAMPLE system_rows(1)")
String findRandomIdentifierContact();

@Query(
value = """
SELECT
c.identifier as identifier,
c.email as email,
c.first_name as firstName,
c.last_name as lastName
FROM
contact c
WHERE
UPPER(c.identifier) LIKE :param || '%'
""",
SELECT
c.identifier as identifier,
c.email as email,
c.first_name as firstName,
c.last_name as lastName
FROM
contact c
WHERE
UPPER(c.identifier) LIKE :param || '%'
""",
nativeQuery = true
)
Page<SearchContactDto> findByIdentifier( String param, Pageable pageable);
Page<SearchContactDto> findByIdentifier(String param, Pageable pageable);

@Query(
value = """
SELECT
c.identifier as identifier,
c.email as email,
c.first_name as firstName,
c.last_name as lastName
FROM
contact c
WHERE
UPPER(c.email) LIKE :param || '%'
""",
SELECT
c.identifier as identifier,
c.email as email,
c.first_name as firstName,
c.last_name as lastName
FROM
contact c
WHERE
UPPER(c.email) LIKE :param || '%'
""",
nativeQuery = true
)
Page<SearchContactDto> findByEmail( String param, Pageable pageable);
Page<SearchContactDto> findByEmail(String param, Pageable pageable);

@Query(
value = """
SELECT
c.identifier as identifier,
c.email as email,
c.first_name as firstName,
c.last_name as lastName
FROM
contact c
WHERE
UPPER(c.last_name) LIKE :param || '%'
OR UPPER(first_name || ' ' || last_name) LIKE :param || '%'
""",
SELECT
c.identifier as identifier,
c.email as email,
c.first_name as firstName,
c.last_name as lastName
FROM
contact c
WHERE
UPPER(c.last_name) LIKE :param || '%'
OR UPPER(first_name || ' ' || last_name) LIKE :param || '%'
""",
nativeQuery = true
)
Page<SearchContactDto> findByFirstNameLastName(String param, Pageable pageable);

@Query(
value = """
SELECT
*
FROM
contact c
WHERE
UPPER(c.last_name) LIKE :param || '%'
UNION ALL
SELECT
*
FROM
contact c
WHERE
UPPER(first_name || ' ' || last_name) LIKE :param || '%'
UNION ALL
SELECT
*
FROM
contact c
WHERE
UPPER(c.email) LIKE :param || '%'
UNION ALL
SELECT
*
FROM
contact c
WHERE
UPPER(c.identifier) LIKE :param || '%'
""",
countQuery = """
SELECT COUNT(*)
FROM (
SELECT 1
FROM contact c
WHERE UPPER(c.last_name) LIKE :param || '%'
UNION ALL
SELECT 1
FROM contact c
WHERE UPPER(c.first_name || ' ' || c.last_name) LIKE :param || '%'
UNION ALL
SELECT 1
FROM contact c
WHERE UPPER(c.email) LIKE :param || '%'
UNION ALL
SELECT 1
FROM contact c
WHERE UPPER(c.identifier) LIKE :param || '%'
) AS count_query""",
nativeQuery = true
)
Page<SearchContactDto> findByParam(String param, Pageable pageable);

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class SurveyUnit {
private String label;


@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private SurveyUnitAddress surveyUnitAddress;

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "surveyUnit" )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface SurveyUnitRepository extends JpaRepository<SurveyUnit, String>
FROM
survey_unit su
WHERE
UPPER(su.id_su) LIKE CONCAT(UPPER(:param), '%')
UPPER(su.id_su) LIKE :param || '%'
""")
Page<SearchSurveyUnitDto> findByIdentifier(String param, Pageable pageable);

Expand All @@ -29,8 +29,8 @@ public interface SurveyUnitRepository extends JpaRepository<SurveyUnit, String>
FROM
survey_unit su
WHERE
UPPER(su.identification_code) LIKE CONCAT(UPPER(:param), '%')
UPPER(su.identification_code) LIKE :param || '%'
""")
Page<SearchSurveyUnitDto> findByIdentificationCode(String param, Pageable pageable);

Expand All @@ -40,8 +40,45 @@ public interface SurveyUnitRepository extends JpaRepository<SurveyUnit, String>
FROM
survey_unit su
WHERE
UPPER(su.identification_name) LIKE CONCAT(UPPER(:param), '%')
UPPER(su.identification_name) LIKE :param || '%'
""")
Page<SearchSurveyUnitDto> findByIdentificationName(String param, Pageable pageable);

@Query(nativeQuery = true,
value = """
SELECT
*
FROM
survey_unit su
WHERE
UPPER(su.id_su) LIKE :param || '%'
UNION ALL
SELECT
*
FROM
survey_unit su
WHERE
UPPER(su.identification_name) LIKE :param || '%'
UNION ALL
SELECT
*
FROM
survey_unit su
WHERE
UPPER(su.identification_code) LIKE :param || '%'
""",
countQuery = """
SELECT COUNT(*)
FROM (
SELECT 1 FROM survey_unit su
WHERE UPPER(su.id_su) LIKE :param || '%'
UNION ALL
SELECT 1 FROM survey_unit su
WHERE UPPER(su.identification_name) LIKE :param || '%'
UNION ALL
SELECT 1 FROM survey_unit su
WHERE UPPER(su.identification_code) LIKE :param || '%'
) AS count_query""")
Page<SearchSurveyUnitDto> findByParam(String param, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ public interface ContactService {

Contact updateOrCreateContact(String id, ContactDto contactDto, JsonNode payload);

Page<SearchContactDto> searchContactByParam(String param, Pageable pageable);

Contact createContactAddressEvent(Contact contact, JsonNode payload);

Contact updateContactAddressEvent(Contact contact, JsonNode payload) throws NotFoundException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ public Page<SearchContactDto> searchContactByName(String name, Pageable pageable
return contactRepository.findByFirstNameLastName(name, pageable);
}

@Override
public Page<SearchContactDto> searchContactByParam(String param, Pageable pageable) {
return contactRepository.findByParam(param, pageable);
}




@Override
public Contact createContactAddressEvent(Contact contact, JsonNode payload) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ public interface SurveyUnitService {

void deleteSurveyUnit(String id);

Page<SearchSurveyUnitDto> findByParameter(String searchParam, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,9 @@ public void deleteSurveyUnit(String id) {

}

@Override
public Page<SearchSurveyUnitDto> findByParameter(String searchParam, Pageable pageable) {
return surveyUnitRepository.findByParam(searchParam.toUpperCase(), pageable);
}

}

0 comments on commit 322d4be

Please sign in to comment.