Skip to content

Commit

Permalink
[FIX] not using Pageable
Browse files Browse the repository at this point in the history
  • Loading branch information
wakkpu committed Jan 18, 2024
1 parent d20a897 commit b838e99
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@ ResponseEntity<ReadProductPageResponse> readProductPage(
@RequestParam(required = false) Long categoryId,
@RequestParam ProductType type,
@RequestParam(required = false) String query,
@PageableDefault(
size = 5,
sort = {"updatedAt"},
direction = Sort.Direction.DESC
) Pageable pageable
@RequestParam int page,
@RequestParam int size,
@RequestParam String sort,
@RequestParam String direction
) {
return ResponseEntity.status(HttpStatus.OK).body(
productFacade.readProductPage(brandId, categoryId, type, query, pageable)
productFacade.readProductPage(
brandId, categoryId, type, query,
page, size, sort, direction
)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,14 @@ public ReadOOTDSearchSliceResponse searchFromOOTD(Long lastId, String query) {
return productService.searchFromOOTD(lastId, query);
}

public ReadProductPageResponse readProductPage(Long brandId, Long categoryId, ProductType type, String query, Pageable pageable) {
return productService.readProductPage(brandId, categoryId, type, query, pageable);
public ReadProductPageResponse readProductPage(
Long brandId, Long categoryId, ProductType type, String query,
int page, int size, String sort, String direction
) {
return productService.readProductPage(
brandId, categoryId, type, query,
page, size, sort, direction
);
}

@Cacheable(value = "newProducts", unless = "#result == null")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ Slice<Product> findProductSlice(
);

Page<Product> findProductPage(
Long brandId, List<Category> childCategories,
ProductType type, String query, Pageable pageable
Long brandId, List<Category> childCategories, ProductType type, String query,
int page, int size, String sort, String direction
);

Slice<Product> searchProductsFromOOTD(Long lastId, String query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
import com.dailyon.productservice.common.enums.ProductType;
import com.dailyon.productservice.product.entity.Product;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.PathBuilder;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -73,10 +71,11 @@ public Slice<Product> findProductSlice(

@Override
public Page<Product> findProductPage(
Long brandId, List<Category> childCategories,
ProductType type, String query, Pageable pageable
Long brandId, List<Category> childCategories, ProductType type, String query,
int page, int size, String sort, String direction
) {
PathBuilder<Product> entityPath = new PathBuilder<>(Product.class, "product");
Pageable pageable = PageRequest.of(page, size);

BooleanBuilder booleanBuilder = new BooleanBuilder();
if(query != null) {
booleanBuilder.and(nameContains(query).or(codeContains(query)));
Expand All @@ -90,14 +89,10 @@ public Page<Product> findProductPage(
.and(booleanBuilder)
.and(productTypeEq(type))
)
.orderBy(orderSpecifier(sort, direction))
.offset(pageable.getOffset())
.limit(pageable.getPageSize());
for(Sort.Order order: pageable.getSort()) {
indexQuery.orderBy(new OrderSpecifier(
order.isAscending() ? Order.ASC : Order.DESC,
entityPath.get(order.getProperty())
));
}

List<Long> indexes = indexQuery.fetch();
if(indexes.isEmpty()) {
return new PageImpl<>(new ArrayList<>(), pageable, 0);
Expand All @@ -111,14 +106,9 @@ public Page<Product> findProductPage(
.leftJoin(product.describeImages, describeImage).fetchJoin()
.leftJoin(product.productStocks, productStock).fetchJoin()
.leftJoin(product.reviewAggregate, reviewAggregate).fetchJoin()
.orderBy(orderSpecifier(sort, direction))
.where(product.id.in(indexes));

for(Sort.Order order: pageable.getSort()) {
resultQuery.orderBy(new OrderSpecifier(
order.isAscending() ? Order.ASC : Order.DESC,
entityPath.get(order.getProperty())
));
}
List<Product> result = resultQuery.fetch();

JPAQuery<Long> countQuery = jpaQueryFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,19 @@ public Slice<Product> readProductSlice(
}

public ReadProductPageResponse readProductPage(
Long brandId, Long categoryId, ProductType type,
String query, Pageable pageable
Long brandId, Long categoryId, ProductType type, String query,
int page, int size, String sort, String direction
) {
List<Category> childCategories = null;
if(categoryId != null) {
childCategories = categoryRepository.findAllChildCategories(categoryId);
}
return ReadProductPageResponse.fromEntity(
productRepository.findProductPage(brandId, childCategories, type, query, pageable));
productRepository.findProductPage(
brandId, childCategories, type, query,
page, size, sort, direction
)
);
}

public ReadOOTDSearchSliceResponse searchFromOOTD(Long lastId, String query) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,8 @@ void searchProductsFromOOTDTest() {
void readProductPage() {
// given, when
ReadProductPageResponse response = productService.readProductPage(
null, null, ProductType.valueOf("NORMAL"), null, PageRequest.of(0, 8)
null, null, ProductType.valueOf("NORMAL"), null,
0, 5, "updatedAt", "desc"
);

// then
Expand Down

0 comments on commit b838e99

Please sign in to comment.