주문시 상품 변경 추가
This commit is contained in:
81
src/main/java/com/example/template/OrderPlaced.java
Normal file
81
src/main/java/com/example/template/OrderPlaced.java
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
package com.example.template;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public class OrderPlaced implements Serializable {
|
||||||
|
|
||||||
|
private String type;
|
||||||
|
|
||||||
|
private Long productId;
|
||||||
|
private Long orderId;
|
||||||
|
private String productName;
|
||||||
|
private int quantity;
|
||||||
|
private int price;
|
||||||
|
private String customerName;
|
||||||
|
private String customerAddr;
|
||||||
|
|
||||||
|
public String getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(String type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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 getCustomerName() {
|
||||||
|
return customerName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCustomerName(String customerName) {
|
||||||
|
this.customerName = customerName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCustomerAddr() {
|
||||||
|
return customerAddr;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCustomerAddr(String customerAddr) {
|
||||||
|
this.customerAddr = customerAddr;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,6 +9,7 @@ import org.springframework.messaging.handler.annotation.Payload;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class ProductService {
|
public class ProductService {
|
||||||
@@ -52,6 +53,19 @@ public class ProductService {
|
|||||||
productRepository.save(product);
|
productRepository.save(product);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* 주문이 발생시, 수량을 줄인다.
|
||||||
|
*/
|
||||||
|
else if( productRequired.getType().equals(OrderPlaced.class.getSimpleName())){
|
||||||
|
OrderPlaced orderPlaced = objectMapper.readValue(message, OrderPlaced.class);
|
||||||
|
|
||||||
|
Optional<Product> productOptional = productRepository.findById(orderPlaced.getProductId());
|
||||||
|
Product product = productOptional.get();
|
||||||
|
product.setStock(product.getStock() - orderPlaced.getQuantity());
|
||||||
|
|
||||||
|
productRepository.save(product);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}catch (Exception e){
|
}catch (Exception e){
|
||||||
|
|
||||||
|
|||||||
4
src/main/resources/data.sql
Normal file
4
src/main/resources/data.sql
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
insert into Product(id, name, price, stock) values (1, 'TV', 1000, 100);
|
||||||
|
insert into Product(id, name, price, stock) values (2, 'RADIO', 500, 100);
|
||||||
|
insert into Product(id, name, price, stock) values (3, 'PHONE', 100, 50);
|
||||||
|
insert into Product(id, name, price, stock) values (4, 'NOTEBOOK', 2000, 100);
|
||||||
Reference in New Issue
Block a user