Skip to content

Commit

Permalink
update: Updated basic domains
Browse files Browse the repository at this point in the history
- added more fields and methods to domain classes
- fixed import statements (from * to individual import)
  • Loading branch information
koreanMike513 committed Dec 28, 2024
1 parent 64efe13 commit 405fa00
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 11 deletions.
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 BigDecimal.valueOf((long) price.intValue() * 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) {
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

0 comments on commit 405fa00

Please sign in to comment.