Skip to content

Commit

Permalink
Merge pull request #68 from Uni-Made/feature/#63
Browse files Browse the repository at this point in the history
๐Ÿ› [BUG] ํŒ๋งค ์ƒํ’ˆ ๋ฆฌ์ŠคํŠธ ํ•„ํ„ฐ๋ง ๋ฒ„๊ทธ ์ˆ˜์ • #63
  • Loading branch information
moonyaeyoon authored Jul 24, 2024
2 parents 1644b30 + dcbdd81 commit a674979
Showing 1 changed file with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import umc.unimade.domain.products.entity.Products;
import umc.unimade.domain.products.entity.QProducts;
import umc.unimade.domain.favorite.entity.QFavoriteProduct;
import com.querydsl.core.types.OrderSpecifier;

import java.util.List;

@RequiredArgsConstructor
public class ProductsRepositoryCustomImpl implements ProductsRepositoryCustom{
public class ProductsRepositoryCustomImpl implements ProductsRepositoryCustom {
private final JPAQueryFactory queryFactory;

@Override
Expand All @@ -20,12 +21,14 @@ public List<Products> findProductsByFilters(String category, String keyword, Lon

return queryFactory
.selectFrom(products)
.leftJoin(favoriteProduct).on(favoriteProduct.product.eq(products))
.where(
categoryEq(category),
keywordContains(keyword),
priceBetween(minPrice, maxPrice),
cursorLessThan(cursor)
)
.groupBy(products.id)
.orderBy(getSortOrder(sort, products, favoriteProduct))
.limit(pageSize)
.fetch();
Expand All @@ -41,17 +44,29 @@ private BooleanExpression keywordContains(String keyword) {
}

private BooleanExpression priceBetween(Long minPrice, Long maxPrice) {
return minPrice != null && maxPrice != null ? QProducts.products.price.between(minPrice, maxPrice) : null;
if (minPrice == null && maxPrice == null) {
return null;
}
if (minPrice == null) {
return QProducts.products.price.loe(maxPrice);
}
if (maxPrice == null) {
return QProducts.products.price.goe(minPrice);
}
return QProducts.products.price.between(minPrice, maxPrice);
}

private BooleanExpression cursorLessThan(Long cursor) {
return cursor != null ? QProducts.products.id.lt(cursor) : null;
}

private com.querydsl.core.types.OrderSpecifier<?> getSortOrder(String sort, QProducts products, QFavoriteProduct favoriteProduct) {
private OrderSpecifier<?> getSortOrder(String sort, QProducts products, QFavoriteProduct favoriteProduct) {
if (sort == null || sort.isEmpty()) {
sort = "favorite";
}
switch (sort) {
case "favorite":
return favoriteProduct.count().desc();
return favoriteProduct.id.count().desc();
case "latest":
return products.createdAt.desc();
case "deadline":
Expand All @@ -60,4 +75,4 @@ private com.querydsl.core.types.OrderSpecifier<?> getSortOrder(String sort, QPro
return products.id.desc();
}
}
}
}

0 comments on commit a674979

Please sign in to comment.