@Override
public Page<Tuple> sortedProducts(PageRequestDTO pageRequestDTO, Pageable pageable) {
BooleanExpression expression = qProduct.subCategory.subCateNo.eq(subCateNo)
.and(qProduct.state.eq("판매"));
OrderSpecifier<?> orderSpecifier = switch (sortType) {
case "lowPrice" -> discountedPrice.asc();
case "highPrice" -> discountedPrice.desc();
case "highRating" -> qProduct.ratingAvg.desc();
case "manyReviews"-> qProduct.reviewCount.desc();
default -> qProduct.regDate.desc();
};
List<Tuple> tupleList = queryFactory
.select(qProduct, qSeller.company, qSeller.rank, qProductImage.sNameList)
.from(qProduct)
.join(qSeller).on(qProduct.seller.sno.eq(qSeller.sno))
.leftJoin(qProductImage).on(qProductImage.product.eq(qProduct))
.where(expression)
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.orderBy(orderSpecifier)
.fetch();
long total = queryFactory.select(qProduct.count()).from(qProduct)
.where(expression).fetchOne();
return new PageImpl<>(tupleList, pageable, total);
}
기술 설명
- BooleanExpression expression
qProduct.subCategory.subCateNo.eq(subCateNo)qProduct.state.eq("판매")- 두 조건을 AND로 결합하여 판매 가능한 해당 서브카테고리 상품을 필터링
- OrderSpecifier\<?> orderSpecifier
sortType값에 따라 동적 정렬 조건 생성
lowPrice:discountedPrice.asc()(낮은 가격 순)highPrice:discountedPrice.desc()(높은 가격 순)highRating:qProduct.ratingAvg.desc()(평점 높은 순)manyReviews:qProduct.reviewCount.desc()(리뷰 많은 순)- 기본:
qProduct.regDate.desc()(최근 등록 순)- queryFactory.select(...).from(qProduct)
- 선택 컬럼:
qProduct(상품),qSeller.company,qSeller.rank,qProductImage.sNameListjoin(qSeller)로 판매자 정보 조인leftJoin(qProductImage)로 상품 이미지 조인where(expression)로 필터 적용offset(pageable.getOffset()),limit(pageable.getPageSize())로 페이징 적용orderBy(orderSpecifier)로 정렬 적용fetch()로 결과 List<Tuple> 반환- 총 개수 조회
queryFactory.select(qProduct.count())...fetchOne()로 전체 건수 조회- PageImpl<>(tupleList, pageable, total)
- 조회 결과 Tuple 리스트, Pageable 정보, 총 개수를
Page<Tuple>형태로 반환