From 2954565fc7068b38397ab2ee3a053fbcbce7da33 Mon Sep 17 00:00:00 2001 From: kric_jspark Date: Wed, 22 Jul 2026 11:19:48 +0900 Subject: [PATCH] =?UTF-8?q?260722=5F01=5FENH:=20pageable=EC=97=90=20sort?= =?UTF-8?q?=20alias=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- .../common/feature/web/page/Pageables.java | 23 ++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/build.gradle b/build.gradle index d4bd6fd..2f01c94 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { } // 버전 변경은 여기서 -def commonVer = "beta.1.3.0" +def commonVer = "beta.1.4.0" group = 'com.kri.common' version = commonVer diff --git a/src/main/java/com/kri/common/feature/web/page/Pageables.java b/src/main/java/com/kri/common/feature/web/page/Pageables.java index 9c0ad1b..f869045 100644 --- a/src/main/java/com/kri/common/feature/web/page/Pageables.java +++ b/src/main/java/com/kri/common/feature/web/page/Pageables.java @@ -6,7 +6,10 @@ import java.beans.PropertyDescriptor; import java.util.Arrays; import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Objects; import java.util.Set; +import java.util.function.Function; import java.util.stream.Collectors; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; @@ -18,14 +21,23 @@ public final class Pageables { } public static Pageable of(PageRequestDto req, Class sortType) { + return of(req, sortType, Function.identity()); + } + + public static Pageable of(PageRequestDto req, Class sortType, Map sortAliases) { + Map aliases = (sortAliases == null) ? Map.of() : sortAliases; + return of(req, sortType, sortKey -> aliases.getOrDefault(sortKey, sortKey)); + } + + public static Pageable of(PageRequestDto req, Class sortType, Function sortMapper) { int page = Math.max(req.getPageIndex(), 0); int size = Math.max(req.getPageSize(), 1); - Sort sort = buildSort(req, sortType); + Sort sort = buildSort(req, sortType, sortMapper); return (sort == null) ? PageRequest.of(page, size) : PageRequest.of(page, size, sort); } - private static Sort buildSort(PageRequestDto req, Class sortType) { + private static Sort buildSort(PageRequestDto req, Class sortType, Function sortMapper) { String sortBy = req.getSortBy(); if (sortBy == null || sortBy.isBlank()) { return null; @@ -36,8 +48,13 @@ public final class Pageables { : Sort.Direction.DESC; Set allowed = allowedProperties(sortType); + Function mapper = (sortMapper == null) ? Function.identity() : sortMapper; List props = Arrays.stream(sortBy.split(",")) + .map(String::trim) + .filter(s -> !s.isBlank()) + .map(mapper) + .filter(Objects::nonNull) .map(String::trim) .filter(s -> !s.isBlank()) .collect(Collectors.toList()); @@ -74,4 +91,4 @@ public final class Pageables { throw new IllegalStateException("Failed to introspect sortType=" + sortType.getName(), e); } } -} \ No newline at end of file +}