Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update: Updated basic domains #18

Merged
merged 2 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/main/java/com/f_lab/la_planete/domain/Food.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package com.f_lab.la_planete.domain;

import com.f_lab.la_planete.domain.base.BaseEntity;
import jakarta.persistence.*;


import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
Expand All @@ -26,23 +33,27 @@ public class Food extends BaseEntity {

private BigDecimal price;

private int quantity;
private int totalQuantity;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "store_id")
private Store store;

public static Food of(BigDecimal price, int quantity) {
public static Food of(BigDecimal price, int totalQuantity) {
Food food = new Food();
food.setPrice(price);
food.setQuantity(quantity);
food.setTotalQuantity(totalQuantity);
return food;
}

public BigDecimal calculateCost(int quantity) {
return price.multiply(BigDecimal.valueOf(quantity));
}

public void minusQuantity(int deductions) {
if (quantity - deductions < 0)
if (totalQuantity - deductions < 0)
throw new IllegalStateException("수량이 부족합니다. 따라서 구매가 진행될 수 없습니다.");

quantity -= deductions;
totalQuantity -= deductions;
}
}
36 changes: 35 additions & 1 deletion src/main/java/com/f_lab/la_planete/domain/Order.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
package com.f_lab.la_planete.domain;

import com.f_lab.la_planete.domain.base.BaseEntity;
import jakarta.persistence.*;

import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;

import static jakarta.persistence.FetchType.LAZY;
import static jakarta.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PROTECTED;

@Entity
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = PROTECTED)
@Table(name = "orders")
public class Order extends BaseEntity {
Expand All @@ -19,10 +35,28 @@ public class Order extends BaseEntity {
@GeneratedValue(strategy = IDENTITY)
private Long id;

@ManyToOne
@JoinColumn(name = "food_id")
private Food food;

private BigDecimal totalCost;

private int quantity;

// 화폐 추가

@Enumerated(EnumType.STRING)
private OrderStatus status;

@OneToOne(fetch = LAZY)
@JoinColumn(name = "payment_id")
private Payment payment;

public void updateTotalCost(BigDecimal totalCost) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

totalCost, updateTotalCost, calculateTotalCost 중 필요한 것만 노출 고려

this.totalCost = totalCost;
}

public BigDecimal calculateTotalCost() {
return food.calculateCost(quantity);
}
}
22 changes: 19 additions & 3 deletions src/main/java/com/f_lab/la_planete/domain/Payment.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,46 @@
package com.f_lab.la_planete.domain;

import com.f_lab.la_planete.domain.base.BaseEntity;
import jakarta.persistence.*;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.math.BigDecimal;

import static jakarta.persistence.FetchType.LAZY;
import static jakarta.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PRIVATE;
import static lombok.AccessLevel.PROTECTED;

@Entity
@Getter
@Getter @Setter(PRIVATE)
@NoArgsConstructor(access = PROTECTED)
@Table(name = "payments")
public class Payment extends BaseEntity {

@Id @GeneratedValue(strategy = IDENTITY)
private Long id;

private BigDecimal amount;
private BigDecimal totalCost;

@Enumerated(EnumType.STRING)
private PaymentStatus status;

@OneToOne(mappedBy = "payment", fetch = LAZY)
private Order order;

public static Payment of(BigDecimal totalCost, Order order) {
Payment payment = new Payment();
payment.setTotalCost(totalCost);
payment.setOrder(order);
payment.setStatus(PaymentStatus.READY);
return payment;
}
}
6 changes: 5 additions & 1 deletion src/main/java/com/f_lab/la_planete/domain/Store.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.f_lab.la_planete.domain;

import com.f_lab.la_planete.domain.base.BaseTimeEntity;
import jakarta.persistence.*;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand Down
Loading