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 2b98eb6d..70a39ff6 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); + } } 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); + } +}