forked from Loopers-dev-lab/loop-pack-be-l2-vol2-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRankingFacade.java
More file actions
111 lines (94 loc) · 4.29 KB
/
RankingFacade.java
File metadata and controls
111 lines (94 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package com.loopers.application.ranking;
import com.loopers.application.ranking.RankingInfo.RankingItemInfo;
import com.loopers.application.ranking.RankingInfo.RankingPageInfo;
import com.loopers.domain.brand.Brand;
import com.loopers.domain.brand.repository.BrandRepository;
import com.loopers.domain.product.Product;
import com.loopers.domain.product.repository.ProductRepository;
import com.loopers.infrastructure.cache.ProductRankingCache;
import com.loopers.infrastructure.cache.ProductRankingCache.RankingEntry;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
@Slf4j
@RequiredArgsConstructor
@Component
@Transactional(readOnly = true)
public class RankingFacade {
private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd");
private final ProductRankingCache productRankingCache;
private final ProductRepository productRepository;
private final BrandRepository brandRepository;
/** @param page 0-based */
public RankingPageInfo getRankings(String date, int page, int size) {
// 날짜 검증 및 기본값 처리
String targetDate = validateAndNormalizeDate(date);
// 1. ZSET에서 랭킹 조회
List<RankingEntry> rankingEntries = productRankingCache.getTopRankings(targetDate, page, size);
if (rankingEntries.isEmpty()) {
return RankingPageInfo.of(Collections.emptyList(), targetDate, page, size, 0);
}
// 2. 상품 ID 목록 추출
List<Long> productIds = rankingEntries.stream()
.map(RankingEntry::productId)
.toList();
// 3. 상품 정보 조회
List<Product> products = productRepository.findByIdIn(productIds);
Map<Long, Product> productMap = products.stream()
.collect(Collectors.toMap(Product::getId, Function.identity()));
// 4. 브랜드 정보 조회 (N+1 방지)
List<Long> brandIds = products.stream()
.map(Product::getBrandId)
.distinct()
.toList();
List<Brand> brands = brandRepository.findByIdIn(brandIds);
Map<Long, Brand> brandMap = brands.stream()
.collect(Collectors.toMap(Brand::getId, Function.identity()));
// 5. 응답 생성
List<RankingItemInfo> rankings = rankingEntries.stream()
.map(entry -> {
Product product = productMap.get(entry.productId());
if (product == null) {
log.warn("[Ranking] Product not found - productId: {}", entry.productId());
return null;
}
Brand brand = brandMap.get(product.getBrandId());
String brandName = brand != null ? brand.getName() : "Unknown";
return new RankingItemInfo(
entry.rank(),
product.getId(),
product.getName(),
brandName,
product.getPrice(),
product.getLikeCount(),
entry.score()
);
})
.filter(item -> item != null)
.toList();
// 6. 전체 개수 조회
long totalCount = productRankingCache.getTotalCount(targetDate);
return RankingPageInfo.of(rankings, targetDate, page, size, totalCount);
}
/** @throws IllegalArgumentException 유효하지 않은 날짜 형식 */
private String validateAndNormalizeDate(String date) {
if (date == null || date.isBlank()) {
return productRankingCache.getTodayDate();
}
try {
LocalDate.parse(date, DATE_FORMATTER);
return date;
} catch (DateTimeParseException e) {
throw new IllegalArgumentException("Invalid date format. Expected yyyyMMdd, got: " + date);
}
}
}