Skip to content

Commit ccc015e

Browse files
committed
refactor: 상품 상세 정보 API 수정
* 상품 상세 정보 조회 시 랭킹 정보도 함께 조회하도록 수정하면서 기존 API 수정 - 반환타입 수정 - Facade 수정 - DTO 수정
1 parent b397ce4 commit ccc015e

6 files changed

Lines changed: 60 additions & 6 deletions

File tree

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import org.springframework.transaction.annotation.Transactional;
1818

1919
import java.math.BigDecimal;
20+
import java.time.LocalDate;
21+
import java.time.format.DateTimeFormatter;
2022
import java.util.List;
2123

2224
@Component
@@ -25,6 +27,7 @@ public class ProductFacade {
2527
private final ProductRepository productRepository;
2628
private final BrandRepository brandRepository;
2729
private final OutboxRepository outBoxRepository;
30+
private final RankingRedisReader rankingRedisReader;
2831

2932
@Transactional
3033
public ProductInfo registerProduct(ProductV1Dto.ProductRequest request) {
@@ -52,7 +55,7 @@ public List<ProductInfo> findAllProducts() {
5255

5356
@Transactional
5457
@Cacheable(value = "product", key = "#id")
55-
public ProductInfo findProductById(Long id) {
58+
public ProductRankingInfo findProductById(Long id) {
5659
Product product = productRepository.findById(id).orElseThrow(
5760
() -> new CoreException(ErrorType.NOT_FOUND, "찾고자 하는 상품이 존재하지 않습니다.")
5861
);
@@ -65,7 +68,14 @@ public ProductInfo findProductById(Long id) {
6568

6669
outBoxRepository.save(outBoxEvent);
6770

68-
return ProductInfo.from(product);
71+
RankingInfo ranking = null;
72+
73+
try {
74+
String date = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE);
75+
ranking = rankingRedisReader.getDailyRanking(date, product.getId());
76+
} catch (Exception ignored) {}
77+
78+
return ProductRankingInfo.from(product, ranking);
6979
}
7080

7181
@Transactional(readOnly = true)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.loopers.application.product;
2+
3+
import com.loopers.domain.product.Product;
4+
5+
import java.math.BigDecimal;
6+
7+
public record ProductRankingInfo(Long id, Long brandId, String name, BigDecimal price, int stock, int likeCount, int rank, double score) {
8+
public static ProductRankingInfo from(Product product, RankingInfo ranking) {
9+
return new ProductRankingInfo(
10+
product.getId(),
11+
product.getBrandId(),
12+
product.getName(),
13+
product.getPrice(),
14+
product.getStock(),
15+
product.getLikeCount(),
16+
ranking.rank(),
17+
ranking.score()
18+
);
19+
}
20+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.loopers.application.product;
2+
3+
public record RankingInfo(
4+
String date,
5+
double score,
6+
int rank,
7+
Long total
8+
) {
9+
}

apps/commerce-api/src/main/java/com/loopers/interfaces/api/product/ProductV1ApiSpec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public interface ProductV1ApiSpec {
1515
ApiResponse<List<ProductV1Dto.ProductResponse>> findAllProducts();
1616

1717
@Operation(summary = "상품 상세 조회")
18-
ApiResponse<ProductV1Dto.ProductResponse> findProductById(Long id);
18+
ApiResponse<ProductV1Dto.ProductRankingResponse> findProductById(Long id);
1919

2020
@Operation(summary = "상품 정렬 조회")
2121
ApiResponse<List<ProductV1Dto.ProductResponse>> findProductsBySortCondition(ProductV1Dto.SearchProductRequest request);

apps/commerce-api/src/main/java/com/loopers/interfaces/api/product/ProductV1Controller.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.loopers.application.product.ProductFacade;
44
import com.loopers.application.product.ProductInfo;
5+
import com.loopers.application.product.ProductRankingInfo;
56
import com.loopers.interfaces.api.ApiResponse;
67
import lombok.RequiredArgsConstructor;
78
import org.springframework.web.bind.annotation.GetMapping;
@@ -42,9 +43,9 @@ public ApiResponse<List<ProductV1Dto.ProductResponse>> findAllProducts() {
4243

4344
@GetMapping("/{id}")
4445
@Override
45-
public ApiResponse<ProductV1Dto.ProductResponse> findProductById(@PathVariable Long id) {
46-
ProductInfo info = productFacade.findProductById(id);
47-
ProductV1Dto.ProductResponse response = ProductV1Dto.ProductResponse.from(info);
46+
public ApiResponse<ProductV1Dto.ProductRankingResponse> findProductById(@PathVariable Long id) {
47+
ProductRankingInfo info = productFacade.findProductById(id);
48+
ProductV1Dto.ProductRankingResponse response = ProductV1Dto.ProductRankingResponse.from(info);
4849

4950
return ApiResponse.success(response);
5051
}

apps/commerce-api/src/main/java/com/loopers/interfaces/api/product/ProductV1Dto.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
package com.loopers.interfaces.api.product;
22

33
import com.loopers.application.product.ProductInfo;
4+
import com.loopers.application.product.ProductRankingInfo;
45
import com.loopers.domain.product.Product;
56
import com.loopers.support.error.CoreException;
67
import com.loopers.support.error.ErrorType;
78

89
import java.math.BigDecimal;
910

1011
public class ProductV1Dto {
12+
public record ProductRankingResponse(Long id, Long brandId, String name, BigDecimal price, int stock, int rank, double score) {
13+
public static ProductRankingResponse from(ProductRankingInfo info) {
14+
return new ProductRankingResponse(
15+
info.id(),
16+
info.brandId(),
17+
info.name(),
18+
info.price(),
19+
info.stock(),
20+
info.rank(),
21+
info.score()
22+
);
23+
}
24+
}
1125
public record ProductResponse(Long id, Long brandId, String name, BigDecimal price, int stock, int likeCount) {
1226
public static ProductResponse from(ProductInfo info) {
1327
return new ProductResponse(

0 commit comments

Comments
 (0)