From 515b5ffc3b7fcc40ac8a1fa0fc9df065c7463de0 Mon Sep 17 00:00:00 2001 From: hi-june Date: Sat, 18 Nov 2023 01:34:27 +0900 Subject: [PATCH 1/2] =?UTF-8?q?NABI-255--feat=20:=20OrderSpecifier=20Util?= =?UTF-8?q?=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/util/QueryDslUtil.java | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/main/java/org/prgrms/nabimarketbe/global/util/QueryDslUtil.java diff --git a/src/main/java/org/prgrms/nabimarketbe/global/util/QueryDslUtil.java b/src/main/java/org/prgrms/nabimarketbe/global/util/QueryDslUtil.java new file mode 100644 index 00000000..0d946a24 --- /dev/null +++ b/src/main/java/org/prgrms/nabimarketbe/global/util/QueryDslUtil.java @@ -0,0 +1,27 @@ +package org.prgrms.nabimarketbe.global.util; + +import com.querydsl.core.types.Order; +import com.querydsl.core.types.OrderSpecifier; +import com.querydsl.core.types.Path; +import com.querydsl.core.types.dsl.Expressions; + +public class QueryDslUtil { + /** + * Order, Path, fieldName을 전달하면 OrderSpecifier 객체를 리턴하는 Util 클래스. + * Sort시 마다 사용할 수 있도록 한다. + * + * @param order + * @param parent compileQuerydsl 빌드를 통해서 생성된 Q타입 클래스의 객체(Sort의 대상이 되는 Q타입 클래스 객체를 전달한다.) + * @param fieldName + * @return OrderSpecifier 객체 + */ + public static OrderSpecifier getSortedColumn( + Order order, + Path parent, + String fieldName + ) { + Path fieldPath = Expressions.path(Object.class, parent, fieldName); + + return new OrderSpecifier(order, fieldPath); + } +} From 069bf42377bb69316fcc99816793b9d4cd5c0add Mon Sep 17 00:00:00 2001 From: hi-june Date: Sat, 18 Nov 2023 01:36:26 +0900 Subject: [PATCH 2/2] =?UTF-8?q?NABI-255--feat=20:=20=EC=B9=B4=EB=93=9C=20?= =?UTF-8?q?=EB=8F=84=EB=A9=94=EC=9D=B8=20=EB=8B=A4=EC=A4=91=20=EC=A0=95?= =?UTF-8?q?=EB=A0=AC=20=EC=A1=B0=EA=B1=B4=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../card/repository/CardRepositoryImpl.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/card/repository/CardRepositoryImpl.java b/src/main/java/org/prgrms/nabimarketbe/domain/card/repository/CardRepositoryImpl.java index afbd0f3c..e8118e50 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/card/repository/CardRepositoryImpl.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/card/repository/CardRepositoryImpl.java @@ -1,6 +1,8 @@ package org.prgrms.nabimarketbe.domain.card.repository; import com.querydsl.core.types.ConstantImpl; +import com.querydsl.core.types.Order; +import com.querydsl.core.types.OrderSpecifier; import com.querydsl.core.types.Projections; import com.querydsl.core.types.dsl.BooleanExpression; import com.querydsl.core.types.dsl.Expressions; @@ -17,7 +19,10 @@ import org.prgrms.nabimarketbe.domain.item.entity.PriceRange; import org.prgrms.nabimarketbe.domain.suggestion.dto.response.projection.SuggestionInfo; import org.prgrms.nabimarketbe.domain.user.entity.User; +import org.prgrms.nabimarketbe.global.util.QueryDslUtil; +import org.springframework.data.domain.Sort; +import java.util.ArrayList; import java.util.List; import static org.prgrms.nabimarketbe.domain.card.entity.QCard.card; @@ -59,7 +64,10 @@ public CardPagingResponseDTO getCardsByCondition( priceRangeEquals(priceRange), titleEquals(cardTitle) ) - .orderBy(card.createdDate.desc()) // 디폴트는 생성일자 최신순 정렬 + .orderBy(getOrderSpecifier(Sort.by( + Sort.Order.desc("createdDate"), + Sort.Order.desc("cardId") + ))) .limit(size) .fetch(); @@ -200,4 +208,15 @@ private String generateCursor(CardListReadResponseDTO cardListReadResponseDTO) { .replace(":", "") + String.format("%08d", cardListReadResponseDTO.getCardId()); } + + private OrderSpecifier[] getOrderSpecifier(Sort sort) { + List orders = new ArrayList<>(); + + for (Sort.Order order : sort) { // Sort에 여러 정렬 기준을 담을 수 있음 + Order direction = order.getDirection().isAscending() ? Order.ASC : Order.DESC; + orders.add(QueryDslUtil.getSortedColumn(direction, card, order.getProperty())); + } + + return orders.toArray(OrderSpecifier[]::new); + } }