feat(order-service): 주문 엔티티 생성 메소드 추가
- 주문 엔티티 생성 메소드 추가 - 서버 기동 시 테스트 데이터 추가
This commit is contained in:
@@ -1,9 +1,17 @@
|
||||
package com.justpickup.orderservice;
|
||||
|
||||
import com.justpickup.orderservice.domain.order.entity.Order;
|
||||
import com.justpickup.orderservice.domain.order.repository.OrderRepository;
|
||||
import com.justpickup.orderservice.domain.orderItem.entity.OrderItem;
|
||||
import com.justpickup.orderservice.domain.orderItemOption.entity.OrderItemOption;
|
||||
import com.justpickup.orderservice.domain.transaction.entity.Transaction;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableEurekaClient
|
||||
@@ -14,4 +22,32 @@ public class OrderServiceApplication {
|
||||
SpringApplication.run(OrderServiceApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Transactional
|
||||
CommandLineRunner run(OrderRepository orderRepository) {
|
||||
return args -> {
|
||||
Long userId = 1L;
|
||||
Long userCouponId = null;
|
||||
Long storeId = 1L;
|
||||
Long orderPrice = 1000L;
|
||||
|
||||
for (int i = 1; i <= 20; i++) {
|
||||
Transaction transaction = Transaction.of();
|
||||
|
||||
Long itemId = 1L;
|
||||
Long price = 100L;
|
||||
Long count = 1L;
|
||||
OrderItem orderItem = OrderItem.of(itemId + i, price * i, count + i,
|
||||
OrderItemOption.of(), OrderItemOption.of());
|
||||
|
||||
OrderItem orderItem1 = OrderItem.of(itemId + i + 1, price * (i + 1), count + (i + 1),
|
||||
OrderItemOption.of(), OrderItemOption.of());
|
||||
|
||||
Order order = Order.of(userId, userCouponId, storeId, orderPrice * i, transaction, orderItem, orderItem1);
|
||||
|
||||
orderRepository.save(order);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import org.hibernate.annotations.BatchSize;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@@ -37,11 +38,39 @@ public class Order extends BaseEntity {
|
||||
@Enumerated(EnumType.STRING)
|
||||
private OrderStatus orderStatus;
|
||||
|
||||
@OneToOne(mappedBy = "order", fetch = FetchType.LAZY)
|
||||
@OneToOne(mappedBy = "order", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
|
||||
private Transaction transaction;
|
||||
|
||||
@BatchSize(size = 100)
|
||||
@OneToMany(mappedBy = "order")
|
||||
private List<OrderItem> orderItems;
|
||||
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL)
|
||||
private List<OrderItem> orderItems = new ArrayList<>();
|
||||
|
||||
public static Order of(Long userId, Long userCouponId, Long storeId, Long orderPrice,
|
||||
Transaction transaction, OrderItem... orderItems) {
|
||||
Order order = new Order();
|
||||
order.userId = userId;
|
||||
order.userCouponId = userCouponId;
|
||||
order.storeId = storeId;
|
||||
order.orderPrice = orderPrice;
|
||||
|
||||
order.setTransaction(transaction);
|
||||
for (OrderItem orderItem : orderItems) {
|
||||
order.addOrderItem(orderItem);
|
||||
}
|
||||
|
||||
order.usedPoint = 0L;
|
||||
order.orderStatus = OrderStatus.PLACED;
|
||||
order.orderTime = LocalDateTime.now();
|
||||
return order;
|
||||
}
|
||||
|
||||
public void setTransaction(Transaction transaction) {
|
||||
this.transaction = transaction;
|
||||
transaction.setOrder(this);
|
||||
}
|
||||
|
||||
public void addOrderItem(OrderItem orderItem) {
|
||||
this.orderItems.add(orderItem);
|
||||
orderItem.setOrder(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@@ -27,11 +28,30 @@ public class OrderItem extends BaseEntity {
|
||||
@JoinColumn(name = "order_id")
|
||||
private Order order;
|
||||
|
||||
@OneToMany(mappedBy = "orderItem")
|
||||
private List<OrderItemOption> orderItemOptions;
|
||||
@OneToMany(mappedBy = "orderItem", cascade = CascadeType.ALL)
|
||||
private List<OrderItemOption> orderItemOptions = new ArrayList<>();
|
||||
|
||||
private Long price;
|
||||
|
||||
private Long count;
|
||||
|
||||
public void setOrder(Order order) {
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
public void addOrderItemOption(OrderItemOption orderItemOption) {
|
||||
this.orderItemOptions.add(orderItemOption);
|
||||
orderItemOption.setOrderItem(this);
|
||||
}
|
||||
|
||||
public static OrderItem of(Long itemId, Long price, Long count, OrderItemOption... orderItemOptions) {
|
||||
OrderItem orderItem = new OrderItem();
|
||||
orderItem.itemId = itemId;
|
||||
orderItem.price = price;
|
||||
orderItem.count = count;
|
||||
for (OrderItemOption orderItemOption : orderItemOptions) {
|
||||
orderItem.addOrderItemOption(orderItemOption);
|
||||
}
|
||||
return orderItem;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,4 +24,12 @@ public class OrderItemOption extends BaseEntity {
|
||||
|
||||
private Long itemOptionId;
|
||||
|
||||
public void setOrderItem(OrderItem orderItem) {
|
||||
this.orderItem = orderItem;
|
||||
}
|
||||
|
||||
public static OrderItemOption of() {
|
||||
OrderItemOption orderItemOption = new OrderItemOption();
|
||||
return orderItemOption;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,4 +22,13 @@ public class Transaction extends BaseEntity {
|
||||
@JoinColumn(name = "order_id")
|
||||
private Order order;
|
||||
|
||||
public void setOrder(Order order) {
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
public static Transaction of() {
|
||||
Transaction transaction = new Transaction();
|
||||
return transaction;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user