Skip to content

Commit 8173d24

Browse files
committed
fix(user-admin): User role 정렬 적용 pagination
1 parent d77532f commit 8173d24

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

src/main/java/inha/gdgoc/domain/user/service/UserAdminService.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
import inha.gdgoc.global.exception.GlobalErrorCode;
1212
import lombok.RequiredArgsConstructor;
1313
import org.springframework.data.domain.Page;
14+
import org.springframework.data.domain.PageRequest;
1415
import org.springframework.data.domain.Pageable;
16+
import org.springframework.data.domain.Sort;
17+
import org.springframework.data.jpa.domain.JpaSort;
1518
import org.springframework.stereotype.Service;
1619
import org.springframework.transaction.annotation.Transactional;
1720

@@ -25,7 +28,43 @@ public class UserAdminService {
2528

2629
@Transactional(readOnly = true)
2730
public Page<UserSummaryResponse> listUsers(String q, Pageable pageable) {
28-
return userRepository.findSummaries(q, pageable);
31+
Pageable fixed = rewriteSort(pageable);
32+
return userRepository.findSummaries(q, fixed);
33+
}
34+
35+
private Pageable rewriteSort(Pageable pageable) {
36+
Sort original = pageable.getSort();
37+
if (original.isUnsorted()) return pageable;
38+
39+
Sort composed = Sort.unsorted();
40+
boolean hasUserRoleOrder = false;
41+
42+
for (Sort.Order o : original) {
43+
String prop = o.getProperty();
44+
Sort.Direction dir = o.getDirection();
45+
46+
if ("userRole".equals(prop)) {
47+
hasUserRoleOrder = true;
48+
String roleRankCase = "CASE u.userRole " +
49+
"WHEN 'GUEST' THEN 0 " +
50+
"WHEN 'MEMBER' THEN 1 " +
51+
"WHEN 'CORE' THEN 2 " +
52+
"WHEN 'LEAD' THEN 3 " +
53+
"WHEN 'ORGANIZER' THEN 4 " +
54+
"WHEN 'ADMIN' THEN 5 " +
55+
"ELSE -1 END";
56+
composed = composed.and(JpaSort.unsafe(dir, roleRankCase));
57+
} else {
58+
composed = composed.and(Sort.by(new Sort.Order(dir, prop)));
59+
}
60+
}
61+
62+
// ROLE 정렬 요청이 있었다면, 같은 권한 내에서 name ASC로 안정화
63+
if (hasUserRoleOrder) {
64+
composed = composed.and(Sort.by("name").ascending());
65+
}
66+
67+
return PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), composed);
2968
}
3069

3170
/**

0 commit comments

Comments
 (0)