Conversation
pparkjs
left a comment
There was a problem hiding this comment.
제가 구현한 사용자 검색API에 팔로잉 유무 응답 추가해주셔서 감사합니다!
다만 제가 제안 드린 방식도 가능한지 검토 한 번 해주시면 더할나위 없을 거 같습니다!
| public static UserSearchResponse createWithoutLogin(List<UserSearchResult> userSearchResults, Long totalCount, Boolean hasNext) { | ||
| return new UserSearchResponse( | ||
| userSearchResults.stream().map(UserDto::from).toList(), | ||
| totalCount, | ||
| hasNext | ||
| ); | ||
| } | ||
|
|
||
| public static UserSearchResponse createWithLogin(Map<UserSearchResult, Boolean> 팔로우_유무, Long totalCount, Boolean hasNext) { | ||
| return new UserSearchResponse( | ||
| 팔로우_유무.entrySet().stream().map(UserDto::fromEntry).toList(), | ||
| totalCount, | ||
| hasNext | ||
| ); | ||
| } |
| Map<UserSearchResult, Boolean> 팔로우_유무 = searchResult.getContent().stream() | ||
| .collect(toMap( | ||
| Function.identity(), | ||
| userSearchResult -> { | ||
| User 검색_대상_유저 = userRepository.getById(userSearchResult.getId()); | ||
| return followRepository.existsByFollowerUserAndFollowingUser(검색하는_유저, 검색_대상_유저); | ||
| } | ||
| )); | ||
| return UserSearchResponse.createWithLogin(팔로우_유무, count, searchResult.hasNext()); |
There was a problem hiding this comment.
이거 생각해봤는데 반복문마다 쿼리 돌리는것보다 코드 레벨에서 해결하는게 더 좋을 거 같아서요
-> 검색하는_유저와 searchResult에서 추출한 검색 당한 유저 Ids로 In절 을 통해 내가 팔로우하고 있는 유저Id 리스트 가져오는 쿼리(내가 팔로우하고 있는 유저 아이디 리스트) 하나 만든 후에 내가 팔로우하고 있는 유저 아이디 리스트를 toMap으로 key는 userId로 value는 true로 만들어서 저기 userSearchResult -> 내가_팔로우_하고_있는_유저_리스트.getOrDefault(userSearchResult.getId()), false)
이런식으로하면 어떤가요!? 될련지는 모르겠는데
There was a problem hiding this comment.
저도 코드 레벨에서 해결하는 것이 좋다고 생각되어서 수정해 보았습니다.
좋은 리뷰 감사합니다 !! 😊
There was a problem hiding this comment.
역시 동호님 제가 딱 바라던 방식으로 수정해주셨네요 ㅎㅎ
There was a problem hiding this comment.
변수명이 조금 긴 것 같은데, 어떤가요??
개인적으로는 조금 길어도 변수명 그 자체로 코드 설명이 되는 게 좋다고 생각합니다만 개선이 필요할까요?
There was a problem hiding this comment.
변수명은 개발자가 햇갈리지 않게 바로 보자마자 알 수 있도록 하는게 좋을 거 같습니다! 이대로 가시죠
그리고 한가지 더 제안 드리는 것은 contains 사용인데
ArrayList, HashSet의 contains는 시간복잡도가 O(n), O(1) 차이가 있어 contains를 쓸때는 HashSet을 이용해서 하는게 더 좋을 거 같습니다!
고로 검색하는_유저가_팔로우하고_있는_검색_결과_유저_ID_리스트를 toList가 아닌 toSet으로 하는걸 제안드립니다!
참고 링크
https://www.baeldung.com/java-hashset-arraylist-contains-performance
There was a problem hiding this comment.
아니면 더 나아가 아싸리 follow entity 전체를 조회하는 쿼리에서 follow전체를 가져오는 것보다 HashSet을 리턴타입으로 하여 f.id만 가져오도록 하는게 더 깔끔할 거 같네요
- 검색을 하는 유저가, 검색 대상 유저를 팔로우 하고 있는 지 여부를 포함
pparkjs
left a comment
There was a problem hiding this comment.
반영해주시느라 고생하셨습니다! Approve 드릴게요!
Description
검색을 하는 유저가, 검색 대상 유저를 팔로우하고 있는지 여부를 포함하도록 수정했습니다.
Relation Issues