Skip to content

Commit

Permalink
fix: 상품 검색 필터 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
sanbonai06 committed Nov 26, 2023
1 parent 476d587 commit daf2fcc
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.api.annotations.ParameterObject;
Expand Down Expand Up @@ -61,13 +62,8 @@ public SliceResponse<ProductRetrieveDTO> productSearch(

@Operation(summary = "SmallCategory(FE기준 중분류) 별 상품을 조회합니다.")
@GetMapping("/products/small-category")
public SliceResponse<ProductRetrieveDTO> getCategoricalProduct(
@ParameterObject ProductFilterCondition productFilterCondition,
@ParameterObject @PageableDefault(size = 10) Pageable pageable) {
return retrieveProductListUseCase.executeToSmallCategory(
productFilterCondition.getSmallCategoryList(),
productFilterCondition.getPriceOrder(),
productFilterCondition.getPriceFilter(),
pageable);
public List<ProductRetrieveDTO> getCategoricalProduct(
@ParameterObject ProductFilterCondition productFilterCondition) {
return retrieveProductListUseCase.executeToSmallCategory(productFilterCondition);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.transaction.annotation.Transactional;
import tify.server.api.common.slice.SliceResponse;
import tify.server.api.product.model.dto.ProductFilterCondition;
import tify.server.core.annotation.UseCase;
import tify.server.domain.domains.product.adaptor.ProductAdaptor;
import tify.server.domain.domains.product.domain.PriceFilter;
import tify.server.domain.domains.product.domain.PriceOrder;
import tify.server.domain.domains.product.domain.Product;
import tify.server.domain.domains.product.dto.ProductCategoryCondition;
import tify.server.domain.domains.product.dto.ProductRetrieveDTO;
import tify.server.domain.domains.question.adaptor.FavorQuestionAdaptor;
import tify.server.domain.domains.question.domain.FavorQuestionCategory;
import tify.server.domain.domains.user.domain.SmallCategory;

@UseCase
@RequiredArgsConstructor
Expand All @@ -25,22 +23,26 @@ public class RetrieveProductListUseCase {
private final FavorQuestionAdaptor favorQuestionAdaptor;

@Transactional(readOnly = true)
public SliceResponse<ProductRetrieveDTO> executeToSmallCategory(
List<SmallCategory> smallCategory,
PriceOrder priceOrder,
PriceFilter priceFilter,
Pageable pageable) {
public List<ProductRetrieveDTO> executeToSmallCategory(
ProductFilterCondition productFilterCondition) {
List<Long> categoryIdList = new ArrayList<>();
smallCategory.forEach(
category -> {
categoryIdList.addAll(
favorQuestionAdaptor.queryBySmallCategory(category).stream()
.map(FavorQuestionCategory::getId)
.toList());
});
return SliceResponse.of(
productAdaptor.searchBySmallCategoryId(
productFilterCondition
.getSmallCategoryList()
.forEach(
category -> {
categoryIdList.addAll(
favorQuestionAdaptor.queryBySmallCategory(category).stream()
.map(FavorQuestionCategory::getId)
.toList());
});
List<Product> results =
productAdaptor.findAllBySmallCategoryId(
new ProductCategoryCondition(
categoryIdList, priceOrder, priceFilter, pageable)));
categoryIdList,
productFilterCondition.getPriceOrder(),
productFilterCondition.getPriceFilter(),
null));
Collections.shuffle(results);
return results.stream().map(ProductRetrieveDTO::from).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ public Slice<ProductRetrieveDTO> searchBySmallCategoryId(
ProductCategoryCondition productCategoryCondition) {
return productRepository.searchBySmallCategory(productCategoryCondition);
}

public List<Product> findAllBySmallCategoryId(
ProductCategoryCondition productCategoryCondition) {
return productRepository.findAllBySmallCategory(productCategoryCondition);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
@AllArgsConstructor
public enum PriceFilter {
DEFAULT(0L, MAX_VALUE),
LESS_THAN_10000(0L, 10000L),
MORE_THAN_10000_LESS_THAN_20000(10000L, 20000L),
MORE_THAN_20000_LESS_THAN_30000(20000L, 30000L),
MORE_THAN_30000_LESS_THAN_40000(30000L, 40000L),
MORE_THAN_40000_LESS_THAN_50000(40000L, 50000L),
LESS_THAN_10000(0L, 9999L),
MORE_THAN_10000_LESS_THAN_30000(10000L, 29999L),
MORE_THAN_30000_LESS_THAN_50000(30000L, 49999L),
MORE_THAN_50000(50000L, MAX_VALUE);

final Long start;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@


import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import tify.server.domain.domains.product.domain.Product;

@Getter
@AllArgsConstructor
@Builder
public class ProductRetrieveDTO {

private Long productId;
Expand All @@ -15,4 +18,18 @@ public class ProductRetrieveDTO {
private Long price;
private String productOption;
private String imageUrl;
private String siteUrl;

public static ProductRetrieveDTO from(Product product) {
return ProductRetrieveDTO.builder()
.productId(product.getId())
.name(product.getName())
.brand(product.getBrand())
.characteristic(product.getCharacteristic())
.price(product.getPrice())
.productOption(product.getProductOption())
.imageUrl(product.getImageUrl())
.siteUrl(product.getCrawlUrl())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ public interface ProductCustomRepository {

Slice<ProductRetrieveDTO> searchBySmallCategory(
ProductCategoryCondition productCategoryCondition);

List<Product> findAllBySmallCategory(ProductCategoryCondition productCategoryCondition);
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ public Slice<ProductRetrieveDTO> searchBySmallCategory(
return SliceUtil.valueOf(products, productCategoryCondition.getPageable());
}

@Override
public List<Product> findAllBySmallCategory(ProductCategoryCondition productCategoryCondition) {
return queryFactory
.selectFrom(product)
.where(
product.favorQuestionCategoryId.in(
productCategoryCondition.getCategoryIdList()),
priceBetween(productCategoryCondition.getPriceFilter()))
.fetch();
}

private OrderSpecifier[] orderByPrice(PriceOrder priceOrder) {
List<OrderSpecifier> orderSpecifiers = new ArrayList<>();
if (priceOrder.equals(PRICE_ASC)) {
Expand All @@ -127,15 +138,11 @@ private OrderSpecifier[] orderByPrice(PriceOrder priceOrder) {

private BooleanExpression priceBetween(PriceFilter priceFilter) {
if (priceFilter.equals(LESS_THAN_10000)) {
return product.price.between(0L, 10000L);
} else if (priceFilter.equals(MORE_THAN_10000_LESS_THAN_20000)) {
return product.price.between(10000L, 20000L);
} else if (priceFilter.equals(MORE_THAN_20000_LESS_THAN_30000)) {
return product.price.between(20000L, 30000L);
} else if (priceFilter.equals(MORE_THAN_30000_LESS_THAN_40000)) {
return product.price.between(30000L, 40000L);
} else if (priceFilter.equals(MORE_THAN_40000_LESS_THAN_50000)) {
return product.price.between(40000L, 50000L);
return product.price.between(0L, 9999L);
} else if (priceFilter.equals(MORE_THAN_10000_LESS_THAN_30000)) {
return product.price.between(10000L, 29999L);
} else if (priceFilter.equals(MORE_THAN_30000_LESS_THAN_50000)) {
return product.price.between(30000L, 49999L);
} else if (priceFilter.equals(MORE_THAN_50000)) {
return product.price.between(50000L, MAX_VALUE);
} else {
Expand Down

0 comments on commit daf2fcc

Please sign in to comment.