Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ public Page<Post> findAllByHashtagIdAndVisibilityPublic(Long hashtagId, Pageable
return postJpaRepository.findAllByHashtagIdAndVisibilityPublic(hashtagId, pageable);
}

@Override
public Page<Post> findAllByHashtagIdAndVisibilityPublicExcludingBlockedUsers(
Long hashtagId, Long currentUserId, Pageable pageable) {
return postJpaRepository.findAllByHashtagIdAndVisibilityPublicExcludingBlockedUsers(
hashtagId, currentUserId, pageable);
}

@Override
public long count() {
return postJpaRepository.count();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ public Page<User> searchByNickName(String keyword, Pageable pageable) {
return userJpaRepository.findByNickNameContainingIgnoreCase(keyword, pageable);
}

@Override
public Page<User> searchByNickNameExcludingBlockedUsers(
String keyword, Long currentUserId, Pageable pageable) {
return userJpaRepository.searchByNickNameExcludingBlockedUsers(keyword, currentUserId, pageable);
}

@Override
public Page<User> findAll(Pageable pageable) {
return userJpaRepository.findAll(pageable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ WHERE p.postId IN (
Page<Post> 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<Post> findAllByHashtagIdAndVisibilityPublicExcludingBlockedUsers(
@Param("hashtagId") Long hashtagId,
@Param("currentUserId") Long currentUserId,
Pageable pageable);

List<Post> findAllByPostIdIn(List<Long> postIds);

@Modifying
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ List<User> findNearbyUsersByType(

Page<User> 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<User> searchByNickNameExcludingBlockedUsers(
@Param("keyword") String keyword,
@Param("currentUserId") Long currentUserId,
Pageable pageable);

Page<User> findAll(Pageable pageable);

@Modifying
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public interface PostRepository {

Page<Post> findAllByHashtagIdAndVisibilityPublic(Long hashtagId, Pageable pageable);

Page<Post> findAllByHashtagIdAndVisibilityPublicExcludingBlockedUsers(
Long hashtagId, Long currentUserId, Pageable pageable);

long count();

List<Post> findAllByIds(List<Long> postIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ List<User> findNearbyUsersByType(double lat, double lng, double radiusKm,

Page<User> searchByNickName(String keyword, Pageable pageable);

Page<User> searchByNickNameExcludingBlockedUsers(
String keyword, Long currentUserId, Pageable pageable);

Page<User> findAll(Pageable pageable);

long count();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<HashtagResponseDTO> searchHashtags(String keyword, Pageable pageable) {
validateKeyword(keyword);
Expand All @@ -58,42 +56,43 @@ public PageResponse<PostSummaryResponseDTO> searchPostsByHashtag(
Hashtag hashtag = hashtagRepository.findByTagName(normalizedName)
.orElseThrow(() -> new BusinessException(PostErrorCode.HASHTAG_NOT_FOUND));

Page<Post> postPage = postRepository.findAllByHashtagIdAndVisibilityPublic(
hashtag.getHashtagId(), pageable);

List<Long> blockedIds = getBlockedIds(currentUserId);

List<Post> filteredPosts = postPage.getContent().stream()
.filter(post -> !blockedIds.contains(post.getUserId()))
.toList();
Page<Post> postPage;
if (currentUserId != null) {
postPage = postRepository.findAllByHashtagIdAndVisibilityPublicExcludingBlockedUsers(
hashtag.getHashtagId(), currentUserId, pageable);
} else {
postPage = postRepository.findAllByHashtagIdAndVisibilityPublic(
hashtag.getHashtagId(), pageable);
}

List<Long> postIds = filteredPosts.stream()
List<Long> postIds = postPage.getContent().stream()
.map(Post::getPostId)
.toList();

Map<Long, String> thumbnailMap = postImageService.getThumbnailUrls(postIds);

List<PostSummaryResponseDTO> summaries = filteredPosts.stream()
List<PostSummaryResponseDTO> summaries = postPage.getContent().stream()
.map(post -> PostSummaryResponseDTO.of(
post, thumbnailMap.get(post.getPostId())))
.toList();

return PageResponse.of(summaries, postPage);
}



public PageResponse<UserSearchResponseDTO> searchUsers(
String keyword, Long currentUserId, Pageable pageable) {

validateKeyword(keyword);

Page<User> userPage = userRepository.searchByNickName(keyword.trim(), pageable);

List<Long> blockedIds = getBlockedIds(currentUserId);
Page<User> userPage;
if (currentUserId != null) {
userPage = userRepository.searchByNickNameExcludingBlockedUsers(
keyword.trim(), currentUserId, pageable);
} else {
userPage = userRepository.searchByNickName(keyword.trim(), pageable);
}

List<UserSearchResponseDTO> users = userPage.getContent().stream()
.filter(user -> !blockedIds.contains(user.getUserId()))
.map(UserSearchResponseDTO::from)
.toList();

Expand All @@ -114,11 +113,4 @@ private String normalizeHashtagKeyword(String keyword) {
.replaceAll("^#+", "")
.toLowerCase();
}

private List<Long> getBlockedIds(Long currentUserId) {
if (currentUserId == null) {
return List.of();
}
return blockRepository.findBlockedIdsByBlockerId(currentUserId);
}
}
Loading