From 3a38d928c051b0735a8808cfae3d29b3b20e347f Mon Sep 17 00:00:00 2001 From: nowgnas Date: Thu, 28 Dec 2023 12:03:03 +0900 Subject: [PATCH 1/2] =?UTF-8?q?:sparkles:=20LF1-861=20=EC=A3=BC=EB=AC=B8?= =?UTF-8?q?=20=EC=B0=A8=EA=B0=90=20=EB=A1=A4=EB=B0=B1=20=EC=A3=BC=EB=AC=B8?= =?UTF-8?q?=20=EA=B2=B0=EC=A0=9C=20=EB=A1=A4=EB=B0=B1=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- .../application/handler/ProductQueryHandler.java | 10 +++++++++- .../infrastructure/client/StoreServiceClient.java | 3 +++ .../infrastructure/event/ProductKafkaListener.java | 7 ++++++- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index c7ceab2f..90c0b949 100644 --- a/.gitignore +++ b/.gitignore @@ -37,4 +37,5 @@ out/ .vscode/ docker.sh *.DS_Store -data.sql \ No newline at end of file +data.sql +ProductKafkaTest.java \ No newline at end of file diff --git a/src/main/java/kr/bb/product/domain/product/application/handler/ProductQueryHandler.java b/src/main/java/kr/bb/product/domain/product/application/handler/ProductQueryHandler.java index 9e39a292..bc618aea 100644 --- a/src/main/java/kr/bb/product/domain/product/application/handler/ProductQueryHandler.java +++ b/src/main/java/kr/bb/product/domain/product/application/handler/ProductQueryHandler.java @@ -12,17 +12,25 @@ @RequiredArgsConstructor public class ProductQueryHandler { private static final String STORE_AVERAGE_RATING_UPDATE_TOPIC = "store-average-rating-update"; + private static final String STOCK_DECREASE_ROLLBACK = "stock-decrease-rollback"; private final ProductQueryUseCase productQueryUseCase; private final StoreServiceClient storeServiceClient; private final ProductKafkaProcessor> productKafkaProcessor; + private final ProductKafkaProcessor processOrderDtoProductKafkaProcessor; public void getStoreAverageRating() { Map storeAverageRating = productQueryUseCase.getStoreAverageRating(); productKafkaProcessor.send(STORE_AVERAGE_RATING_UPDATE_TOPIC, storeAverageRating); } - public void getFlowerAmountForOrder(ProcessOrderDto processOrderDto) { + public void getFlowerStockDecrease(ProcessOrderDto processOrderDto) { storeServiceClient.flowerStockDecreaseRequest( productQueryUseCase.getFlowerAmountGroupByStoreId(processOrderDto)); } + + public void getFlowerStockRollback(ProcessOrderDto processOrderDto) { +// storeServiceClient.flowerStockIncreaseRequest( +// productQueryUseCase.getFlowerAmountGroupByStoreId(processOrderDto)); + processOrderDtoProductKafkaProcessor.send(STOCK_DECREASE_ROLLBACK, processOrderDto); + } } diff --git a/src/main/java/kr/bb/product/domain/product/infrastructure/client/StoreServiceClient.java b/src/main/java/kr/bb/product/domain/product/infrastructure/client/StoreServiceClient.java index 9016cd2a..217f8394 100644 --- a/src/main/java/kr/bb/product/domain/product/infrastructure/client/StoreServiceClient.java +++ b/src/main/java/kr/bb/product/domain/product/infrastructure/client/StoreServiceClient.java @@ -42,6 +42,9 @@ CommonResponse> getCartItemProductInformation( @PutMapping("/client/stores/flowers/stocks/subtract") void flowerStockDecreaseRequest(@RequestBody List stockChangeDto); + @PutMapping("/client/stores/flowers/stocks/add") + void flowerStockIncreaseRequest(@RequestBody List flowerAmountGroupByStoreId); + default CommonResponse getStoreNameOfProductDetailFallback(Long storeId, Throwable t) { return CommonResponse.success(StoreName.builder().storeName("가게명").build()); } diff --git a/src/main/java/kr/bb/product/domain/product/infrastructure/event/ProductKafkaListener.java b/src/main/java/kr/bb/product/domain/product/infrastructure/event/ProductKafkaListener.java index 4016c536..a630ab4e 100644 --- a/src/main/java/kr/bb/product/domain/product/infrastructure/event/ProductKafkaListener.java +++ b/src/main/java/kr/bb/product/domain/product/infrastructure/event/ProductKafkaListener.java @@ -13,6 +13,11 @@ public class ProductKafkaListener { @KafkaListener(topics = "stock-decrease", groupId = "stock-decrease") public void stockDecreaseRequestListener(ProcessOrderDto processOrderDto) { - productQueryHandler.getFlowerAmountForOrder(processOrderDto); + productQueryHandler.getFlowerStockDecrease(processOrderDto); + } + + @KafkaListener(topics = "order-create-rollback", groupId = "order-rollback") + public void orderRollbackRequestListener(ProcessOrderDto processOrderDto) { + productQueryHandler.getFlowerStockRollback(processOrderDto); } } From 8ac71990eb22382ea1889b8d36b681fe9c8f8474 Mon Sep 17 00:00:00 2001 From: nowgnas Date: Thu, 28 Dec 2023 16:10:06 +0900 Subject: [PATCH 2/2] :recycle: Refactor code --- .../bb/product/common/dto/NewOrderEvent.java | 51 ------------------- .../common/dto/ReviewRegisterEvent.java | 18 ------- .../kr/bb/product/common/dto/ReviewType.java | 10 ---- .../kr/bb/product/common/dto/StorePolicy.java | 17 ------- .../out/mongo/ProductCommandRepository.java | 4 +- .../handler/ProductCommandHandler.java | 4 +- .../port/in/ProductCommandInputPort.java | 4 +- .../port/in/ProductQueryInputPort.java | 2 +- .../port/out/ProductCommandOutPort.java | 4 +- .../usecase/ProductCommandUseCase.java | 4 +- .../client/StoreServiceClient.java | 2 +- .../message/ProductSQSListener.java | 4 +- .../domain/product/mapper/ProductCommand.java | 2 +- .../event/ReviewSNSPublisher.java | 2 +- .../domain/review/mapper/ReviewCommand.java | 5 +- .../mongo/ProductCommandRepositoryTest.java | 6 +-- 16 files changed, 21 insertions(+), 118 deletions(-) delete mode 100644 src/main/java/kr/bb/product/common/dto/NewOrderEvent.java delete mode 100644 src/main/java/kr/bb/product/common/dto/ReviewRegisterEvent.java delete mode 100644 src/main/java/kr/bb/product/common/dto/ReviewType.java delete mode 100644 src/main/java/kr/bb/product/common/dto/StorePolicy.java diff --git a/src/main/java/kr/bb/product/common/dto/NewOrderEvent.java b/src/main/java/kr/bb/product/common/dto/NewOrderEvent.java deleted file mode 100644 index 073f0c18..00000000 --- a/src/main/java/kr/bb/product/common/dto/NewOrderEvent.java +++ /dev/null @@ -1,51 +0,0 @@ -package kr.bb.product.common.dto; - -import bloomingblooms.domain.notification.order.OrderType; -import java.util.List; -import java.util.stream.Collectors; -import lombok.AccessLevel; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@Getter -@Builder -@NoArgsConstructor(access = AccessLevel.PROTECTED) -@AllArgsConstructor -public class NewOrderEvent { - private List orders; - - public static List getProductDataForUpdateSaleCount(NewOrderEvent newOrderEvent) { - return newOrderEvent.getOrders().stream() - .flatMap(item -> item.getProducts().stream()) - .map( - productCount -> - ProductCount.builder() - .productId(productCount.getProductId()) - .quantity(productCount.getQuantity()) - .build()) - .collect(Collectors.toList()); - } - - @Getter - @Builder - @NoArgsConstructor(access = AccessLevel.PROTECTED) - @AllArgsConstructor - public static class NewOrderEventItem { - private Long orderId; - private String productName; - private Long storeId; - private OrderType orderType; - private List products; - } - - @Getter - @Builder - @NoArgsConstructor(access = AccessLevel.PROTECTED) - @AllArgsConstructor - public static class ProductCount { - private String productId; - private Long quantity; - } -} diff --git a/src/main/java/kr/bb/product/common/dto/ReviewRegisterEvent.java b/src/main/java/kr/bb/product/common/dto/ReviewRegisterEvent.java deleted file mode 100644 index 89c8c4eb..00000000 --- a/src/main/java/kr/bb/product/common/dto/ReviewRegisterEvent.java +++ /dev/null @@ -1,18 +0,0 @@ -package kr.bb.product.common.dto; - -import lombok.AccessLevel; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@Getter -@Builder -@NoArgsConstructor(access = AccessLevel.PROTECTED) -@AllArgsConstructor -public class ReviewRegisterEvent { - private String productId; - private Double reviewRating; - private ReviewType reviewType; - private String id; -} diff --git a/src/main/java/kr/bb/product/common/dto/ReviewType.java b/src/main/java/kr/bb/product/common/dto/ReviewType.java deleted file mode 100644 index 6edcd4b9..00000000 --- a/src/main/java/kr/bb/product/common/dto/ReviewType.java +++ /dev/null @@ -1,10 +0,0 @@ -package kr.bb.product.common.dto; - -import lombok.Getter; - -@Getter -public enum ReviewType { - DELIVERY, - PICKUP, - SUBSCRIBE -} diff --git a/src/main/java/kr/bb/product/common/dto/StorePolicy.java b/src/main/java/kr/bb/product/common/dto/StorePolicy.java deleted file mode 100644 index 7e75c46b..00000000 --- a/src/main/java/kr/bb/product/common/dto/StorePolicy.java +++ /dev/null @@ -1,17 +0,0 @@ -package kr.bb.product.common.dto; - -import lombok.AccessLevel; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; - -@Getter -@Builder -@NoArgsConstructor(access = AccessLevel.PROTECTED) -@AllArgsConstructor -public class StorePolicy { - @Builder.Default private String storeName = "가게명"; - @Builder.Default private Long deliveryCost = 0L; - @Builder.Default private Long freeDeliveryMinCost = 0L; -} diff --git a/src/main/java/kr/bb/product/domain/product/adapter/out/mongo/ProductCommandRepository.java b/src/main/java/kr/bb/product/domain/product/adapter/out/mongo/ProductCommandRepository.java index 6751dd59..e4672691 100644 --- a/src/main/java/kr/bb/product/domain/product/adapter/out/mongo/ProductCommandRepository.java +++ b/src/main/java/kr/bb/product/domain/product/adapter/out/mongo/ProductCommandRepository.java @@ -1,8 +1,8 @@ package kr.bb.product.domain.product.adapter.out.mongo; +import bloomingblooms.domain.order.NewOrderEvent.ProductCount; +import bloomingblooms.domain.review.ReviewRegisterEvent; import java.util.List; -import kr.bb.product.common.dto.NewOrderEvent.ProductCount; -import kr.bb.product.common.dto.ReviewRegisterEvent; import kr.bb.product.domain.product.application.port.out.ProductCommandOutPort; import kr.bb.product.domain.product.entity.Product; import kr.bb.product.domain.product.entity.ProductSaleStatus; diff --git a/src/main/java/kr/bb/product/domain/product/application/handler/ProductCommandHandler.java b/src/main/java/kr/bb/product/domain/product/application/handler/ProductCommandHandler.java index 31019c8a..a6c3081e 100644 --- a/src/main/java/kr/bb/product/domain/product/application/handler/ProductCommandHandler.java +++ b/src/main/java/kr/bb/product/domain/product/application/handler/ProductCommandHandler.java @@ -1,7 +1,7 @@ package kr.bb.product.domain.product.application.handler; -import kr.bb.product.common.dto.NewOrderEvent; -import kr.bb.product.common.dto.ReviewRegisterEvent; +import bloomingblooms.domain.order.NewOrderEvent; +import bloomingblooms.domain.review.ReviewRegisterEvent; import kr.bb.product.domain.product.application.usecase.ProductCommandUseCase; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; diff --git a/src/main/java/kr/bb/product/domain/product/application/port/in/ProductCommandInputPort.java b/src/main/java/kr/bb/product/domain/product/application/port/in/ProductCommandInputPort.java index 29e92f54..9a5e675d 100644 --- a/src/main/java/kr/bb/product/domain/product/application/port/in/ProductCommandInputPort.java +++ b/src/main/java/kr/bb/product/domain/product/application/port/in/ProductCommandInputPort.java @@ -1,8 +1,8 @@ package kr.bb.product.domain.product.application.port.in; +import bloomingblooms.domain.order.NewOrderEvent; +import bloomingblooms.domain.review.ReviewRegisterEvent; import java.util.List; -import kr.bb.product.common.dto.NewOrderEvent; -import kr.bb.product.common.dto.ReviewRegisterEvent; import kr.bb.product.domain.category.entity.Category; import kr.bb.product.domain.category.repository.jpa.CategoryRepository; import kr.bb.product.domain.flower.mapper.FlowerCommand.ProductFlowers; diff --git a/src/main/java/kr/bb/product/domain/product/application/port/in/ProductQueryInputPort.java b/src/main/java/kr/bb/product/domain/product/application/port/in/ProductQueryInputPort.java index ef7a08ef..d658c712 100644 --- a/src/main/java/kr/bb/product/domain/product/application/port/in/ProductQueryInputPort.java +++ b/src/main/java/kr/bb/product/domain/product/application/port/in/ProductQueryInputPort.java @@ -7,6 +7,7 @@ import bloomingblooms.domain.product.ProductInformation; import bloomingblooms.domain.product.ProductThumbnail; import bloomingblooms.domain.product.StoreSubscriptionProductId; +import bloomingblooms.domain.store.StorePolicy; import bloomingblooms.domain.wishlist.cart.GetUserCartItemsResponse; import bloomingblooms.domain.wishlist.likes.LikedProductInfoResponse; import bloomingblooms.errors.EntityNotFoundException; @@ -14,7 +15,6 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; -import kr.bb.product.common.dto.StorePolicy; import kr.bb.product.domain.flower.adapter.out.jpa.FlowerJpaRepository; import kr.bb.product.domain.flower.application.port.out.FlowerQueryOutPort; import kr.bb.product.domain.flower.entity.Flower; diff --git a/src/main/java/kr/bb/product/domain/product/application/port/out/ProductCommandOutPort.java b/src/main/java/kr/bb/product/domain/product/application/port/out/ProductCommandOutPort.java index 1e34459a..1c47ffb0 100644 --- a/src/main/java/kr/bb/product/domain/product/application/port/out/ProductCommandOutPort.java +++ b/src/main/java/kr/bb/product/domain/product/application/port/out/ProductCommandOutPort.java @@ -1,8 +1,8 @@ package kr.bb.product.domain.product.application.port.out; +import bloomingblooms.domain.order.NewOrderEvent.ProductCount; +import bloomingblooms.domain.review.ReviewRegisterEvent; import java.util.List; -import kr.bb.product.common.dto.NewOrderEvent.ProductCount; -import kr.bb.product.common.dto.ReviewRegisterEvent; import kr.bb.product.domain.product.entity.Product; import kr.bb.product.domain.product.entity.ProductSaleStatus; import kr.bb.product.domain.product.mapper.ProductCommand; diff --git a/src/main/java/kr/bb/product/domain/product/application/usecase/ProductCommandUseCase.java b/src/main/java/kr/bb/product/domain/product/application/usecase/ProductCommandUseCase.java index 3b686fa4..b3b00dea 100644 --- a/src/main/java/kr/bb/product/domain/product/application/usecase/ProductCommandUseCase.java +++ b/src/main/java/kr/bb/product/domain/product/application/usecase/ProductCommandUseCase.java @@ -1,8 +1,8 @@ package kr.bb.product.domain.product.application.usecase; +import bloomingblooms.domain.order.NewOrderEvent.ProductCount; +import bloomingblooms.domain.review.ReviewRegisterEvent; import java.util.List; -import kr.bb.product.common.dto.NewOrderEvent.ProductCount; -import kr.bb.product.common.dto.ReviewRegisterEvent; import kr.bb.product.domain.product.mapper.ProductCommand; public interface ProductCommandUseCase { diff --git a/src/main/java/kr/bb/product/domain/product/infrastructure/client/StoreServiceClient.java b/src/main/java/kr/bb/product/domain/product/infrastructure/client/StoreServiceClient.java index 217f8394..e7dc815f 100644 --- a/src/main/java/kr/bb/product/domain/product/infrastructure/client/StoreServiceClient.java +++ b/src/main/java/kr/bb/product/domain/product/infrastructure/client/StoreServiceClient.java @@ -2,12 +2,12 @@ import bloomingblooms.domain.flower.StockChangeDto; import bloomingblooms.domain.store.StoreName; +import bloomingblooms.domain.store.StorePolicy; import bloomingblooms.response.CommonResponse; import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker; import java.util.List; import java.util.Map; import java.util.stream.Collectors; -import kr.bb.product.common.dto.StorePolicy; import kr.bb.product.config.OpenFeignClientConfiguration; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; diff --git a/src/main/java/kr/bb/product/domain/product/infrastructure/message/ProductSQSListener.java b/src/main/java/kr/bb/product/domain/product/infrastructure/message/ProductSQSListener.java index d8000c54..bf141267 100644 --- a/src/main/java/kr/bb/product/domain/product/infrastructure/message/ProductSQSListener.java +++ b/src/main/java/kr/bb/product/domain/product/infrastructure/message/ProductSQSListener.java @@ -1,11 +1,11 @@ package kr.bb.product.domain.product.infrastructure.message; +import bloomingblooms.domain.order.NewOrderEvent; +import bloomingblooms.domain.review.ReviewRegisterEvent; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import java.util.Map; -import kr.bb.product.common.dto.NewOrderEvent; -import kr.bb.product.common.dto.ReviewRegisterEvent; import kr.bb.product.domain.product.application.handler.ProductCommandHandler; import kr.bb.product.domain.product.application.handler.ProductQueryHandler; import lombok.RequiredArgsConstructor; diff --git a/src/main/java/kr/bb/product/domain/product/mapper/ProductCommand.java b/src/main/java/kr/bb/product/domain/product/mapper/ProductCommand.java index 16cb32f4..c79bb00f 100644 --- a/src/main/java/kr/bb/product/domain/product/mapper/ProductCommand.java +++ b/src/main/java/kr/bb/product/domain/product/mapper/ProductCommand.java @@ -7,6 +7,7 @@ import bloomingblooms.domain.product.ProductInformation; import bloomingblooms.domain.product.ProductThumbnail; import bloomingblooms.domain.product.StoreSubscriptionProductId; +import bloomingblooms.domain.store.StorePolicy; import bloomingblooms.domain.wishlist.cart.CartProductItemInfo; import bloomingblooms.domain.wishlist.cart.GetUserCartItemsResponse; import bloomingblooms.domain.wishlist.likes.LikedProductInfoResponse; @@ -15,7 +16,6 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; -import kr.bb.product.common.dto.StorePolicy; import kr.bb.product.domain.category.entity.CategoryCommand; import kr.bb.product.domain.category.entity.CategoryCommand.CategoryDetail; import kr.bb.product.domain.flower.entity.Flower; diff --git a/src/main/java/kr/bb/product/domain/review/infrastructure/event/ReviewSNSPublisher.java b/src/main/java/kr/bb/product/domain/review/infrastructure/event/ReviewSNSPublisher.java index cfe7ed68..6968248e 100644 --- a/src/main/java/kr/bb/product/domain/review/infrastructure/event/ReviewSNSPublisher.java +++ b/src/main/java/kr/bb/product/domain/review/infrastructure/event/ReviewSNSPublisher.java @@ -1,8 +1,8 @@ package kr.bb.product.domain.review.infrastructure.event; +import bloomingblooms.domain.review.ReviewRegisterEvent; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import kr.bb.product.common.dto.ReviewRegisterEvent; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; diff --git a/src/main/java/kr/bb/product/domain/review/mapper/ReviewCommand.java b/src/main/java/kr/bb/product/domain/review/mapper/ReviewCommand.java index 3fccc744..4482bc9f 100644 --- a/src/main/java/kr/bb/product/domain/review/mapper/ReviewCommand.java +++ b/src/main/java/kr/bb/product/domain/review/mapper/ReviewCommand.java @@ -1,12 +1,12 @@ package kr.bb.product.domain.review.mapper; +import bloomingblooms.domain.review.ReviewRegisterEvent; +import bloomingblooms.domain.review.ReviewType; import java.time.LocalDateTime; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import javax.annotation.Nullable; -import kr.bb.product.common.dto.ReviewRegisterEvent; -import kr.bb.product.common.dto.ReviewType; import kr.bb.product.domain.review.entity.Review; import kr.bb.product.domain.review.entity.ReviewImages; import kr.bb.product.domain.review.mapper.mapper.ReviewImageMapper; @@ -21,7 +21,6 @@ public static ReviewRegisterEvent getReviewRegisterEventData(String productId, R return ReviewRegisterEvent.builder() .reviewRating(review.getRating()) .productId(productId) - .id(review.id) .reviewType(review.reviewType) // TODO: review type required .build(); } diff --git a/src/test/java/kr/bb/product/domain/product/adapter/out/mongo/ProductCommandRepositoryTest.java b/src/test/java/kr/bb/product/domain/product/adapter/out/mongo/ProductCommandRepositoryTest.java index 1b1cc5cd..5d708403 100644 --- a/src/test/java/kr/bb/product/domain/product/adapter/out/mongo/ProductCommandRepositoryTest.java +++ b/src/test/java/kr/bb/product/domain/product/adapter/out/mongo/ProductCommandRepositoryTest.java @@ -2,11 +2,11 @@ import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import bloomingblooms.domain.order.NewOrderEvent.ProductCount; +import bloomingblooms.domain.review.ReviewRegisterEvent; +import bloomingblooms.domain.review.ReviewType; import java.util.ArrayList; import java.util.List; -import kr.bb.product.common.dto.NewOrderEvent.ProductCount; -import kr.bb.product.common.dto.ReviewRegisterEvent; -import kr.bb.product.common.dto.ReviewType; import kr.bb.product.domain.product.entity.Product; import kr.bb.product.domain.product.mapper.ProductCommand; import org.junit.jupiter.api.DisplayName;