Skip to content

Commit

Permalink
refactor: 결제 테이블 coupon_id 컬럼을 discount_amount로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
kmebin committed Nov 26, 2023
1 parent bde153c commit abe23c7
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static Payment toPayment(Long memberId, Product product) {
.memberId(memberId)
.product(product)
.order(order)
.amount(product.getPrice())
.totalAmount(product.getPrice())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static PurchaseProductResponse toPurchaseProductResponse(Payment payment)
return PurchaseProductResponse.builder()
.paymentId(payment.getId())
.orderName(payment.getOrder().getName())
.price(payment.getAmount())
.price(payment.getTotalAmount())
.build();
}
}
29 changes: 14 additions & 15 deletions src/main/java/com/moabam/api/domain/payment/Payment.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,17 @@ public class Payment {
@JoinColumn(name = "product_id", updatable = false, nullable = false)
private Product product;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "coupon_id")
private Coupon coupon;

@Column(name = "coupon_wallet_id")
private Long couponWalletId;

@Embedded
private Order order;

@Column(name = "amount", nullable = false)
private int amount;
@Column(name = "total_amount", nullable = false)
private int totalAmount;

@Column(name = "discount_amount", nullable = false)
private int discountAmount;

@Column(name = "payment_key")
private String paymentKey;
Expand All @@ -83,14 +82,14 @@ public class Payment {
private LocalDateTime approvedAt;

@Builder
public Payment(Long memberId, Product product, Coupon coupon, Long couponWalletId, Order order, int amount,
PaymentStatus status) {
public Payment(Long memberId, Product product, Long couponWalletId, Order order, int totalAmount,
int discountAmount, PaymentStatus status) {
this.memberId = requireNonNull(memberId);
this.product = requireNonNull(product);
this.coupon = coupon;
this.couponWalletId = couponWalletId;
this.order = requireNonNull(order);
this.amount = validateAmount(amount);
this.totalAmount = validateAmount(totalAmount);
this.discountAmount = validateAmount(discountAmount);
this.status = requireNonNullElse(status, PaymentStatus.READY);
}

Expand All @@ -104,7 +103,7 @@ private int validateAmount(int amount) {

public void validateInfo(Long memberId, int amount) {
validateByMember(memberId);
validateByAmount(amount);
validateByTotalAmount(amount);
}

public void validateByMember(Long memberId) {
Expand All @@ -113,8 +112,8 @@ public void validateByMember(Long memberId) {
}
}

private void validateByAmount(int amount) {
if (this.amount != amount) {
private void validateByTotalAmount(int amount) {
if (this.totalAmount != amount) {
throw new BadRequestException(INVALID_PAYMENT_INFO);
}
}
Expand All @@ -124,9 +123,9 @@ public boolean isCouponApplied() {
}

public void applyCoupon(Coupon coupon, Long couponWalletId) {
this.coupon = coupon;
this.couponWalletId = couponWalletId;
this.amount = Math.max(MIN_AMOUNT, this.amount - coupon.getPoint());
this.discountAmount = coupon.getPoint();
this.totalAmount = Math.max(MIN_AMOUNT, this.totalAmount - coupon.getPoint());
}

public void request(String orderId) {
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/com/moabam/api/domain/payment/PaymentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void validate_amount_exception() {
.memberId(1L)
.product(bugProduct())
.order(order(bugProduct()))
.amount(-1000);
.totalAmount(-1000);

assertThatThrownBy(paymentBuilder::build)
.isInstanceOf(BadRequestException.class)
Expand All @@ -44,8 +44,8 @@ void success() {
payment.applyCoupon(coupon, couponWalletId);

// then
assertThat(payment.getAmount()).isEqualTo(BUG_PRODUCT_PRICE - 1000);
assertThat(payment.getCoupon()).isEqualTo(coupon);
assertThat(payment.getTotalAmount()).isEqualTo(BUG_PRODUCT_PRICE - 1000);
assertThat(payment.getDiscountAmount()).isEqualTo(coupon.getPoint());
assertThat(payment.getCouponWalletId()).isEqualTo(couponWalletId);
}

Expand All @@ -61,7 +61,7 @@ void discount_amount_greater() {
payment.applyCoupon(coupon, couponWalletId);

// then
assertThat(payment.getAmount()).isZero();
assertThat(payment.getTotalAmount()).isZero();
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/test/java/com/moabam/support/fixture/PaymentFixture.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ public static Payment payment(Product product) {
.memberId(1L)
.product(product)
.order(order(product))
.amount(product.getPrice())
.totalAmount(product.getPrice())
.build();
}

public static Payment paymentWithCoupon(Product product, Coupon coupon, Long couponWalletId) {
return Payment.builder()
.memberId(1L)
.product(product)
.coupon(coupon)
.couponWalletId(couponWalletId)
.order(order(product))
.amount(product.getPrice())
.totalAmount(product.getPrice())
.discountAmount(coupon.getPoint())
.build();
}

Expand Down

0 comments on commit abe23c7

Please sign in to comment.