From 55007dd9b05ac1ba691e66466d61d1ba8613e3a5 Mon Sep 17 00:00:00 2001 From: kimscott Date: Fri, 22 Nov 2019 13:42:05 +0900 Subject: [PATCH] =?UTF-8?q?=EC=A3=BC=EB=AC=B8=20=EC=B7=A8=EC=86=8C=20?= =?UTF-8?q?=EC=9D=B4=EB=B2=A4=ED=8A=B8=20=EB=B0=9C=EC=83=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/example/template/Order.java | 66 +++++++++---- .../com/example/template/OrderCancelled.java | 99 +++++++++++++++++++ 2 files changed, 146 insertions(+), 19 deletions(-) create mode 100644 src/main/java/com/example/template/OrderCancelled.java diff --git a/src/main/java/com/example/template/Order.java b/src/main/java/com/example/template/Order.java index 72c0cef..d5aa4d4 100644 --- a/src/main/java/com/example/template/Order.java +++ b/src/main/java/com/example/template/Order.java @@ -1,20 +1,10 @@ package com.example.template; -import com.example.template.config.kafka.KafkaProcessor; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; import com.google.gson.JsonObject; import com.google.gson.JsonParser; -import org.springframework.beans.BeanUtils; import org.springframework.core.env.Environment; -import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.messaging.MessageChannel; -import org.springframework.messaging.MessageHeaders; -import org.springframework.messaging.support.MessageBuilder; -import org.springframework.util.MimeTypeUtils; import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.client.RestTemplate; import javax.persistence.*; @@ -34,13 +24,10 @@ public class Order { private String customerId; private String customerName; private String customerAddr; + private String state = "OrderPlaced"; - /** - * 주문이 들어옴 - */ - @PostPersist - @ExceptionHandler(OrderException.class) - private void publishOrderPlaced(){ + @PrePersist + private void orderCheck(){ RestTemplate restTemplate = Application.applicationContext.getBean(RestTemplate.class); Environment env = Application.applicationContext.getEnvironment(); @@ -48,6 +35,9 @@ public class Order { throw new RuntimeException(); } + int price = 0; + String productName = null; + if("true".equalsIgnoreCase(env.getProperty("checkStock"))){ // 1. 주문에 대한 상품 조회 - API String productUrl = env.getProperty("productUrl") + "/product/" + productId; @@ -56,25 +46,55 @@ public class Order { JsonParser parser = new JsonParser(); JsonObject jsonObject = parser.parse(productEntity.getBody()).getAsJsonObject(); - this.setPrice(jsonObject.get("price").getAsInt()); - this.setProductName(jsonObject.get("name").getAsString()); - + price = jsonObject.get("price").getAsInt(); + productName = jsonObject.get("name").getAsString(); if( jsonObject.get("stock").getAsInt() < getQuantity()){ throw new OrderException("No Available stock!"); } + + }else{ ProductRepository productRepository = Application.applicationContext.getBean(ProductRepository.class); Optional productOptional = productRepository.findById(productId); Product product = productOptional.get(); + + price = product.getPrice(); + productName = product.getName(); if( product.getStock() < getQuantity()){ throw new OrderException("No Available stock!"); } } + this.setPrice(price); + this.setProductName(productName); + } + /** + * 주문이 들어옴 + */ + @PostPersist + @ExceptionHandler(OrderException.class) + private void publishOrderPlaced(){ OrderPlaced orderPlaced = new OrderPlaced(this); orderPlaced.sendMessage(orderPlaced.toJson()); } + /** + * 주문이 취소됨 + */ + @PostUpdate + private void publishOrderCancelled(){ + if( "OrderCancelled".equals(this.getState())){ + // 이벤트를 발송하기 위하여 주문의 상세 정보를 조회 + + OrderRepository orderRepository = Application.applicationContext.getBean(OrderRepository.class); + Optional orderOptional = orderRepository.findById(this.getId()); + Order order = orderOptional.get(); + + OrderCancelled orderCancelled = new OrderCancelled(order); + orderCancelled.sendMessage(orderCancelled.toJson()); + } + } + public Long getId() { return id; } @@ -138,4 +158,12 @@ public class Order { public void setCustomerAddr(String customerAddr) { this.customerAddr = customerAddr; } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } } \ No newline at end of file diff --git a/src/main/java/com/example/template/OrderCancelled.java b/src/main/java/com/example/template/OrderCancelled.java new file mode 100644 index 0000000..7a3394b --- /dev/null +++ b/src/main/java/com/example/template/OrderCancelled.java @@ -0,0 +1,99 @@ +package com.example.template; + +import java.text.SimpleDateFormat; +import java.util.Date; + +public class OrderCancelled extends AbstractEvent{ + + private String stateMessage = "주문이 취소됨"; + + private Long productId; + private Long orderId; + private String productName; + private int quantity; + private int price; + private String customerId; + private String customerName; + + public OrderCancelled(){ + this.setEventType(this.getClass().getSimpleName()); + SimpleDateFormat defaultSimpleDateFormat = new SimpleDateFormat("YYYYMMddHHmmss"); + this.timestamp = defaultSimpleDateFormat.format(new Date()); + } + + public OrderCancelled(Order order){ + this(); + this.setProductId(order.getProductId()); + this.setProductName(order.getProductName()); + this.setOrderId(order.getId()); + this.setQuantity(order.getQuantity()); + this.setPrice(order.getPrice()); + this.setCustomerId(order.getCustomerId()); + this.setCustomerName(order.getCustomerName()); + } + + public String getStateMessage() { + return stateMessage; + } + + public void setStateMessage(String stateMessage) { + this.stateMessage = stateMessage; + } + + public Long getProductId() { + return productId; + } + + public void setProductId(Long productId) { + this.productId = productId; + } + + public Long getOrderId() { + return orderId; + } + + public void setOrderId(Long orderId) { + this.orderId = orderId; + } + + public String getProductName() { + return productName; + } + + public void setProductName(String productName) { + this.productName = productName; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + public int getPrice() { + return price; + } + + public void setPrice(int price) { + this.price = price; + } + + public String getCustomerId() { + return customerId; + } + + public void setCustomerId(String customerId) { + this.customerId = customerId; + } + + public String getCustomerName() { + return customerName; + } + + public void setCustomerName(String customerName) { + this.customerName = customerName; + } + +}