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 entities after Currency entity #26

Merged
merged 1 commit into from
Jan 2, 2025
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
8 changes: 8 additions & 0 deletions src/main/java/com/f_lab/la_planete/domain/Food.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
Expand All @@ -20,8 +22,10 @@


@Entity
@Builder
@Getter @Setter
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "foods")
public class Food extends BaseEntity {

Expand All @@ -37,6 +41,10 @@ public class Food extends BaseEntity {
@JoinColumn(name = "store_id")
private Store store;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "currency_id")
private Currency currency;

public BigDecimal calculateCost(int quantity) {
return price.multiply(BigDecimal.valueOf(quantity));
}
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/com/f_lab/la_planete/domain/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@

@Entity
@Builder
@Getter
@Setter
@Getter @Setter
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "orders")
Expand All @@ -44,22 +43,20 @@ public class Order extends BaseEntity {

private int quantity;

// 화폐 추가

@Enumerated(EnumType.STRING)
private OrderStatus status;

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

@ManyToOne
@ManyToOne(fetch = LAZY)
@JoinColumn(name = "voucher_id")
private Voucher voucher;

public BigDecimal calculateTotalCost() {
return (voucher != null)
? voucher.apply(food.calculateCost(quantity))
? voucher.apply(food.calculateCost(quantity), food.getCurrency())
: food.calculateCost(quantity);
}
}
18 changes: 6 additions & 12 deletions src/main/java/com/f_lab/la_planete/domain/Payment.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import jakarta.persistence.Id;
import jakarta.persistence.OneToOne;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
Expand All @@ -16,12 +18,12 @@

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

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

Expand All @@ -35,12 +37,4 @@ public class Payment extends BaseEntity {

@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;
}
}
20 changes: 14 additions & 6 deletions src/main/java/com/f_lab/la_planete/domain/Voucher.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
package com.f_lab.la_planete.domain;

import com.f_lab.la_planete.domain.base.BaseTimeEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;

import static jakarta.persistence.GenerationType.IDENTITY;

@Entity
@Getter
@Builder
@Getter @Setter
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "vouchers")
public class Voucher {
public class Voucher extends BaseTimeEntity {

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

private LocalDateTime expiryDate;

private BigDecimal percentages;
private BigDecimal discountRate; // 소수의 값으로 표시됩니다 (e.g. 0.45 == 45%)

public BigDecimal apply(BigDecimal totalCost) {
BigDecimal afterDiscounts = BigDecimal.valueOf(1).subtract(percentages);
return totalCost.multiply(afterDiscounts);
public BigDecimal apply(BigDecimal totalCost, Currency currency) {
if (ObjectUtils.isEmpty(currency))
throw new IllegalStateException("화페 값이 비어있습니다. 적절히 변경 후 다시 시도해주시길 바랍니다.");

BigDecimal afterDiscounts = BigDecimal.ONE.subtract(discountRate);
return totalCost.multiply(afterDiscounts).setScale(currency.getRoundingScale(), currency.getRoundingMode());
}
}
11 changes: 6 additions & 5 deletions src/main/java/com/f_lab/la_planete/service/OrderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@
import com.f_lab.la_planete.repository.FoodRepository;
import com.f_lab.la_planete.repository.OrderRepository;
import com.f_lab.la_planete.repository.PaymentRepository;
import jakarta.persistence.LockTimeoutException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.PessimisticLockException;
import org.springframework.dao.PessimisticLockingFailureException;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Optional;

@Slf4j
@Service
Expand All @@ -42,8 +39,12 @@ public OrderCreateResponseDTO createFoodOrder(OrderCreateRequestDTO request) {
BigDecimal totalCost = order.calculateTotalCost();
order.setTotalCost(order.calculateTotalCost());

// TODO 추후에 따로 결제 진행 클래스를 만들어서 로직을 변경
// 결제 생성
Payment payment = Payment.of(totalCost, order);
Payment payment = Payment.builder()
.totalCost(totalCost)
.order(order)
.build();

orderRepository.save(order);
paymentRepository.save(payment);
Expand Down
Loading