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
29 changes: 29 additions & 0 deletions src/main/java/goorm/ddok/member/repository/UserRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,33 @@ interface UserOverlayRow {
ORDER BY LOWER(u.nickname) ASC, u.id ASC
""")
Page<User> searchPlayersWithKeyword(@Param("keyword") String keyword, Pageable pageable);

@Query("""
SELECT DISTINCT u.id, LOWER(u.nickname) as nickname_lower FROM User u
LEFT JOIN u.location loc
LEFT JOIN u.positions pos
WHERE (:keyword IS NULL OR :keyword = '' OR
LOWER(u.nickname) LIKE LOWER(CONCAT('%', :keyword, '%')) OR
LOWER(pos.positionName) LIKE LOWER(CONCAT('%', :keyword, '%')) OR
LOWER(COALESCE(loc.region1DepthName, '')) LIKE LOWER(CONCAT('%', :keyword, '%')) OR
LOWER(COALESCE(loc.region2DepthName, '')) LIKE LOWER(CONCAT('%', :keyword, '%')) OR
LOWER(COALESCE(loc.region3DepthName, '')) LIKE LOWER(CONCAT('%', :keyword, '%')) OR
LOWER(COALESCE(loc.roadName, '')) LIKE LOWER(CONCAT('%', :keyword, '%')) OR
LOWER(COALESCE(loc.mainBuildingNo, '')) LIKE LOWER(CONCAT('%', :keyword, '%')) OR
LOWER(COALESCE(loc.subBuildingNo, '')) LIKE LOWER(CONCAT('%', :keyword, '%')) OR
LOWER(CONCAT(COALESCE(loc.region1DepthName, ''), ' ', COALESCE(loc.region2DepthName, ''))) LIKE LOWER(CONCAT('%', :keyword, '%'))
)
ORDER BY LOWER(u.nickname) ASC, u.id ASC
""")
Page<Object[]> searchPlayerIdsWithKeywordAndNickname(@Param("keyword") String keyword, Pageable pageable);

// 2단계: ID 리스트로 User 엔티티 조회 (정렬 유지)
@Query("""
SELECT u FROM User u
LEFT JOIN FETCH u.location
LEFT JOIN FETCH u.positions
WHERE u.id IN :userIds
ORDER BY LOWER(u.nickname) ASC, u.id ASC
""")
List<User> findUsersByIdsWithDetails(@Param("userIds") List<Long> userIds);
}
36 changes: 24 additions & 12 deletions src/main/java/goorm/ddok/player/service/ProfileSearchService.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,40 @@ public Page<ProfileSearchResponse> searchPlayers(String keyword, int page, int s
size = (size <= 0) ? 10 : size;

Pageable pageable = PageRequest.of(page, size);

String searchKeyword = hasText(keyword) ? keyword.trim() : null;

Page<User> rows = userRepository.searchPlayersWithKeyword(searchKeyword, pageable);
// 1단계: ID와 정렬용 nickname 조회
Page<Object[]> userIdPage = userRepository.searchPlayerIdsWithKeywordAndNickname(searchKeyword, pageable);

if (userIdPage.getContent().isEmpty()) {
return new PageImpl<>(Collections.emptyList(), pageable, 0);
}

// ID만 추출 (이미 정렬된 순서)
List<Long> userIds = userIdPage.getContent().stream()
.map(row -> (Long) row[0])
.toList();

// 2단계: User 엔티티 조회 (정렬 순서 유지)
List<User> users = userRepository.findUsersByIdsWithDetails(userIds);

List<User> distinctUsers = rows.getContent().stream()
.collect(Collectors.toMap(User::getId, user -> user, (existing, replacement) -> existing))
.values()
.stream()
.sorted((u1, u2) -> {
int nicknameCompare = u1.getNickname().compareToIgnoreCase(u2.getNickname());
return nicknameCompare != 0 ? nicknameCompare : u1.getId().compareTo(u2.getId());
})
// ID 순서대로 정렬 (첫 번째 쿼리의 정렬 순서 유지)
Map<Long, User> userMap = users.stream()
.collect(Collectors.toMap(User::getId, user -> user));

List<User> sortedUsers = userIds.stream()
.map(userMap::get)
.filter(Objects::nonNull)
.toList();

List<ProfileSearchResponse> responses = distinctUsers.stream()
List<ProfileSearchResponse> responses = sortedUsers.stream()
.map(u -> toResponse(u, currentUserId))
.toList();

return new PageImpl<>(responses, pageable, rows.getTotalElements());
return new PageImpl<>(responses, pageable, userIdPage.getTotalElements());
}


// 나머지 메서드들은 그대로 유지
private ProfileSearchResponse toResponse(User u, Long currentUserId) {
// 기존 코드 그대로
Expand Down