Post

TIL(20240816) [UT : 사용성 테스트 피드백 개선 - 검색기능 추가구현]


image

1
2
3
4
5
6
7
8
9
10
@GetMapping("/search")
  public ResponseEntity<CommonResponseDto<PaginationResponse<ChallengeSummaryResponseDto>>> getChallengesBySearch(
	  @RequestParam(value = "title", required = false) String title,
	  @RequestParam(value = "page", defaultValue = "0") int page,
	  @RequestParam(value = "size", defaultValue = "9") int size
  ) {
	PaginationResponse<ChallengeSummaryResponseDto> challengeListBySearch = challengeService.getChallengesBySearch(title, page, size);
	return ResponseEntity.ok().body(new CommonResponseDto<>(
		HttpStatus.OK.value(), "챌린지 제목 검색결과 조회 성공", challengeListBySearch));
  }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Transactional(readOnly = true)
  public PaginationResponse<ChallengeSummaryResponseDto> getChallengesBySearch(String title, int page, int size) {
	Pageable pageable = PageRequest.of(page, size);

	if(title == null) {
	  title = "";
	}

	Page<Challenge> challengeListBySearch = challengeRepository.findByTitleContaining(title, pageable);
	if (challengeListBySearch.isEmpty()) {
	  throw new GlobalException(ErrorCode.NOT_FOUND_CHALLENGE);
	}

	List<ChallengeSummaryResponseDto> challengeList = challengeListBySearch.getContent().stream()
		.map(challenge -> new ChallengeSummaryResponseDto(challenge)).toList();

	return new PaginationResponse<>(
		challengeList,
		challengeListBySearch.getTotalPages(),
		challengeListBySearch.getTotalPages(),
		challengeListBySearch.getNumber(),
		challengeListBySearch.getSize()
	);
  }
This post is licensed under CC BY 4.0 by the author.