-
Notifications
You must be signed in to change notification settings - Fork 34
[volume-9] Product Ranking with Redis #216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1dcbe11
feat: μΌκ° λνΉ μ‘°ν API μΆκ°
yeonsu00 806feda
feat: Outbox OrderCreated μ΄λ²€νΈμ μ£Όλ¬Έ νλͺ© μ 보 μΆκ°
yeonsu00 b551192
feat: μ’μμ μ, μ‘°ν μ, μ£Όλ¬Έ μμ± μ΄λ²€νΈ 컨μλ¨Έμμ λνΉ μ§κ³ λ‘μ§ μΆκ°
yeonsu00 e52fd0f
feat: μ½λμ€ννΈ μνλ₯Ό μν μ€μΌμ€λ¬ ꡬν
yeonsu00 ae39cab
refactor: carry over λ‘μ§ ZUNIONSTORE λͺ
λ Ήμ΄ μ¬μ©νλλ‘ μμ
yeonsu00 f845467
feat: λνΉ νμ΄μ§ μ‘°ν ꡬν
yeonsu00 9829964
feat: μν μμΈ μ‘°ν μ μν μμ λ°ν λ‘μ§ κ΅¬ν
yeonsu00 a16b2a2
test: μν μ‘°ν μ λνΉ λ°ν ν
μ€νΈ μ½λ λ°μ
yeonsu00 35d23f4
test: λνΉ API κ΄λ ¨ ν
μ€νΈ μ½λ
yeonsu00 f834a6b
test: λνΉ μ»¨μλ¨Έ κ΄λ ¨ ν
μ€νΈ μ½λ
yeonsu00 d396a9e
Merge pull request #36 from yeonsu00/feature/week9-ranking
yeonsu00 945a352
Merge pull request #37 from yeonsu00/round-9
yeonsu00 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
apps/commerce-api/src/main/java/com/loopers/application/ranking/RankingCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.loopers.application.ranking; | ||
|
|
||
| import java.time.LocalDate; | ||
|
|
||
| public class RankingCommand { | ||
|
|
||
| public record GetDailyRankingCommand( | ||
| LocalDate date, | ||
| int page, | ||
| int size | ||
| ) { | ||
| } | ||
|
|
||
| } | ||
76 changes: 76 additions & 0 deletions
76
apps/commerce-api/src/main/java/com/loopers/application/ranking/RankingFacade.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package com.loopers.application.ranking; | ||
|
|
||
| import com.loopers.domain.brand.BrandService; | ||
| import com.loopers.domain.product.Product; | ||
| import com.loopers.domain.product.ProductService; | ||
| import com.loopers.domain.ranking.Ranking; | ||
| import com.loopers.domain.ranking.RankingService; | ||
| import java.util.Objects; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| @RequiredArgsConstructor | ||
| @Component | ||
| @Slf4j | ||
| public class RankingFacade { | ||
|
|
||
| private final RankingService rankingService; | ||
| private final ProductService productService; | ||
| private final BrandService brandService; | ||
|
|
||
| public RankingInfo getDailyRanking(RankingCommand.GetDailyRankingCommand command) { | ||
| List<Ranking> rankings = rankingService.getRanking( | ||
| command.date(), | ||
| command.page(), | ||
| command.size() | ||
| ); | ||
|
|
||
| if (rankings.isEmpty()) { | ||
| return new RankingInfo(List.of()); | ||
| } | ||
|
|
||
| List<Long> productIds = rankings.stream() | ||
| .map(Ranking::productId) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| List<Product> products = productService.findProductsByIds(productIds); | ||
| Map<Long, Product> productMap = products.stream() | ||
| .collect(Collectors.toMap(Product::getId, product -> product)); | ||
|
|
||
| List<Long> brandIds = products.stream() | ||
| .map(Product::getBrandId) | ||
| .distinct() | ||
| .collect(Collectors.toList()); | ||
| Map<Long, String> brandNameMap = brandService.findBrandNamesByIds(brandIds); | ||
|
|
||
| List<RankingInfo.RankingItemInfo> rankingItemInfos = rankings.stream() | ||
| .map(ranking -> { | ||
| Product product = productMap.get(ranking.productId()); | ||
| if (product == null) { | ||
| log.warn("λνΉμ μλ μνμ΄ DBμ μ‘΄μ¬νμ§ μμ: productId={}", ranking.productId()); | ||
| return null; | ||
| } | ||
|
|
||
| String brandName = brandNameMap.getOrDefault(product.getBrandId(), "μ μ μμ"); | ||
|
|
||
| return new RankingInfo.RankingItemInfo( | ||
| ranking.productId(), | ||
| product.getName(), | ||
| brandName, | ||
| product.getPrice().getPrice(), | ||
| product.getLikeCount().getCount(), | ||
| ranking.rank(), | ||
| ranking.score() | ||
| ); | ||
| }) | ||
| .filter(Objects::nonNull) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| return new RankingInfo(rankingItemInfos); | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
apps/commerce-api/src/main/java/com/loopers/application/ranking/RankingInfo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.loopers.application.ranking; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public record RankingInfo( | ||
| List<RankingItemInfo> items | ||
| ) { | ||
| public record RankingItemInfo( | ||
| Long productId, | ||
| String productName, | ||
| String brandName, | ||
| Integer price, | ||
| Integer likeCount, | ||
| Long rank, | ||
| Double score | ||
| ) { | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
apps/commerce-api/src/main/java/com/loopers/domain/ranking/Ranking.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.loopers.domain.ranking; | ||
|
|
||
| public record Ranking( | ||
| Long productId, | ||
| Long rank, | ||
| Double score | ||
| ) { | ||
| } |
8 changes: 8 additions & 0 deletions
8
apps/commerce-api/src/main/java/com/loopers/domain/ranking/RankingItem.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.loopers.domain.ranking; | ||
|
|
||
| public record RankingItem( | ||
| Long productId, | ||
| Double score | ||
| ) { | ||
| } | ||
|
|
37 changes: 37 additions & 0 deletions
37
apps/commerce-api/src/main/java/com/loopers/domain/ranking/RankingService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package com.loopers.domain.ranking; | ||
|
|
||
| import com.loopers.infrastructure.cache.RankingCacheService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| @RequiredArgsConstructor | ||
| @Service | ||
| public class RankingService { | ||
|
|
||
| private final RankingCacheService rankingCacheService; | ||
|
|
||
| public List<Ranking> getRanking(LocalDate date, int page, int size) { | ||
| long start = (long) (page - 1) * size; | ||
| long end = start + size - 1; | ||
|
|
||
yeonsu00 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| List<RankingItem> rankingItems = rankingCacheService.getRankingRange(date, start, end); | ||
|
|
||
| if (rankingItems.isEmpty()) { | ||
| return new ArrayList<>(); | ||
| } | ||
|
|
||
| return IntStream.range(0, rankingItems.size()) | ||
| .mapToObj(i -> { | ||
| RankingItem item = rankingItems.get(i); | ||
| long rank = start + 1 + i; | ||
| return new Ranking(item.productId(), rank, item.score()); | ||
| }) | ||
| .toList(); | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.