주문 취소 이벤트 발생
This commit is contained in:
@@ -1,20 +1,10 @@
|
|||||||
package com.example.template;
|
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.JsonObject;
|
||||||
import com.google.gson.JsonParser;
|
import com.google.gson.JsonParser;
|
||||||
import org.springframework.beans.BeanUtils;
|
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
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.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
|
||||||
import org.springframework.web.client.RestTemplate;
|
import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
@@ -34,13 +24,10 @@ public class Order {
|
|||||||
private String customerId;
|
private String customerId;
|
||||||
private String customerName;
|
private String customerName;
|
||||||
private String customerAddr;
|
private String customerAddr;
|
||||||
|
private String state = "OrderPlaced";
|
||||||
|
|
||||||
/**
|
@PrePersist
|
||||||
* 주문이 들어옴
|
private void orderCheck(){
|
||||||
*/
|
|
||||||
@PostPersist
|
|
||||||
@ExceptionHandler(OrderException.class)
|
|
||||||
private void publishOrderPlaced(){
|
|
||||||
RestTemplate restTemplate = Application.applicationContext.getBean(RestTemplate.class);
|
RestTemplate restTemplate = Application.applicationContext.getBean(RestTemplate.class);
|
||||||
Environment env = Application.applicationContext.getEnvironment();
|
Environment env = Application.applicationContext.getEnvironment();
|
||||||
|
|
||||||
@@ -48,6 +35,9 @@ public class Order {
|
|||||||
throw new RuntimeException();
|
throw new RuntimeException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int price = 0;
|
||||||
|
String productName = null;
|
||||||
|
|
||||||
if("true".equalsIgnoreCase(env.getProperty("checkStock"))){
|
if("true".equalsIgnoreCase(env.getProperty("checkStock"))){
|
||||||
// 1. 주문에 대한 상품 조회 - API
|
// 1. 주문에 대한 상품 조회 - API
|
||||||
String productUrl = env.getProperty("productUrl") + "/product/" + productId;
|
String productUrl = env.getProperty("productUrl") + "/product/" + productId;
|
||||||
@@ -56,25 +46,55 @@ public class Order {
|
|||||||
JsonParser parser = new JsonParser();
|
JsonParser parser = new JsonParser();
|
||||||
JsonObject jsonObject = parser.parse(productEntity.getBody()).getAsJsonObject();
|
JsonObject jsonObject = parser.parse(productEntity.getBody()).getAsJsonObject();
|
||||||
|
|
||||||
this.setPrice(jsonObject.get("price").getAsInt());
|
price = jsonObject.get("price").getAsInt();
|
||||||
this.setProductName(jsonObject.get("name").getAsString());
|
productName = jsonObject.get("name").getAsString();
|
||||||
|
|
||||||
if( jsonObject.get("stock").getAsInt() < getQuantity()){
|
if( jsonObject.get("stock").getAsInt() < getQuantity()){
|
||||||
throw new OrderException("No Available stock!");
|
throw new OrderException("No Available stock!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}else{
|
}else{
|
||||||
ProductRepository productRepository = Application.applicationContext.getBean(ProductRepository.class);
|
ProductRepository productRepository = Application.applicationContext.getBean(ProductRepository.class);
|
||||||
Optional<Product> productOptional = productRepository.findById(productId);
|
Optional<Product> productOptional = productRepository.findById(productId);
|
||||||
Product product = productOptional.get();
|
Product product = productOptional.get();
|
||||||
|
|
||||||
|
price = product.getPrice();
|
||||||
|
productName = product.getName();
|
||||||
if( product.getStock() < getQuantity()){
|
if( product.getStock() < getQuantity()){
|
||||||
throw new OrderException("No Available stock!");
|
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 orderPlaced = new OrderPlaced(this);
|
||||||
orderPlaced.sendMessage(orderPlaced.toJson());
|
orderPlaced.sendMessage(orderPlaced.toJson());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 주문이 취소됨
|
||||||
|
*/
|
||||||
|
@PostUpdate
|
||||||
|
private void publishOrderCancelled(){
|
||||||
|
if( "OrderCancelled".equals(this.getState())){
|
||||||
|
// 이벤트를 발송하기 위하여 주문의 상세 정보를 조회
|
||||||
|
|
||||||
|
OrderRepository orderRepository = Application.applicationContext.getBean(OrderRepository.class);
|
||||||
|
Optional<Order> orderOptional = orderRepository.findById(this.getId());
|
||||||
|
Order order = orderOptional.get();
|
||||||
|
|
||||||
|
OrderCancelled orderCancelled = new OrderCancelled(order);
|
||||||
|
orderCancelled.sendMessage(orderCancelled.toJson());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
@@ -138,4 +158,12 @@ public class Order {
|
|||||||
public void setCustomerAddr(String customerAddr) {
|
public void setCustomerAddr(String customerAddr) {
|
||||||
this.customerAddr = customerAddr;
|
this.customerAddr = customerAddr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getState() {
|
||||||
|
return state;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setState(String state) {
|
||||||
|
this.state = state;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
99
src/main/java/com/example/template/OrderCancelled.java
Normal file
99
src/main/java/com/example/template/OrderCancelled.java
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user