Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NABI-255--fix card list read with cursor paging #47

Merged
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
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -200,4 +208,15 @@ private String generateCursor(CardListReadResponseDTO cardListReadResponseDTO) {
.replace(":", "")
+ String.format("%08d", cardListReadResponseDTO.getCardId());
}

private OrderSpecifier[] getOrderSpecifier(Sort sort) {
List<OrderSpecifier> 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);
}
Comment on lines +212 to +221
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p1
해당 메소드는 제가 예상하기엔 커스텀 커서 페이징이 필요한 모든 곳에서 작성되어야 할 것 같은데요 !
QueryDslUtil의 static 메소드로 작성하지 않으신 이유가 있을까요 ?!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

추후에 따로 브랜치를 파서 커서 페이징 하는 모든 부분들을 통합해서 수정할 예정입니다.
공통으로 사용할 수 있는 부분들을 최대한 빼 볼 생각인데, 그 때 같이 넣어두겠습니다~

}
Original file line number Diff line number Diff line change
@@ -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<Object> fieldPath = Expressions.path(Object.class, parent, fieldName);

return new OrderSpecifier(order, fieldPath);
}
}