diff --git a/prothsync/src/main/java/com/prothsync/prothsync/repository/impl/PostRepositoryImpl.java b/prothsync/src/main/java/com/prothsync/prothsync/repository/impl/PostRepositoryImpl.java index 098f6bd..4265224 100644 --- a/prothsync/src/main/java/com/prothsync/prothsync/repository/impl/PostRepositoryImpl.java +++ b/prothsync/src/main/java/com/prothsync/prothsync/repository/impl/PostRepositoryImpl.java @@ -64,6 +64,13 @@ public Page findAllByHashtagIdAndVisibilityPublic(Long hashtagId, Pageable return postJpaRepository.findAllByHashtagIdAndVisibilityPublic(hashtagId, pageable); } + @Override + public Page findAllByHashtagIdAndVisibilityPublicExcludingBlockedUsers( + Long hashtagId, Long currentUserId, Pageable pageable) { + return postJpaRepository.findAllByHashtagIdAndVisibilityPublicExcludingBlockedUsers( + hashtagId, currentUserId, pageable); + } + @Override public long count() { return postJpaRepository.count(); diff --git a/prothsync/src/main/java/com/prothsync/prothsync/repository/impl/UserRepositoryImpl.java b/prothsync/src/main/java/com/prothsync/prothsync/repository/impl/UserRepositoryImpl.java index 84c68ab..4084f15 100644 --- a/prothsync/src/main/java/com/prothsync/prothsync/repository/impl/UserRepositoryImpl.java +++ b/prothsync/src/main/java/com/prothsync/prothsync/repository/impl/UserRepositoryImpl.java @@ -80,6 +80,12 @@ public Page searchByNickName(String keyword, Pageable pageable) { return userJpaRepository.findByNickNameContainingIgnoreCase(keyword, pageable); } + @Override + public Page searchByNickNameExcludingBlockedUsers( + String keyword, Long currentUserId, Pageable pageable) { + return userJpaRepository.searchByNickNameExcludingBlockedUsers(keyword, currentUserId, pageable); + } + @Override public Page findAll(Pageable pageable) { return userJpaRepository.findAll(pageable); diff --git a/prothsync/src/main/java/com/prothsync/prothsync/repository/jpa/PostJpaRepository.java b/prothsync/src/main/java/com/prothsync/prothsync/repository/jpa/PostJpaRepository.java index 2208c5a..d1bc401 100644 --- a/prothsync/src/main/java/com/prothsync/prothsync/repository/jpa/PostJpaRepository.java +++ b/prothsync/src/main/java/com/prothsync/prothsync/repository/jpa/PostJpaRepository.java @@ -37,6 +37,22 @@ WHERE p.postId IN ( Page findAllByHashtagIdAndVisibilityPublic( @Param("hashtagId") Long hashtagId, Pageable pageable); + @Query(""" + SELECT p FROM Post p + WHERE p.postId IN ( + SELECT ph.postId FROM PostHashtag ph WHERE ph.hashtagId = :hashtagId + ) + AND p.visibility = 'PUBLIC' + AND p.userId NOT IN ( + SELECT b.blockedId FROM Block b WHERE b.blockerId = :currentUserId + ) + ORDER BY p.createdAt DESC + """) + Page findAllByHashtagIdAndVisibilityPublicExcludingBlockedUsers( + @Param("hashtagId") Long hashtagId, + @Param("currentUserId") Long currentUserId, + Pageable pageable); + List findAllByPostIdIn(List postIds); @Modifying diff --git a/prothsync/src/main/java/com/prothsync/prothsync/repository/jpa/UserJpaRepository.java b/prothsync/src/main/java/com/prothsync/prothsync/repository/jpa/UserJpaRepository.java index ab93a2b..0ba75ae 100644 --- a/prothsync/src/main/java/com/prothsync/prothsync/repository/jpa/UserJpaRepository.java +++ b/prothsync/src/main/java/com/prothsync/prothsync/repository/jpa/UserJpaRepository.java @@ -108,6 +108,18 @@ List findNearbyUsersByType( Page findByNickNameContainingIgnoreCase(String keyword, Pageable pageable); + @Query(""" + SELECT u FROM User u + WHERE LOWER(u.nickName) LIKE LOWER(CONCAT('%', :keyword, '%')) + AND u.userId NOT IN ( + SELECT b.blockedId FROM Block b WHERE b.blockerId = :currentUserId + ) + """) + Page searchByNickNameExcludingBlockedUsers( + @Param("keyword") String keyword, + @Param("currentUserId") Long currentUserId, + Pageable pageable); + Page findAll(Pageable pageable); @Modifying diff --git a/prothsync/src/main/java/com/prothsync/prothsync/repository/repository/PostRepository.java b/prothsync/src/main/java/com/prothsync/prothsync/repository/repository/PostRepository.java index 217b93f..4cc81e9 100644 --- a/prothsync/src/main/java/com/prothsync/prothsync/repository/repository/PostRepository.java +++ b/prothsync/src/main/java/com/prothsync/prothsync/repository/repository/PostRepository.java @@ -24,6 +24,9 @@ public interface PostRepository { Page findAllByHashtagIdAndVisibilityPublic(Long hashtagId, Pageable pageable); + Page findAllByHashtagIdAndVisibilityPublicExcludingBlockedUsers( + Long hashtagId, Long currentUserId, Pageable pageable); + long count(); List findAllByIds(List postIds); diff --git a/prothsync/src/main/java/com/prothsync/prothsync/repository/repository/UserRepository.java b/prothsync/src/main/java/com/prothsync/prothsync/repository/repository/UserRepository.java index d33b650..dfc8c50 100644 --- a/prothsync/src/main/java/com/prothsync/prothsync/repository/repository/UserRepository.java +++ b/prothsync/src/main/java/com/prothsync/prothsync/repository/repository/UserRepository.java @@ -26,6 +26,9 @@ List findNearbyUsersByType(double lat, double lng, double radiusKm, Page searchByNickName(String keyword, Pageable pageable); + Page searchByNickNameExcludingBlockedUsers( + String keyword, Long currentUserId, Pageable pageable); + Page findAll(Pageable pageable); long count(); diff --git a/prothsync/src/main/java/com/prothsync/prothsync/service/SearchService.java b/prothsync/src/main/java/com/prothsync/prothsync/service/SearchService.java index fe17559..1c65a35 100644 --- a/prothsync/src/main/java/com/prothsync/prothsync/service/SearchService.java +++ b/prothsync/src/main/java/com/prothsync/prothsync/service/SearchService.java @@ -10,7 +10,6 @@ import com.prothsync.prothsync.exception.PostErrorCode; import com.prothsync.prothsync.exception.SearchErrorCode; import com.prothsync.prothsync.global.PageResponse; -import com.prothsync.prothsync.repository.repository.BlockRepository; import com.prothsync.prothsync.repository.repository.HashtagRepository; import com.prothsync.prothsync.repository.repository.PostRepository; import com.prothsync.prothsync.repository.repository.UserRepository; @@ -33,7 +32,6 @@ public class SearchService { private final PostRepository postRepository; private final UserRepository userRepository; private final PostImageService postImageService; - private final BlockRepository blockRepository; public PageResponse searchHashtags(String keyword, Pageable pageable) { validateKeyword(keyword); @@ -58,22 +56,22 @@ public PageResponse searchPostsByHashtag( Hashtag hashtag = hashtagRepository.findByTagName(normalizedName) .orElseThrow(() -> new BusinessException(PostErrorCode.HASHTAG_NOT_FOUND)); - Page postPage = postRepository.findAllByHashtagIdAndVisibilityPublic( - hashtag.getHashtagId(), pageable); - - List blockedIds = getBlockedIds(currentUserId); - - List filteredPosts = postPage.getContent().stream() - .filter(post -> !blockedIds.contains(post.getUserId())) - .toList(); + Page postPage; + if (currentUserId != null) { + postPage = postRepository.findAllByHashtagIdAndVisibilityPublicExcludingBlockedUsers( + hashtag.getHashtagId(), currentUserId, pageable); + } else { + postPage = postRepository.findAllByHashtagIdAndVisibilityPublic( + hashtag.getHashtagId(), pageable); + } - List postIds = filteredPosts.stream() + List postIds = postPage.getContent().stream() .map(Post::getPostId) .toList(); Map thumbnailMap = postImageService.getThumbnailUrls(postIds); - List summaries = filteredPosts.stream() + List summaries = postPage.getContent().stream() .map(post -> PostSummaryResponseDTO.of( post, thumbnailMap.get(post.getPostId()))) .toList(); @@ -81,19 +79,20 @@ public PageResponse searchPostsByHashtag( return PageResponse.of(summaries, postPage); } - - public PageResponse searchUsers( String keyword, Long currentUserId, Pageable pageable) { validateKeyword(keyword); - Page userPage = userRepository.searchByNickName(keyword.trim(), pageable); - - List blockedIds = getBlockedIds(currentUserId); + Page userPage; + if (currentUserId != null) { + userPage = userRepository.searchByNickNameExcludingBlockedUsers( + keyword.trim(), currentUserId, pageable); + } else { + userPage = userRepository.searchByNickName(keyword.trim(), pageable); + } List users = userPage.getContent().stream() - .filter(user -> !blockedIds.contains(user.getUserId())) .map(UserSearchResponseDTO::from) .toList(); @@ -114,11 +113,4 @@ private String normalizeHashtagKeyword(String keyword) { .replaceAll("^#+", "") .toLowerCase(); } - - private List getBlockedIds(Long currentUserId) { - if (currentUserId == null) { - return List.of(); - } - return blockRepository.findBlockedIdsByBlockerId(currentUserId); - } } \ No newline at end of file