Skip to content

Commit

Permalink
Merge pull request #53 from nowgnas/LF1-836-product-sale-count-update
Browse files Browse the repository at this point in the history
Lf1 836 product sale count update
  • Loading branch information
nowgnas authored Dec 25, 2023
2 parents b17e6f6 + fed8c88 commit cd634c4
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 3 deletions.
51 changes: 51 additions & 0 deletions src/main/java/kr/bb/product/common/dto/NewOrderEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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<NewOrderEventItem> orders;

public static List<ProductCount> 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<NewOrderEvent.ProductCount> products;
}

@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public static class ProductCount {
private String productId;
private Long quantity;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package kr.bb.product.domain.product.adapter.out.mongo;

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.mapper.ProductCommand.UpdateSubscriptionProduct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.BulkOperations;
import org.springframework.data.mongodb.core.BulkOperations.BulkMode;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

@Slf4j
@Repository
@RequiredArgsConstructor
public class ProductCommandRepository implements ProductCommandOutPort {
Expand Down Expand Up @@ -47,4 +53,15 @@ public void updateProductReviewData(ReviewRegisterEvent reviewRegisterEvent) {
Update update = new Update().set("averageRating", newAverageRating).inc("reviewCount", 1);
mongoTemplate.updateFirst(query, update, Product.class);
}

@Override
public void updateProductSaleCount(List<ProductCount> newOrderEvent) {
BulkOperations bulkOperations = mongoTemplate.bulkOps(BulkMode.UNORDERED, Product.class);
for (ProductCount products : newOrderEvent) {
Query query = Query.query(Criteria.where("_id").is(products.getProductId()));
Update update = new Update().inc("productSaleAmount", products.getQuantity());
bulkOperations.updateOne(query, update);
}
bulkOperations.execute();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kr.bb.product.domain.product.application.handler;

import kr.bb.product.common.dto.NewOrderEvent;
import kr.bb.product.common.dto.ReviewRegisterEvent;
import kr.bb.product.domain.product.application.usecase.ProductCommandUseCase;
import lombok.RequiredArgsConstructor;
Expand All @@ -13,4 +14,9 @@ public class ProductCommandHandler {
public void updateReviewData(ReviewRegisterEvent reviewRegisterEvent) {
productCommandUseCase.updateProductReviewData(reviewRegisterEvent);
}

public void saleCountUpdate(NewOrderEvent newOrderEvent) {
productCommandUseCase.saleCountUpdate(
NewOrderEvent.getProductDataForUpdateSaleCount(newOrderEvent));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kr.bb.product.domain.product.application.port.in;

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;
Expand Down Expand Up @@ -139,4 +140,9 @@ public void updateSubscriptionProduct(String storeId, UpdateSubscriptionProduct
public void updateProductReviewData(ReviewRegisterEvent reviewRegisterEvent) {
productCommandOutPort.updateProductReviewData(reviewRegisterEvent);
}

@Override
public void saleCountUpdate(List<NewOrderEvent.ProductCount> newOrderEvent) {
productCommandOutPort.updateProductSaleCount(newOrderEvent);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package kr.bb.product.domain.product.application.port.out;

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.mapper.ProductCommand;
Expand All @@ -9,5 +11,7 @@ public interface ProductCommandOutPort {

void updateSubscriptionProduct(ProductCommand.UpdateSubscriptionProduct product);

void updateProductReviewData(ReviewRegisterEvent reviewRegisterEvent);
void updateProductReviewData(ReviewRegisterEvent reviewRegisterEvent);

void updateProductSaleCount(List<ProductCount> newOrderEvent);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package kr.bb.product.domain.product.application.usecase;

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;

Expand All @@ -14,4 +16,6 @@ void updateSubscriptionProduct(
String productId, ProductCommand.UpdateSubscriptionProduct product);

void updateProductReviewData(ReviewRegisterEvent reviewRegisterEvent);

void saleCountUpdate(List<ProductCount> newOrderEvent);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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 lombok.RequiredArgsConstructor;
Expand All @@ -22,10 +23,18 @@ public class ProductSQSListener {
private final ObjectMapper objectMapper;
private final ProductCommandHandler productHandler;

/**
* 상품 리뷰 작성 시 상품 리뷰 정보 업데이트
*
* @param message
* @param headers
* @param ack
* @throws JsonProcessingException
*/
@SqsListener(
value = "${cloud.aws.sqs.product-review-data-update-queue.name}",
deletionPolicy = SqsMessageDeletionPolicy.NEVER)
public void consumeProductResaleNotificationCheckQueue(
public void consumeProductReviewDataUpdateQueue(
@Payload String message, @Headers Map<String, String> headers, Acknowledgment ack)
throws JsonProcessingException {
String messageFromSNS = getMessageFromSNS(message);
Expand All @@ -37,6 +46,28 @@ public void consumeProductResaleNotificationCheckQueue(
ack.acknowledge();
}

/**
* 상품 주문 시 판매량 증가
*
* @param message
* @param headers
* @param ack
* @throws JsonProcessingException
*/
@SqsListener(
value = "${cloud.aws.sqs.sale-count-update-queue.name}",
deletionPolicy = SqsMessageDeletionPolicy.NEVER)
public void consumeSaleCountUpdateQueue(
@Payload String message, @Headers Map<String, String> headers, Acknowledgment ack)
throws JsonProcessingException {
String messageFromSNS = getMessageFromSNS(message);

NewOrderEvent newOrderEvent = objectMapper.readValue(messageFromSNS, NewOrderEvent.class);

productHandler.saleCountUpdate(newOrderEvent);
ack.acknowledge();
}

private String getMessageFromSNS(String message) throws JsonProcessingException {
JsonNode jsonNode = objectMapper.readTree(message);
return jsonNode.get("Message").asText();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

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;
Expand Down Expand Up @@ -58,7 +60,8 @@ void updateSubscriptionProduct() {
@DisplayName("리뷰 작성 시 리뷰 정보 수정 ")
void updateProductReviewData() {
productMongoRepository.deleteAll();
Product product = Product.builder().productId("1234").averageRating(1.0).reviewCount(5L).build();
Product product =
Product.builder().productId("1234").averageRating(1.0).reviewCount(5L).build();
productMongoRepository.save(product);
productCommandRepository.updateProductReviewData(
ReviewRegisterEvent.builder()
Expand All @@ -70,4 +73,21 @@ void updateProductReviewData() {
System.out.println(product1.getAverageRating());
assertThat(product1.getReviewCount()).isEqualTo(product.getReviewCount() + 1);
}

@Test
@DisplayName("상품 판매량 증가 ")
void updateProductSaleCount() {
productMongoRepository.deleteAll();
for (int i = 0; i < 5; i++) {
Product product = Product.builder().productId("i" + i).productSaleAmount(1L).build();
productMongoRepository.save(product);
}
List<ProductCount> lst = new ArrayList<>();
for (int i = 0; i < 5; i++) {
lst.add(ProductCount.builder().quantity(i + 1L).productId("i" + i).build());
}
productCommandRepository.updateProductSaleCount(lst);
Product product = productMongoRepository.findByProductId("i" + 1L).orElseGet(null);
assertThat(product.getProductSaleAmount()).isEqualTo(3);
}
}
3 changes: 3 additions & 0 deletions src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ cloud:
product-review-data-update-queue:
name: ""
url: ""
sale-count-update-queue:
name: ""
url: ""

0 comments on commit cd634c4

Please sign in to comment.