Skip to content

Conversation

@soheeparklee
Copy link
Contributor

#️⃣연관된 이슈

ex) #이슈번호, #이슈번호

📝작업 내용

  • 유저 또는 목표 검색 필드와 검색하는 키워드를 받아 검색합니다.
  • 검색어의 경우 유저는 유저 이름 중에서, 목표는 목표 제목에 포함되었는지 검색합니다. 이는 JPA 네이밍으로 쉽게 해결했습니다.
  • 두 경우 모두 같은 결과(유저 + 목표)를 반환합니다. (디자인 변경이 필요할 것 같습니다)
  • 유저로 검색해서 목표를 받아오는 검색은 별로 문제가 없었는데,
    목표로 검색하는 경우 검색어가 포함된 목표가 여러개가 있으면 유저와 목표를 여러번 반환하는 문제가 있었습니다.
    그래서 Map을 사용해 검색 결과 목표가 여러개더라도 유저가 같으면 한 번만 반환하도록 하였습니다.
  • 저희가 검색하기로 한 테이블이 유저명, 목표명 으로 딱 두 개라서 enum을 사용해 안정성을 높였습니다.

스크린샷 (선택)

Screenshot 2024-12-26 at 00 58 50

💬리뷰 요구사항(선택)

리뷰어가 특별히 봐주었으면 하는 부분이 있다면 작성해주세요

ex) 메소드 XXX의 이름을 더 잘 짓고 싶은데 혹시 좋은 명칭이 있을까요?

@soheeparklee soheeparklee self-assigned this Dec 26, 2024
@soheeparklee soheeparklee linked an issue Dec 26, 2024 that may be closed by this pull request
2 tasks
Copy link
Member

@bbbbooo bbbbooo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고생하셨습니다.

Comment on lines +46 to +83
if( searchField.equals(SearchField.USER_NAME.getValue()) ){
List<User> searchedUsers = userRepository.findByNameContains(keyword);
if(searchedUsers.isEmpty()) throw new SearchException(ErrorStatus.toErrorStatus("유저에 대한 검색 결과가 없습니다.", NOT_FOUND));

List<ReadSearchResponse> responses = searchedUsers.stream()
.map(user -> {
List<Goal> goals = user.getGoals();
List<ReadGoalSearchResponse> goalsResponses = goals.stream()
.map(ReadGoalSearchResponse::from)
.toList();

return ReadSearchResponse.from(user, goalsResponses);
}).toList();
return responses;

}else if(searchField.equals(SearchField.GOAL_TITLE.getValue())){
List<Goal> searchedGoals = goalRepository.findByGoalTitleContains(keyword);
if(searchedGoals.isEmpty()) throw new SearchException(ErrorStatus.toErrorStatus("목표에 대한 검색 결과가 없습니다.", NOT_FOUND));

Map<User, List<Goal>> userGoalsMap = searchedGoals.stream()
.collect(Collectors.groupingBy(Goal::getUser));

List<ReadSearchResponse> responses = userGoalsMap.entrySet().stream()
.map(entry -> {
User user = entry.getKey();
List<Goal> goals = entry.getValue();

List<ReadGoalSearchResponse> goalsResponses = goals.stream()
.map(ReadGoalSearchResponse::from)
.toList();

return ReadSearchResponse.from(user, goalsResponses);

}).toList();
return responses;
}else{
throw new SearchException(ErrorStatus.toErrorStatus("해당 필드로는 검색할 수 없습니다", BAD_REQUEST));
}
Copy link
Member

@bbbbooo bbbbooo Dec 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

현재 코드에서는 SearchField 조건이 추가될 경우 else if를 사용해야 해서 코드 복잡도가 증가할거같아요.

검색 조건별로 코드를 별도의 클래스로 분리하여, 조건에 따라 실행할 로직을 동적으로 결정할 수 있는 구조는 어떻게 생각하시나요? 전략 패턴과 유사한 느낌입니다

@soheeparklee soheeparklee merged commit 98e065a into develop Dec 26, 2024
1 check passed
@soheeparklee soheeparklee deleted the feature/search branch December 27, 2024 10:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

유저 또는 목표 검색

3 participants