Skip to content

Commit 08f7cfe

Browse files
committed
feature: 상품 상세 정보에 랭킹 데이터 추가
- `RankingService` 메서드 `getProductRank` 구현으로 Redis 기반 상품 랭킹 조회 지원 - 상품 상세 조회 시 현재 랭킹 정보를 포함하도록 `ProductInfo` 및 `ProductFacade` 수정 - 기존 `RankingService` 패키지 위치를 `infrastructure`로 변경하여 관리 개선
1 parent 6efdc4f commit 08f7cfe

4 files changed

Lines changed: 26 additions & 8 deletions

File tree

apps/commerce-api/src/main/java/com/loopers/application/product/ProductFacade.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.loopers.domain.brand.BrandService;
44
import com.loopers.domain.product.Product;
55
import com.loopers.domain.product.ProductService;
6+
import com.loopers.infrastructure.rank.RankingService;
67
import com.loopers.event.ProductViewEvent;
78
import lombok.RequiredArgsConstructor;
89
import org.springframework.context.ApplicationEventPublisher;
@@ -16,14 +17,15 @@ public class ProductFacade {
1617

1718
private final ProductService productService;
1819
private final BrandService brandService;
20+
private final RankingService rankingService;
1921
private final ApplicationEventPublisher eventPublisher;
2022

2123
public Page<ProductInfo> getProductsInfo(Pageable pageable) {
2224
Page<Product> products = productService.getProducts(pageable);
2325
return products.map(product -> {
2426
String brandName = brandService.getBrand(product.getBrandId())
2527
.getName();
26-
return ProductInfo.from(product, brandName);
28+
return ProductInfo.from(product, brandName, null);
2729
});
2830
}
2931

@@ -32,9 +34,11 @@ public ProductInfo getProductInfo(long id) {
3234
String brandName = brandService.getBrand(product.getBrandId())
3335
.getName();
3436

37+
Integer currentRank = rankingService.getProductRank(id);
38+
3539
eventPublisher.publishEvent(ProductViewEvent.from(id));
3640

37-
return ProductInfo.from(product, brandName);
41+
return ProductInfo.from(product, brandName, currentRank);
3842
}
3943

4044
}

apps/commerce-api/src/main/java/com/loopers/application/product/ProductInfo.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,28 @@ public record ProductInfo(
88
String name,
99
Money price,
1010
String brandName,
11-
int likeCount
11+
int likeCount,
12+
Integer currentRank
1213
) {
1314
public static ProductInfo from(Product product) {
1415
return new ProductInfo(
1516
product.getId(),
1617
product.getName(),
1718
product.getPrice(),
1819
null,
19-
product.getLikeCount()
20+
product.getLikeCount(),
21+
null
2022
);
2123
}
2224

23-
public static ProductInfo from(Product product, String brandName) {
25+
public static ProductInfo from(Product product, String brandName, Integer currentRank) {
2426
return new ProductInfo(
2527
product.getId(),
2628
product.getName(),
2729
product.getPrice(),
2830
brandName,
29-
product.getLikeCount()
31+
product.getLikeCount(),
32+
currentRank
3033
);
3134
}
3235
}

apps/commerce-api/src/main/java/com/loopers/application/rank/RankingFacade.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.loopers.domain.product.Product;
44
import com.loopers.domain.product.ProductService;
5-
import com.loopers.domain.rank.RankingService;
5+
import com.loopers.infrastructure.rank.RankingService;
66
import java.util.List;
77
import java.util.Map;
88
import java.util.stream.Collectors;

apps/commerce-api/src/main/java/com/loopers/domain/rank/RankingService.java renamed to apps/commerce-api/src/main/java/com/loopers/infrastructure/rank/RankingService.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
package com.loopers.domain.rank;
1+
package com.loopers.infrastructure.rank;
22

3+
import java.time.LocalDateTime;
4+
import java.time.format.DateTimeFormatter;
35
import java.util.List;
46
import java.util.Set;
57
import lombok.RequiredArgsConstructor;
@@ -27,4 +29,13 @@ public List<Long> getTopRankingIds(String date, int page, int size) {
2729
.map(Long::valueOf)
2830
.toList();
2931
}
32+
33+
public Integer getProductRank(Long productId) {
34+
String today = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
35+
String key = "ranking:all:" + today;
36+
37+
Long rank = redisTemplate.opsForZSet().reverseRank(key, String.valueOf(productId));
38+
39+
return (rank != null) ? rank.intValue() + 1 : null;
40+
}
3041
}

0 commit comments

Comments
 (0)