-
Notifications
You must be signed in to change notification settings - Fork 2
[feat]팔로우 목록 조회 #180
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]팔로우 목록 조회 #180
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| import EatPic.spring.domain.reaction.repository.ReactionRepository; | ||
| import EatPic.spring.domain.reaction.service.ReactionService; | ||
| import EatPic.spring.domain.user.converter.UserConverter; | ||
| import EatPic.spring.domain.user.entity.FollowStatus; | ||
| import EatPic.spring.domain.user.entity.User; | ||
| import EatPic.spring.domain.user.repository.UserFollowRepository; | ||
| import EatPic.spring.domain.user.repository.UserRepository; | ||
|
|
@@ -18,14 +19,10 @@ | |
| import EatPic.spring.global.common.exception.handler.ExceptionHandler; | ||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import lombok.RequiredArgsConstructor; // 자동으로 생성자 주입 | ||
| import org.springframework.data.domain.PageRequest; | ||
| import org.springframework.data.domain.Pageable; | ||
| import org.springframework.data.domain.Slice; | ||
| import org.springframework.data.domain.Sort; | ||
| import org.springframework.data.domain.*; | ||
2anizirong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.*; | ||
2anizirong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import java.util.stream.Collectors; | ||
|
|
||
| @Service | ||
|
|
@@ -208,4 +205,47 @@ private Map<Long, Long> getMapCardCountByHashtag(List<Long> hashtagIds){ | |
| row -> (Long) row[1] // count | ||
| )); | ||
| } | ||
|
|
||
| @Override | ||
| public SearchResponseDTO.GetAccountListResponseDtoWithFollow getFollowList(HttpServletRequest request, Long userId, FollowStatus status, String query, int limit, Long cursor) { | ||
|
|
||
| User me = userService.getLoginUser(request); | ||
|
|
||
| // 페이징 처리 하기 | ||
| Pageable pageable = PageRequest.of(0, limit + 1, Sort.by("id").ascending()); | ||
| Slice<User> users = new SliceImpl<>(Collections.emptyList(), pageable, false); | ||
2anizirong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| switch(status){ | ||
| case FOLLOWED -> { // 해당 유저를 팔로우한 사람 목록 | ||
| users = userRepository.searchAccountNotInFollow(query, cursor, pageable, userId); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
| case FOLLOWING -> { // 해당 유저가 팔로우한 사람 목록 | ||
| users = userRepository.searchAccountInFollow(query, cursor, pageable, userId); | ||
| } | ||
| } | ||
|
|
||
| // 검색 결과가 없으면 예외 발생 | ||
| if (users.isEmpty()) { | ||
| throw new ExceptionHandler(ErrorStatus._NO_RESULTS_FOUND); | ||
| } | ||
|
|
||
| List<Long> targetUserIds = users.getContent().stream() | ||
| .map(User::getId) | ||
| .toList(); | ||
2anizirong marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // 내가 팔로우한 유저 목록 | ||
| Set<Long> alreadyFollowedIdSet = new HashSet<>(userFollowRepository.findFollowingUserIds(me.getId())); | ||
|
|
||
|
|
||
|
|
||
| List<SearchResponseDTO.GetAccountResponseDtoWithFollow> result = users.getContent().stream() | ||
| .map(user -> UserConverter.toAccountDtoWithFollow( | ||
| user, | ||
| alreadyFollowedIdSet.contains(user.getId())) | ||
| ).toList(); | ||
|
|
||
|
|
||
| boolean hasNext = users.hasNext(); | ||
| Long nextCursor = hasNext ? users.getContent().get(users.getContent().size() - 1).getId() : null; | ||
|
|
||
| return new SearchResponseDTO.GetAccountListResponseDtoWithFollow(result, nextCursor, result.size(), hasNext); | ||
|
Comment on lines
+239
to
+249
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 페이징 처리를 위해 boolean hasNext = users.hasNext();
Long nextCursor = hasNext ? users.getContent().get(users.getContent().size() - 1).getId() : null;
List<User> userList = users.getContent();
if (hasNext) {
userList = userList.subList(0, limit);
}
List<SearchResponseDTO.GetAccountResponseDtoWithFollow> result = userList.stream()
.map(user -> UserConverter.toAccountDtoWithFollow(
user,
alreadyFollowedIdSet.contains(user.getId()))
).toList();
return new SearchResponseDTO.GetAccountListResponseDtoWithFollow(result, nextCursor, result.size(), hasNext); |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package EatPic.spring.domain.user.entity; | ||
|
|
||
| public enum FollowStatus { | ||
| FOLLOWING, FOLLOWED | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,4 +42,19 @@ Slice<User> searchAccountInAll(@Param("query") String query, | |
| Slice<User> searchAccountInFollow(@Param("query") String query, | ||
| @Param("cursor") Long cursor, Pageable pageable, @Param("loginUserId") Long userId); | ||
|
|
||
| @Query(""" | ||
| SELECT u | ||
| FROM User u | ||
| WHERE u.id NOT IN ( | ||
| SELECT uf.targetUser.id | ||
| FROM UserFollow uf | ||
| WHERE uf.user.id = :loginUserId | ||
| ) | ||
| AND (:cursor IS NULL OR u.id > :cursor) | ||
| AND u.nickname LIKE %:query% | ||
| ORDER BY u.id ASC | ||
| """) | ||
| Slice<User> searchAccountNotInFollow(@Param("query") String query, | ||
| @Param("cursor") Long cursor, Pageable pageable, @Param("loginUserId") Long userId); | ||
|
Comment on lines
+45
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 @Query("""
SELECT u
FROM User u
JOIN UserFollow uf ON u.id = uf.user.id
WHERE uf.targetUser.id = :userId
AND (:cursor IS NULL OR u.id > :cursor)
AND u.nickname LIKE %:query%
ORDER BY u.id ASC
""")
Slice<User> searchFollowersByNickname(@Param("query") String query,
@Param("cursor") Long cursor, Pageable pageable, @Param("userId") Long userId); |
||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.