-
Notifications
You must be signed in to change notification settings - Fork 0
feat: 검색 기능 구현 #130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: 검색 기능 구현 #130
Conversation
bbbbooo
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
고생하셨습니다.
| 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)); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
현재 코드에서는 SearchField 조건이 추가될 경우 else if를 사용해야 해서 코드 복잡도가 증가할거같아요.
검색 조건별로 코드를 별도의 클래스로 분리하여, 조건에 따라 실행할 로직을 동적으로 결정할 수 있는 구조는 어떻게 생각하시나요? 전략 패턴과 유사한 느낌입니다
#️⃣연관된 이슈
📝작업 내용
목표로 검색하는 경우 검색어가 포함된 목표가 여러개가 있으면 유저와 목표를 여러번 반환하는 문제가 있었습니다.
그래서
Map을 사용해 검색 결과 목표가 여러개더라도 유저가 같으면 한 번만 반환하도록 하였습니다.유저명,목표명으로 딱 두 개라서enum을 사용해 안정성을 높였습니다.스크린샷 (선택)
💬리뷰 요구사항(선택)