Skip to content

Commit 6de080b

Browse files
committed
feature: ProductStockEvent에 가격 필드 추가 및 주문 이벤트 처리 개선
- ProductStockEvent에 가격(price) 필드 추가로 이벤트 정보 확장 - OrderCreatedEvent에서 상품과 가격 데이터를 함께 매핑하도록 수정 - OrderSalesAggregateListener에서 이벤트 발행 시 가격 정보를 관리하도록 개선
1 parent 4a1aa52 commit 6de080b

4 files changed

Lines changed: 20 additions & 11 deletions

File tree

apps/commerce-api/src/main/java/com/loopers/application/order/event/OrderCreatedEvent.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.List;
88
import java.util.Map;
99
import java.util.UUID;
10+
import java.util.function.Function;
1011
import java.util.stream.Collectors;
1112

1213
public record OrderCreatedEvent(
@@ -21,7 +22,7 @@ public record OrderCreatedEvent(
2122
Long couponId
2223
) {
2324

24-
public record OrderItemInfo(Long productId, int quantity, int remainStock) {
25+
public record OrderItemInfo(Long productId, int quantity, long price, int remainStock) {
2526

2627
}
2728

@@ -37,15 +38,19 @@ public static OrderCreatedEvent of(
3738
Long couponId
3839
) {
3940

40-
Map<Long, Integer> stockMap = products.stream()
41-
.collect(Collectors.toMap(Product::getId, Product::getStock));
41+
Map<Long, Product> productMap = products.stream()
42+
.collect(Collectors.toMap(Product::getId, p -> p));
4243

4344
List<OrderItemInfo> itemInfos = orderItems.stream()
44-
.map(item -> new OrderItemInfo(
45-
item.getProductId(),
46-
item.getQuantity(),
47-
stockMap.getOrDefault(item.getProductId(), 0)
48-
)).toList();
45+
.map(item -> {
46+
Product product = productMap.get(item.getProductId());
47+
return new OrderItemInfo(
48+
item.getProductId(),
49+
item.getQuantity(),
50+
product != null ? product.getPrice().getValue() : 0,
51+
product != null ? product.getStock() : 0
52+
);
53+
}).toList();
4954

5055
return new OrderCreatedEvent(
5156
UUID.randomUUID().toString(),

apps/commerce-api/src/main/java/com/loopers/application/order/event/OrderSalesAggregateListener.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.loopers.application.order.event;
22

33
import com.loopers.domain.event.OutboxService;
4-
import com.loopers.domain.product.ProductService;
54
import com.loopers.event.ProductStockEvent;
65
import lombok.RequiredArgsConstructor;
76
import org.springframework.context.ApplicationEventPublisher;
@@ -26,7 +25,8 @@ public void handleOrderCreated(OrderCreatedEvent event) {
2625
ProductStockEvent kafkaEvent = ProductStockEvent.of(
2726
item.productId(),
2827
item.quantity(),
29-
item.remainStock()
28+
item.remainStock(),
29+
item.price()
3030
);
3131

3232
outboxService.saveEvent("STOCKS_METRICS", String.valueOf(item.productId()), kafkaEvent);

apps/commerce-streamer/src/main/java/com/loopers/interfaces/consumer/MetricsEventConsumer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.loopers.interfaces.consumer;
22

33
import com.loopers.domain.metrics.ProductMetricsService;
4+
import com.loopers.domain.rank.RankingService;
45
import com.loopers.event.LikeCountEvent;
56
import com.loopers.event.ProductStockEvent;
67
import com.loopers.event.ProductViewEvent;
@@ -17,6 +18,7 @@
1718
public class MetricsEventConsumer {
1819

1920
private final ProductMetricsService metricsService;
21+
private final RankingService rankingService;
2022

2123
@KafkaListener(
2224
topics = "catalog-events",

modules/kafka/src/main/java/com/loopers/event/ProductStockEvent.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ public record ProductStockEvent(
77
Long productId,
88
int sellQuantity,
99
int currentStock,
10+
long price,
1011
long timestamp
1112
) {
12-
public static ProductStockEvent of(Long productId, int sellQuantity, int currentStock) {
13+
public static ProductStockEvent of(Long productId, int sellQuantity, int currentStock, long price) {
1314
return new ProductStockEvent(
1415
UUID.randomUUID().toString(),
1516
productId,
1617
sellQuantity,
1718
currentStock,
19+
price,
1820
System.currentTimeMillis()
1921
);
2022
}

0 commit comments

Comments
 (0)