Skip to content

Commit

Permalink
Merge pull request #197 from marinesnow34/coupon
Browse files Browse the repository at this point in the history
Refactor: 쿠폰 멀티 유니크 설정
  • Loading branch information
marinesnow34 authored Mar 9, 2024
2 parents dab71b0 + 3a1615e commit 49e3dd3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
15 changes: 11 additions & 4 deletions src/main/java/com/readyvery/readyverydemo/domain/Coupon.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import jakarta.persistence.Version;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -25,7 +26,9 @@
@Setter
@Entity
@Builder
@Table(name = "COUPON")
@Table(name = "COUPON", uniqueConstraints = {
@UniqueConstraint(columnNames = {"coupon_detail_idx", "user_idx"})
})
@AllArgsConstructor
@NoArgsConstructor
@Slf4j
Expand All @@ -36,9 +39,13 @@ public class Coupon extends BaseTimeEntity {
@Column(name = "coupon_idx")
private Long id;

// 사용 여부
@Column(name = "used")
private boolean isUsed;
// 발급 갯수
@Column(name = "issue_count")
private int issueCount;

// 사용 갯수
@Column(name = "use_count")
private Long useCount;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "coupon_detail_idx")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public CouponsRes toCouponsRes(List<Coupon> coupons) {
return CouponsRes.builder()
//filter로 isUsed가 false인 쿠폰만 가져옴
.coupons(coupons.stream()
.filter(coupon -> !coupon.isUsed())
.filter(coupon -> coupon.getIssueCount() - coupon.getUseCount() > 0)
.map(this::toCouponDto)
.toList())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ private void isCoupon(Coupon coupon) {
if (coupon == null) {
return;
}
if (!coupon.isUsed()) {
if (coupon.getIssueCount() - coupon.getUseCount() > 0) {
return;
}
if (coupon.getCouponDetail().getExpire().isAfter(LocalDateTime.now())) {
Expand Down Expand Up @@ -423,7 +423,7 @@ private void applyCancelTosspaymentDto(Order order, TosspaymentDto tosspaymentDt
order.getReceipt().setStatus(tosspaymentDto.getStatus());
order.getUserInfo().setPoint(order.getUserInfo().getPoint() - order.getPoint());
if (order.getCoupon() != null) {
order.getCoupon().setUsed(false);
order.getCoupon().setUseCount(order.getCoupon().getUseCount() - 1);
}

}
Expand Down Expand Up @@ -501,7 +501,7 @@ private void applyTosspaymentDto(Order order, TosspaymentDto tosspaymentDto) {
order.setMessage(TOSSPAYMENT_SUCCESS_MESSAGE);
order.getUserInfo().setPoint(order.getUserInfo().getPoint() + order.getPoint());
if (order.getCoupon() != null) {
order.getCoupon().setUsed(true);
order.getCoupon().setUseCount(order.getCoupon().getUseCount() + 1);
}
}

Expand Down

0 comments on commit 49e3dd3

Please sign in to comment.