From e2cbd5d64960686ddb023e87061192e67c016316 Mon Sep 17 00:00:00 2001 From: junho Date: Sun, 30 Jul 2023 07:21:58 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20order=ED=8C=A8=ED=82=A4=EC=A7=80=20enti?= =?UTF-8?q?ty=20=EC=97=B0=EA=B4=80=EA=B4=80=EA=B3=84=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kdt/kdtjpa/domain/order/BaseEntity.java | 17 +++++++ .../com/kdt/kdtjpa/domain/order/Item.java | 34 ++++++++++++++ .../com/kdt/kdtjpa/domain/order/Member.java | 38 ++++++++++++++++ .../com/kdt/kdtjpa/domain/order/Order.java | 45 +++++++++++++++++++ .../kdt/kdtjpa/domain/order/OrderItem.java | 39 ++++++++++++++++ .../kdt/kdtjpa/domain/order/OrderStatus.java | 5 +++ 6 files changed, 178 insertions(+) create mode 100644 kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/BaseEntity.java create mode 100644 kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/Item.java create mode 100644 kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/Member.java create mode 100644 kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/Order.java create mode 100644 kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/OrderItem.java create mode 100644 kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/OrderStatus.java diff --git a/kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/BaseEntity.java b/kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/BaseEntity.java new file mode 100644 index 000000000..a24c66b8b --- /dev/null +++ b/kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/BaseEntity.java @@ -0,0 +1,17 @@ +package com.kdt.kdtjpa.domain.order; + +import jakarta.persistence.Column; +import jakarta.persistence.MappedSuperclass; +import lombok.Getter; + +import java.time.LocalDateTime; + +@Getter +@MappedSuperclass +public abstract class BaseEntity { + @Column(name = "created_by") + private String createdBy; + + @Column(name = "created_at", columnDefinition = "TIMESTAMP") + private LocalDateTime createdAt; +} \ No newline at end of file diff --git a/kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/Item.java b/kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/Item.java new file mode 100644 index 000000000..8341a52a1 --- /dev/null +++ b/kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/Item.java @@ -0,0 +1,34 @@ +package com.kdt.kdtjpa.domain.order; + +import jakarta.persistence.*; +import lombok.Getter; +import lombok.Setter; + +import java.util.Objects; + +@Entity +@Table(name = "item") +@Getter +@Setter +@DiscriminatorColumn(name = "DTYPE") +@Inheritance(strategy = InheritanceType.SINGLE_TABLE) +public abstract class Item extends BaseEntity { + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE) + private Long id; + + private int price; + private int stockQuantity; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "order_item_id", referencedColumnName = "id") + private OrderItem orderItem; + + public void setOrderItem(OrderItem orderItem) { + if (Objects.nonNull(this.orderItem)) { + this.orderItem.getItems().remove(this); + } + this.orderItem = orderItem; + orderItem.getItems().add(this); + } +} diff --git a/kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/Member.java b/kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/Member.java new file mode 100644 index 000000000..e37324e5e --- /dev/null +++ b/kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/Member.java @@ -0,0 +1,38 @@ +package com.kdt.kdtjpa.domain.order; + +import jakarta.persistence.*; +import lombok.Getter; + +import java.util.ArrayList; +import java.util.List; + +@Entity +@Table(name = "member") +@Getter +public class Member extends BaseEntity { + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE) + private Long id; + + @Column(name = "name", nullable = false, length = 30) + private String name; + + @Column(name = "nick_name", nullable = false, length = 30, unique = true) + private String nickName; + + @Column(name = "age", nullable = false) + private int age; + + @Column(name = "address", nullable = false) + private String address; + + @Column(name = "description") + private String description; + + @OneToMany(mappedBy = "member") // 연관관계 주인 설정 + private List orders = new ArrayList<>(); + + public void addOrder(Order order) { // 연관관계 편의 메서드 + order.setMember(this); + } +} diff --git a/kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/Order.java b/kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/Order.java new file mode 100644 index 000000000..2ccbd0889 --- /dev/null +++ b/kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/Order.java @@ -0,0 +1,45 @@ +package com.kdt.kdtjpa.domain.order; + +import jakarta.persistence.*; +import lombok.Getter; + +import java.time.LocalDateTime; +import java.util.List; +import java.util.Objects; + +@Entity +@Table(name = "orders") +@Getter +public class Order extends BaseEntity { + @Id + @Column(name = "id") + private String uuid; + + @Lob + private String memo; + + @Enumerated(value = EnumType.STRING) + private OrderStatus orderStatus; + + @Column(name = "order_datetime", columnDefinition = "TIMESTAMP") + private LocalDateTime orderDateTime; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "memeber_id", referencedColumnName = "id") + private Member member; + + @OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true) + List orderItems; + + public void setMember(Member member) { + if (Objects.nonNull(this.member)) { + member.getOrders().remove(this); + } + this.member = member; + member.getOrders().add(this); + } + + public void addOrderItem(OrderItem orderItem) { + orderItem.setOrder(this); + } +} diff --git a/kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/OrderItem.java b/kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/OrderItem.java new file mode 100644 index 000000000..df5f78191 --- /dev/null +++ b/kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/OrderItem.java @@ -0,0 +1,39 @@ +package com.kdt.kdtjpa.domain.order; + +import jakarta.persistence.*; +import lombok.Getter; + +import java.util.List; +import java.util.Objects; + +@Entity +@Table(name = "order_item") +@Getter +public class OrderItem extends BaseEntity { + + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE) + private Long id; + + private int price; + private int quantity; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "order_id", referencedColumnName = "id") + private Order order; + + @OneToMany(mappedBy = "orderItem", cascade = CascadeType.ALL, orphanRemoval = true) + private List items; + + public void setOrder(Order order) { + if (Objects.nonNull(this.order)) { + this.order.getOrderItems().remove(this); + } + this.order = order; + order.getOrderItems().add(this); + } + + public void addItem(Item item) { + item.setOrderItem(this); + } +} diff --git a/kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/OrderStatus.java b/kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/OrderStatus.java new file mode 100644 index 000000000..1611df9b9 --- /dev/null +++ b/kdt-jpa/src/main/java/com/kdt/kdtjpa/domain/order/OrderStatus.java @@ -0,0 +1,5 @@ +package com.kdt.kdtjpa.domain.order; + +public enum OrderStatus { + OPENED, CANCELLED +}