From 141792887f5fc7a7a9cd70d4c3c237f34553189f Mon Sep 17 00:00:00 2001 From: sooyoungh Date: Fri, 18 Aug 2023 02:35:19 +0900 Subject: [PATCH 1/3] =?UTF-8?q?refactor:=20=EB=AA=BD=EA=B3=A0=EB=94=94?= =?UTF-8?q?=EB=B9=84=20=ED=8E=98=EC=9D=B4=EC=A7=95=20=EB=B0=A9=EC=8B=9D=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD(Aggregation=20=EC=82=AC=EC=9A=A9)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/EventProductController.java | 13 ++- .../controller/PbProductController.java | 11 +- .../product/enumtype/Sorted.java | 27 +++-- .../product/service/EventProductService.java | 60 ++++++----- .../product/service/PbProductService.java | 102 +++++++++++++----- .../product/service/ProductService.java | 59 +++++----- 6 files changed, 168 insertions(+), 104 deletions(-) diff --git a/src/main/java/com/pyonsnalcolor/product/controller/EventProductController.java b/src/main/java/com/pyonsnalcolor/product/controller/EventProductController.java index a65b12e..b4dc456 100644 --- a/src/main/java/com/pyonsnalcolor/product/controller/EventProductController.java +++ b/src/main/java/com/pyonsnalcolor/product/controller/EventProductController.java @@ -8,6 +8,8 @@ import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -19,7 +21,7 @@ public class EventProductController { private final EventProductService eventProductService; - @Operation(summary = "이벤트 상품 필터 조회", description = "이벤트 상품을 필터링 조회합니다.") + @Operation(summary = "행사 상품 필터 조회", description = "행사 상품을 필터링 조회합니다.") @PostMapping("/products/event-products") public ResponseEntity> getEventProductsByFilter( @RequestParam int pageNumber, @@ -27,15 +29,16 @@ public ResponseEntity> getEventProductsByFilter( @RequestParam String storeType, @RequestBody ProductFilterRequestDto productFilterRequestDto ) { - Page products = eventProductService.getFilteredProducts( - pageNumber, pageSize, storeType, productFilterRequestDto); + Pageable pageable = PageRequest.of(pageNumber, pageSize); + Page products = eventProductService.getPagedProductsDtoByFilter( + pageable, storeType, productFilterRequestDto); return new ResponseEntity(products, HttpStatus.OK); } - @Operation(summary = "이벤트 상품 단건 조회", description = "id 바탕으로 이벤트 상품을 조회합니다.") + @Operation(summary = "행사 상품 단건 조회", description = "id로 행사 상품을 조회합니다.") @GetMapping("/products/event-products/{id}") public ResponseEntity getEventProduct(@PathVariable String id) { - ProductResponseDto responseDto = eventProductService.getProduct(id); + ProductResponseDto responseDto = eventProductService.getProductById(id); return new ResponseEntity(responseDto, HttpStatus.OK); } } \ No newline at end of file diff --git a/src/main/java/com/pyonsnalcolor/product/controller/PbProductController.java b/src/main/java/com/pyonsnalcolor/product/controller/PbProductController.java index f508f19..b7409da 100644 --- a/src/main/java/com/pyonsnalcolor/product/controller/PbProductController.java +++ b/src/main/java/com/pyonsnalcolor/product/controller/PbProductController.java @@ -8,6 +8,8 @@ import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -27,15 +29,16 @@ public ResponseEntity> getPbProductsByFilter( @RequestParam String storeType, @RequestBody ProductFilterRequestDto productFilterRequestDto ) { - Page products = pbProductService.getFilteredProducts( - pageNumber, pageSize, storeType, productFilterRequestDto); + Pageable pageable = PageRequest.of(pageNumber, pageSize); + Page products = pbProductService.getPagedProductsDtoByFilter( + pageable, storeType, productFilterRequestDto); return new ResponseEntity(products, HttpStatus.OK); } - @Operation(summary = "PB 상품 단건 조회", description = "id 바탕으로 PB 상품을 조회합니다.") + @Operation(summary = "PB 상품 단건 조회", description = "id로 PB 상품을 조회합니다.") @GetMapping("/products/pb-products/{id}") public ResponseEntity getPbProduct(@PathVariable String id) { - ProductResponseDto product = pbProductService.getProduct(id); + ProductResponseDto product = pbProductService.getProductById(id); return new ResponseEntity(product, HttpStatus.OK); } } \ No newline at end of file diff --git a/src/main/java/com/pyonsnalcolor/product/enumtype/Sorted.java b/src/main/java/com/pyonsnalcolor/product/enumtype/Sorted.java index 9f06e62..ceb70f7 100644 --- a/src/main/java/com/pyonsnalcolor/product/enumtype/Sorted.java +++ b/src/main/java/com/pyonsnalcolor/product/enumtype/Sorted.java @@ -2,6 +2,7 @@ import com.pyonsnalcolor.product.entity.BaseProduct; import lombok.Getter; +import org.springframework.data.domain.Sort; import java.util.Arrays; import java.util.Comparator; @@ -13,6 +14,7 @@ public enum Sorted implements Filter { LATEST(1, "최신순", null, + Sort.by("updatedTime").descending().and(Sort.by("id").ascending()), Comparator.comparing(BaseProduct::getCreatedDate).reversed().thenComparing(BaseProduct::getId)), // VIEW(2, // "조회순", @@ -21,10 +23,12 @@ public enum Sorted implements Filter { LOW_PRICE(3, "낮은가격순", null, + Sort.by("price").ascending().and(Sort.by("id").ascending()), Comparator.comparing(BaseProduct::getPrice).thenComparing(BaseProduct::getId)), HIGH_PRICE(4, "높은가격순", null, + Sort.by("price").descending().and(Sort.by("id").ascending()), Comparator.comparing(BaseProduct::getPrice).reversed().thenComparing(BaseProduct::getId)); // REVIEW(5, // "리뷰순", @@ -34,14 +38,16 @@ public enum Sorted implements Filter { private final int code; private final String korean; private final String image; + private final Sort sort; private final Comparator comparator; private static final String FILTER_TYPE = "sort"; - Sorted(int code, String korean, String image, Comparator comparator) { + Sorted(int code, String korean, String image, Sort sort, Comparator comparator) { this.code = code; this.korean = korean; this.image = image; + this.sort = sort; this.comparator = comparator; } @@ -52,16 +58,6 @@ private static Sorted matchSortedByCode(int code) { .orElse(null); } - public static Comparator getCategoryFilteredComparator(List filterList) { - Comparator filter = Sorted.findComparatorByFilterList(filterList); - - if (Sorted.LATEST.getComparator() == filter) { - return BaseProduct.getCategoryComparator() - .thenComparing(filter); - } - return filter; - } - public static Comparator findComparatorByFilterList(List filterList) { return filterList.stream() .map(Sorted::matchSortedByCode) @@ -71,6 +67,15 @@ public static Comparator findComparatorByFilterList(List f .getComparator(); } + public static Sort findSortByFilterList(List filterList) { + return filterList.stream() + .map(Sorted::matchSortedByCode) + .filter(Objects::nonNull) + .findFirst() + .orElse(LATEST) + .getSort(); + } + @Override public String getFilterType() { return FILTER_TYPE; diff --git a/src/main/java/com/pyonsnalcolor/product/service/EventProductService.java b/src/main/java/com/pyonsnalcolor/product/service/EventProductService.java index ecf5da6..ec2b1c5 100644 --- a/src/main/java/com/pyonsnalcolor/product/service/EventProductService.java +++ b/src/main/java/com/pyonsnalcolor/product/service/EventProductService.java @@ -2,17 +2,20 @@ import com.pyonsnalcolor.exception.PyonsnalcolorProductException; import com.pyonsnalcolor.product.dto.ProductFilterRequestDto; -import com.pyonsnalcolor.product.dto.ProductResponseDto; import com.pyonsnalcolor.product.entity.BaseEventProduct; import com.pyonsnalcolor.product.enumtype.*; import com.pyonsnalcolor.product.repository.EventProductRepository; import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.aggregation.Aggregation; +import org.springframework.data.mongodb.core.aggregation.AggregationResults; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.data.support.PageableExecutionUtils; import org.springframework.stereotype.Service; import java.util.Collection; -import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -23,37 +26,38 @@ @Service public class EventProductService extends ProductService{ - public EventProductService(EventProductRepository eventProductRepository) { - super(eventProductRepository); - } - - @Override - public Page getFilteredProducts( - int pageNumber, int pageSize, String storeType, ProductFilterRequestDto productFilterRequestDto - ) { - List filterList = productFilterRequestDto.getFilterList(); - validateProductFilterCodes(filterList); - - Comparator comparator = Sorted.getCategoryFilteredComparator(filterList); - List eventProducts = getProductsListByFilter(storeType, filterList, BaseEventProduct.class); - eventProducts.sort(comparator); + private static final List FILTER_CODES = Stream.of( + Filter.getCodes(Category.class), + Filter.getCodes(Sorted.class), + Filter.getCodes(EventType.class)) + .flatMap(Collection::stream) + .collect(Collectors.toList()); - List responseDtos = convertToResponseDtoList(eventProducts); - PageRequest pageRequest = PageRequest.of(pageNumber, pageSize); - return convertToPage(responseDtos, pageRequest); + public EventProductService(EventProductRepository eventProductRepository, MongoTemplate mongoTemplate) { + super(eventProductRepository, mongoTemplate); } @Override protected void validateProductFilterCodes(List filterList) { - List codes = Stream.of( - Filter.getCodes(Category.class), - Filter.getCodes(Sorted.class), - Filter.getCodes(EventType.class)) - .flatMap(Collection::stream) - .collect(Collectors.toList()); - - if (!codes.containsAll(filterList)) { + if (!FILTER_CODES.containsAll(filterList)) { throw new PyonsnalcolorProductException(INVALID_FILTER_CODE); } } + + @Override + protected Page getPagedProductsByFilter( + Pageable pageable, String storeType, ProductFilterRequestDto productFilterRequestDto + ) { + Aggregation aggregation = getAggregation(pageable, storeType, productFilterRequestDto); + + AggregationResults results = mongoTemplate.aggregate( + aggregation, "event_product", BaseEventProduct.class + ); + + return PageableExecutionUtils.getPage( + results.getMappedResults(), + pageable, + () -> mongoTemplate.count(new Query(), BaseEventProduct.class) + ); + } } \ No newline at end of file diff --git a/src/main/java/com/pyonsnalcolor/product/service/PbProductService.java b/src/main/java/com/pyonsnalcolor/product/service/PbProductService.java index 0c3d050..a3e8952 100644 --- a/src/main/java/com/pyonsnalcolor/product/service/PbProductService.java +++ b/src/main/java/com/pyonsnalcolor/product/service/PbProductService.java @@ -3,53 +3,101 @@ import com.pyonsnalcolor.exception.PyonsnalcolorProductException; import com.pyonsnalcolor.exception.model.CommonErrorCode; import com.pyonsnalcolor.product.dto.ProductFilterRequestDto; -import com.pyonsnalcolor.product.dto.ProductResponseDto; import com.pyonsnalcolor.product.entity.BasePbProduct; import com.pyonsnalcolor.product.enumtype.*; import com.pyonsnalcolor.product.repository.PbProductRepository; import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.aggregation.*; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; +import org.springframework.data.support.PageableExecutionUtils; import org.springframework.stereotype.Service; import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; +import static org.springframework.data.mongodb.core.aggregation.Aggregation.*; + @Service public class PbProductService extends ProductService { - public PbProductService(PbProductRepository pbProductRepository) { - super(pbProductRepository); + private static final String CATEGORY_GOODS_FIELD = "categoryGoods"; + private static final List FILTER_CODES = Stream.of( + Filter.getCodes(Category.class), + Filter.getCodes(Sorted.class), + Filter.getCodes(Recommend.class), + Filter.getCodes(EventType.class)) + .flatMap(Collection::stream) + .collect(Collectors.toList()); + + public PbProductService(PbProductRepository pbProductRepository, MongoTemplate mongoTemplate) { + super(pbProductRepository, mongoTemplate); } @Override - public Page getFilteredProducts( - int pageNumber, int pageSize, String storeType, ProductFilterRequestDto productFilterRequestDto + protected void validateProductFilterCodes(List filterList) { + if (!FILTER_CODES.containsAll(filterList)) { + throw new PyonsnalcolorProductException(CommonErrorCode.INVALID_FILTER_CODE); + } + } + + @Override + protected Page getPagedProductsByFilter( + Pageable pageable, String storeType, ProductFilterRequestDto productFilterRequestDto ) { - List filterList = productFilterRequestDto.getFilterList(); - validateProductFilterCodes(filterList); + Aggregation aggregation = getAggregationBySortCondition(pageable, storeType, productFilterRequestDto); - Comparator comparator = Sorted.getCategoryFilteredComparator(filterList); - List responseDtos = getProductsListByFilter(storeType, filterList, BasePbProduct.class); - responseDtos.sort(comparator); + AggregationResults aggregationResults = mongoTemplate.aggregate( + aggregation, "pb_product", BasePbProduct.class + ); - List result = convertToResponseDtoList(responseDtos); - PageRequest pageRequest = PageRequest.of(pageNumber, pageSize); - return convertToPage(result, pageRequest); + return PageableExecutionUtils.getPage( + aggregationResults.getMappedResults(), + pageable, + () -> mongoTemplate.count(new Query(), BasePbProduct.class) + ); } - @Override - protected void validateProductFilterCodes(List filterList) { - List codes = Stream.of( - Filter.getCodes(Category.class), - Filter.getCodes(Sorted.class), - Filter.getCodes(Recommend.class), - Filter.getCodes(EventType.class)) - .flatMap(Collection::stream) - .collect(Collectors.toList()); - - if (!codes.containsAll(filterList)) { - throw new PyonsnalcolorProductException(CommonErrorCode.INVALID_FILTER_CODE); - } + private Aggregation getAggregationBySortCondition( + Pageable pageable, String storeType, ProductFilterRequestDto productFilterRequestDto + ) { + Sort sort = Sorted.findSortByFilterList(productFilterRequestDto.getFilterList()); + + if (sort == Sorted.LATEST.getSort()) { + return getAggregationSortCategoryOfGoodsLast(pageable, storeType, productFilterRequestDto); + } return getAggregation(pageable, storeType, productFilterRequestDto); + } + + private Aggregation getAggregationSortCategoryOfGoodsLast( + Pageable pageable, String storeType, ProductFilterRequestDto productFilterRequestDto + ) { + List filterList = productFilterRequestDto.getFilterList(); + Criteria criteria = createCriteriaByFilter(storeType, filterList); + Sort sort = Sorted.findSortByFilterList(filterList); + + return newAggregation( + match(criteria), + addGoodsCategoryField(), + sort(Sort.Direction.ASC, CATEGORY_GOODS_FIELD), + project().andExclude(CATEGORY_GOODS_FIELD), + sort(sort), + skip(pageable.getOffset()), + limit(pageable.getPageSize()) + ); + } + + private AggregationOperation addGoodsCategoryField() { + return addFields() + .addField(CATEGORY_GOODS_FIELD) + .withValue( + ConditionalOperators.when( + ComparisonOperators.Eq.valueOf("category").equalToValue("GOODS")) + .then(1) + .otherwise(0)) + .build(); } } \ No newline at end of file diff --git a/src/main/java/com/pyonsnalcolor/product/service/ProductService.java b/src/main/java/com/pyonsnalcolor/product/service/ProductService.java index 2b52633..b122eb1 100644 --- a/src/main/java/com/pyonsnalcolor/product/service/ProductService.java +++ b/src/main/java/com/pyonsnalcolor/product/service/ProductService.java @@ -6,62 +6,63 @@ import com.pyonsnalcolor.product.enumtype.*; import com.pyonsnalcolor.product.repository.BasicProductRepository; import lombok.RequiredArgsConstructor; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageImpl; -import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.aggregation.Aggregation; import org.springframework.data.mongodb.core.query.Criteria; -import org.springframework.data.mongodb.core.query.Query; import java.util.*; -import java.util.stream.Collectors; + +import static org.springframework.data.mongodb.core.aggregation.Aggregation.*; +import static org.springframework.data.mongodb.core.aggregation.Aggregation.match; @RequiredArgsConstructor public abstract class ProductService { - protected final BasicProductRepository basicProductRepository; - @Autowired - private MongoTemplate mongoTemplate; + protected final BasicProductRepository basicProductRepository; + protected final MongoTemplate mongoTemplate; - public ProductResponseDto getProduct(String id) { + public ProductResponseDto getProductById(String id) { Optional baseProduct = basicProductRepository.findById(id); baseProduct.orElseThrow(NoSuchElementException::new); return baseProduct.get().convertToDto(); } - protected List convertToResponseDtoList(List products) { - return products.stream() - .map(BaseProduct::convertToDto) - .collect(Collectors.toList()); - } - - protected abstract Page getFilteredProducts( - int pageNumber, int pageSize, String storeType, ProductFilterRequestDto productFilterRequestDto); - protected abstract void validateProductFilterCodes(List filterList); - protected Page convertToPage(List list, PageRequest pageRequest) { - int start = (int) pageRequest.getOffset(); - int end = Math.min((start + pageRequest.getPageSize()), list.size()); + public Page getPagedProductsDtoByFilter( + Pageable pageable, String storeType, ProductFilterRequestDto productFilterRequestDto) { - return new PageImpl<>(list.subList(start, end), pageRequest, list.size()); + return getPagedProductsByFilter(pageable, storeType, productFilterRequestDto) + .map(p -> (T) p.convertToDto()); } - protected List getProductsListByFilter( - String storeType, List filterList, Class productClass + protected abstract Page getPagedProductsByFilter( + Pageable pageable, String storeType, ProductFilterRequestDto productFilterRequestDto); + + protected Aggregation getAggregation( + Pageable pageable, String storeType, ProductFilterRequestDto productFilterRequestDto ) { - Query query = createQueryByFilter(storeType, filterList); - return mongoTemplate.find(query, productClass); + List filterList = productFilterRequestDto.getFilterList(); + Criteria criteria = createCriteriaByFilter(storeType, filterList); + Sort sort = Sorted.findSortByFilterList(filterList); + + return newAggregation( + match(criteria), + sort(sort), + skip(pageable.getOffset()), + limit(pageable.getPageSize()) + ); } - private Query createQueryByFilter(String storeType, List filterList) { + protected Criteria createCriteriaByFilter(String storeType, List filterList) { List recommends = Filter.findEnumByFilterList(Recommend.class, filterList); List categories = Filter.findEnumByFilterList(Category.class, filterList); List eventTypes = Filter.findEnumByFilterList(EventType.class, filterList); - Criteria criteria = createFilterCriteria(recommends, categories, eventTypes, storeType); - return new Query(criteria); + return createFilterCriteria(recommends, categories, eventTypes, storeType); } private Criteria createFilterCriteria(List recommends, List categories, From b2882d0cf37a900c471e15a610c553847afa6b08 Mon Sep 17 00:00:00 2001 From: sooyoungh Date: Fri, 18 Aug 2023 15:59:15 +0900 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20=EC=A0=95=EB=A0=AC=ED=95=98=EB=8A=94?= =?UTF-8?q?=20=EC=95=84=EC=9D=B4=EB=94=94=20=ED=95=84=EB=93=9C=EB=AA=85=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/pyonsnalcolor/product/enumtype/Sorted.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/pyonsnalcolor/product/enumtype/Sorted.java b/src/main/java/com/pyonsnalcolor/product/enumtype/Sorted.java index ceb70f7..30b5a80 100644 --- a/src/main/java/com/pyonsnalcolor/product/enumtype/Sorted.java +++ b/src/main/java/com/pyonsnalcolor/product/enumtype/Sorted.java @@ -14,7 +14,7 @@ public enum Sorted implements Filter { LATEST(1, "최신순", null, - Sort.by("updatedTime").descending().and(Sort.by("id").ascending()), + Sort.by("updatedTime").descending().and(Sort.by("_id").ascending()), Comparator.comparing(BaseProduct::getCreatedDate).reversed().thenComparing(BaseProduct::getId)), // VIEW(2, // "조회순", @@ -23,12 +23,12 @@ public enum Sorted implements Filter { LOW_PRICE(3, "낮은가격순", null, - Sort.by("price").ascending().and(Sort.by("id").ascending()), + Sort.by("price").ascending().and(Sort.by("_id").ascending()), Comparator.comparing(BaseProduct::getPrice).thenComparing(BaseProduct::getId)), HIGH_PRICE(4, "높은가격순", null, - Sort.by("price").descending().and(Sort.by("id").ascending()), + Sort.by("price").descending().and(Sort.by("_id").ascending()), Comparator.comparing(BaseProduct::getPrice).reversed().thenComparing(BaseProduct::getId)); // REVIEW(5, // "리뷰순", From 3f46b22ad72555f5de95028d813cc4523fec5ebe Mon Sep 17 00:00:00 2001 From: sooyoungh Date: Sat, 19 Aug 2023 22:29:59 +0900 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20=ED=8E=98=EC=9D=B4=EC=A7=95=20?= =?UTF-8?q?=EC=A0=84=EC=B2=B4=20=EB=8D=B0=EC=9D=B4=ED=84=B0=20=EA=B0=AF?= =?UTF-8?q?=EC=88=98=20=EC=A1=B0=ED=9A=8C=20=EC=BF=BC=EB=A6=AC=20=EC=88=98?= =?UTF-8?q?=EC=A0=95,=20=EC=A0=95=EB=A0=AC=20=EC=88=9C=EC=84=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../product/service/EventProductService.java | 8 +++++--- .../product/service/PbProductService.java | 11 +++++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/pyonsnalcolor/product/service/EventProductService.java b/src/main/java/com/pyonsnalcolor/product/service/EventProductService.java index ec2b1c5..42c66e5 100644 --- a/src/main/java/com/pyonsnalcolor/product/service/EventProductService.java +++ b/src/main/java/com/pyonsnalcolor/product/service/EventProductService.java @@ -11,6 +11,7 @@ import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.aggregation.Aggregation; import org.springframework.data.mongodb.core.aggregation.AggregationResults; +import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.support.PageableExecutionUtils; import org.springframework.stereotype.Service; @@ -50,14 +51,15 @@ protected Page getPagedProductsByFilter( ) { Aggregation aggregation = getAggregation(pageable, storeType, productFilterRequestDto); - AggregationResults results = mongoTemplate.aggregate( + AggregationResults aggregationResults = mongoTemplate.aggregate( aggregation, "event_product", BaseEventProduct.class ); + Criteria criteria = createCriteriaByFilter(storeType, productFilterRequestDto.getFilterList()); return PageableExecutionUtils.getPage( - results.getMappedResults(), + aggregationResults.getMappedResults(), pageable, - () -> mongoTemplate.count(new Query(), BaseEventProduct.class) + () -> mongoTemplate.count(new Query(criteria), BaseEventProduct.class) ); } } \ No newline at end of file diff --git a/src/main/java/com/pyonsnalcolor/product/service/PbProductService.java b/src/main/java/com/pyonsnalcolor/product/service/PbProductService.java index a3e8952..28ebe45 100644 --- a/src/main/java/com/pyonsnalcolor/product/service/PbProductService.java +++ b/src/main/java/com/pyonsnalcolor/product/service/PbProductService.java @@ -55,10 +55,11 @@ protected Page getPagedProductsByFilter( aggregation, "pb_product", BasePbProduct.class ); + Criteria criteria = createCriteriaByFilter(storeType, productFilterRequestDto.getFilterList()); return PageableExecutionUtils.getPage( aggregationResults.getMappedResults(), pageable, - () -> mongoTemplate.count(new Query(), BasePbProduct.class) + () -> mongoTemplate.count(new Query(criteria), BasePbProduct.class) ); } @@ -77,14 +78,16 @@ private Aggregation getAggregationSortCategoryOfGoodsLast( ) { List filterList = productFilterRequestDto.getFilterList(); Criteria criteria = createCriteriaByFilter(storeType, filterList); - Sort sort = Sorted.findSortByFilterList(filterList); + Sort sortByFilterList = Sorted.findSortByFilterList(filterList); + + SortOperation sortStage = Aggregation.sort(Sort.Direction.ASC, CATEGORY_GOODS_FIELD); + sortStage.and(sortByFilterList); return newAggregation( match(criteria), addGoodsCategoryField(), - sort(Sort.Direction.ASC, CATEGORY_GOODS_FIELD), + sort(Sort.Direction.ASC, CATEGORY_GOODS_FIELD).and(sortByFilterList), project().andExclude(CATEGORY_GOODS_FIELD), - sort(sort), skip(pageable.getOffset()), limit(pageable.getPageSize()) );