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
134 lines (112 loc) · 4.16 KB
/
Copy pathRankingFacade.java
File metadata and controls
134 lines (112 loc) · 4.16 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package com.loopers.application.ranking;
import com.loopers.domain.product.Product;
import com.loopers.domain.product.ProductRepository;
import com.loopers.domain.ranking.RankingEntry;
import com.loopers.domain.ranking.RankingInfo;
import com.loopers.domain.ranking.RankingService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.time.Clock;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Slf4j
@Component
@RequiredArgsConstructor
public class RankingFacade {
private final RankingService rankingService;
private final ProductRepository productRepository;
private final Clock clock;
/**
* 랭킹 페이지 조회
*/
@Transactional(readOnly = true)
public RankingPageInfo getRankingPage(RankingCommand command) {
List<RankingEntry> entries = rankingService.getRankingPage(
command.date(),
command.page(),
command.size()
);
if (entries.isEmpty()) {
return RankingPageInfo.empty(command.date(), command.page(), command.size());
}
// 상품 정보 조회
List<Long> productIds = entries.stream()
.map(RankingEntry::productId)
.collect(Collectors.toList());
Map<Long, Product> productMap = productRepository.findAllByIds(productIds).stream()
.collect(Collectors.toMap(Product::getId, p -> p));
// 랭킹 정보 조합
List<RankingInfo> rankings = new ArrayList<>();
long startRank = (long) command.page() * command.size() + 1;
for (int i = 0; i < entries.size(); i++) {
RankingEntry entry = entries.get(i);
Product product = productMap.get(entry.productId());
if (product != null) {
rankings.add(RankingInfo.of(
product.getId(),
product.getName(),
product.getPriceValue(),
product.getBrand().getName(),
startRank + i,
entry.score()
));
}
}
Long totalCount = rankingService.getRankingSize(command.date());
return RankingPageInfo.of(
rankings,
command.date(),
command.page(),
command.size(),
totalCount
);
}
/**
* Top-N 랭킹 조회
*/
@Transactional(readOnly = true)
public List<RankingInfo> getTopN(LocalDate date, int n) {
List<RankingEntry> entries = rankingService.getTopNWithScores(date, n);
if (entries.isEmpty()) {
return List.of();
}
List<Long> productIds = entries.stream()
.map(RankingEntry::productId)
.collect(Collectors.toList());
Map<Long, Product> productMap = productRepository.findAllByIds(productIds).stream()
.collect(Collectors.toMap(Product::getId, p -> p));
List<RankingInfo> rankings = new ArrayList<>();
for (int i = 0; i < entries.size(); i++) {
RankingEntry entry = entries.get(i);
Product product = productMap.get(entry.productId());
if (product != null) {
rankings.add(RankingInfo.of(
product.getId(),
product.getName(),
product.getPriceValue(),
product.getBrand().getName(),
(long) (i + 1),
entry.score()
));
}
}
return rankings;
}
/**
* 특정 상품의 순위 조회
*/
public Long getProductRank(Long productId, LocalDate date) {
return rankingService.getRank(productId, date);
}
/**
* 특정 상품의 순위 조회 (오늘 기준)
*/
public Long getProductRankToday(Long productId) {
return rankingService.getRank(productId, LocalDate.now(clock));
}
}