Skip to content

Commit f6f6815

Browse files
committed
fix: getRandomCategories 사전 필터링 적용
1 parent 7fe5739 commit f6f6815

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/main/java/com/ongil/backend/domain/category/service/CategoryService.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,26 @@ public List<SubCategoryResponse> getSubCategories(Long parentCategoryId) {
8686
// 랜덤 카테고리 조회 (상품이 있는 카테고리만)
8787
public List<CategoryRandomResponse> getRandomCategories(int count) {
8888
List<Category> allCategories = categoryRepository.findAllByOrderByDisplayOrder();
89+
Set<Long> activeCategoryIds = new HashSet<>(productRepository.findCategoryIdsWithOnSaleProducts());
90+
91+
// 상품이 있는 카테고리만 사전 필터링 (상위 카테고리는 하위 중 하나라도 있으면 포함)
92+
List<Category> activeCategories = allCategories.stream()
93+
.filter(category -> {
94+
if (category.getParentCategory() != null) {
95+
// 하위 카테고리: 직접 확인
96+
return activeCategoryIds.contains(category.getId());
97+
}
98+
// 상위 카테고리: 하위 카테고리 중 하나라도 상품이 있으면 포함
99+
return category.getSubCategories().stream()
100+
.anyMatch(sub -> activeCategoryIds.contains(sub.getId()));
101+
})
102+
.collect(Collectors.toList());
89103

90-
List<Category> shuffledCategories = new ArrayList<>(allCategories);
91-
Collections.shuffle(shuffledCategories);
104+
Collections.shuffle(activeCategories);
92105

106+
// 사전 필터링된 카테고리만 썸네일 조회 (DB 호출 최소화)
93107
List<CategoryRandomResponse> result = new ArrayList<>();
94-
for (Category category : shuffledCategories) {
108+
for (Category category : activeCategories) {
95109
if (result.size() >= count) break;
96110
String thumbnailUrl = getTopProductThumbnail(category);
97111
if (thumbnailUrl != null) {

0 commit comments

Comments
 (0)