Skip to content

feat: 사용자 검색 API 응답값 수정#348

Merged
kdkdhoho merged 3 commits intodevfrom
feat/343
Jan 28, 2025
Merged

feat: 사용자 검색 API 응답값 수정#348
kdkdhoho merged 3 commits intodevfrom
feat/343

Conversation

@kdkdhoho
Copy link
Collaborator

Description

검색을 하는 유저가, 검색 대상 유저를 팔로우하고 있는지 여부를 포함하도록 수정했습니다.

Relation Issues

@kdkdhoho kdkdhoho self-assigned this Jan 27, 2025
@kdkdhoho kdkdhoho requested a review from pparkjs as a code owner January 27, 2025 03:50
@kdkdhoho kdkdhoho linked an issue Jan 27, 2025 that may be closed by this pull request
Copy link
Collaborator

@pparkjs pparkjs left a comment

Choose a reason for hiding this comment

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

제가 구현한 사용자 검색API에 팔로잉 유무 응답 추가해주셔서 감사합니다!

다만 제가 제안 드린 방식도 가능한지 검토 한 번 해주시면 더할나위 없을 거 같습니다!

Comment on lines +12 to +26
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
);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

비회원이랑 회원 나눈거 좋네요!

Comment on lines 72 to 80
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());
Copy link
Collaborator

Choose a reason for hiding this comment

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

이거 생각해봤는데 반복문마다 쿼리 돌리는것보다 코드 레벨에서 해결하는게 더 좋을 거 같아서요

-> 검색하는_유저와 searchResult에서 추출한 검색 당한 유저 Ids로 In절 을 통해 내가 팔로우하고 있는 유저Id 리스트 가져오는 쿼리(내가 팔로우하고 있는 유저 아이디 리스트) 하나 만든 후에 내가 팔로우하고 있는 유저 아이디 리스트를 toMap으로 key는 userId로 value는 true로 만들어서 저기 userSearchResult -> 내가_팔로우_하고_있는_유저_리스트.getOrDefault(userSearchResult.getId()), false)

이런식으로하면 어떤가요!? 될련지는 모르겠는데

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

저도 코드 레벨에서 해결하는 것이 좋다고 생각되어서 수정해 보았습니다.
좋은 리뷰 감사합니다 !! 😊

Copy link
Collaborator

Choose a reason for hiding this comment

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

역시 동호님 제가 딱 바라던 방식으로 수정해주셨네요 ㅎㅎ

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

변수명이 조금 긴 것 같은데, 어떤가요??
개인적으로는 조금 길어도 변수명 그 자체로 코드 설명이 되는 게 좋다고 생각합니다만 개선이 필요할까요?

Copy link
Collaborator

@pparkjs pparkjs Jan 28, 2025

Choose a reason for hiding this comment

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

변수명은 개발자가 햇갈리지 않게 바로 보자마자 알 수 있도록 하는게 좋을 거 같습니다! 이대로 가시죠

그리고 한가지 더 제안 드리는 것은 contains 사용인데
ArrayList, HashSet의 contains는 시간복잡도가 O(n), O(1) 차이가 있어 contains를 쓸때는 HashSet을 이용해서 하는게 더 좋을 거 같습니다!
고로 검색하는_유저가_팔로우하고_있는_검색_결과_유저_ID_리스트를 toList가 아닌 toSet으로 하는걸 제안드립니다!

참고 링크
https://www.baeldung.com/java-hashset-arraylist-contains-performance

Copy link
Collaborator

@pparkjs pparkjs Jan 28, 2025

Choose a reason for hiding this comment

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

아니면 더 나아가 아싸리 follow entity 전체를 조회하는 쿼리에서 follow전체를 가져오는 것보다 HashSet을 리턴타입으로 하여 f.id만 가져오도록 하는게 더 깔끔할 거 같네요

- 검색을 하는 유저가, 검색 대상 유저를 팔로우 하고 있는 지 여부를 포함
Copy link
Collaborator

@pparkjs pparkjs left a comment

Choose a reason for hiding this comment

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

반영해주시느라 고생하셨습니다! Approve 드릴게요!

@kdkdhoho kdkdhoho merged commit 60d4c47 into dev Jan 28, 2025
1 check passed
@kdkdhoho kdkdhoho deleted the feat/343 branch January 28, 2025 05:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

사용자 검색 응답값에 팔로잉 여부를 포함한다.

2 participants