|
2 | 2 |
|
3 | 3 | import java.util.ArrayList; |
4 | 4 | import java.util.Collections; |
| 5 | +import java.util.HashSet; |
5 | 6 | import java.util.List; |
| 7 | +import java.util.Set; |
6 | 8 | import java.util.stream.Collectors; |
7 | 9 |
|
8 | 10 | import org.springframework.stereotype.Service; |
@@ -46,11 +48,13 @@ public List<CategoryResponse> getAllCategories() { |
46 | 48 |
|
47 | 49 | // Cache Miss → DB 조회 |
48 | 50 | List<Category> parentCategories = categoryRepository.findAllParentCategoriesWithSub(); |
| 51 | + Set<Long> activeCategoryIds = new HashSet<>(productRepository.findCategoryIdsWithOnSaleProducts()); |
| 52 | + |
49 | 53 | List<CategoryResponse> response = parentCategories.stream() |
50 | 54 | .map(parent -> { |
51 | | - // 상품이 있는 하위 카테고리만 필터링 |
| 55 | + // 판매 중인 상품이 있는 하위 카테고리만 필터링 |
52 | 56 | List<SubCategoryResponse> filteredSubs = parent.getSubCategories().stream() |
53 | | - .filter(sub -> productRepository.existsByCategoryIdAndOnSaleTrue(sub.getId())) |
| 57 | + .filter(sub -> activeCategoryIds.contains(sub.getId())) |
54 | 58 | .map(categoryConverter::toSubCategoryResponse) |
55 | 59 | .collect(Collectors.toList()); |
56 | 60 |
|
@@ -100,9 +104,10 @@ public List<CategoryRandomResponse> getRandomCategories(int count) { |
100 | 104 | // 추천 하위 카테고리 조회 (상품이 있는 하위 카테고리만) |
101 | 105 | public List<CategorySimpleResponse> getRecommendedSubCategories(int count) { |
102 | 106 | List<Category> subCategories = categoryRepository.findAllSubCategories(); |
| 107 | + Set<Long> activeCategoryIds = new HashSet<>(productRepository.findCategoryIdsWithOnSaleProducts()); |
103 | 108 |
|
104 | 109 | return subCategories.stream() |
105 | | - .filter(category -> productRepository.existsByCategoryIdAndOnSaleTrue(category.getId())) |
| 110 | + .filter(category -> activeCategoryIds.contains(category.getId())) |
106 | 111 | .limit(count) |
107 | 112 | .map(categoryConverter::toSimpleResponse) |
108 | 113 | .collect(Collectors.toList()); |
|
0 commit comments