주문 취소 이벤트 발생

This commit is contained in:
kimscott
2019-11-22 13:42:05 +09:00
parent 3ab8cdf2d9
commit 55007dd9b0
2 changed files with 146 additions and 19 deletions

View File

@@ -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<Product> 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<Order> 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;
}
}

View 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;
}
}