diff --git a/threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/coupon/Coupon.java b/threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/coupon/Coupon.java index c004106..006038c 100644 --- a/threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/coupon/Coupon.java +++ b/threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/coupon/Coupon.java @@ -2,11 +2,10 @@ import com.threedollar.domain.BaseEntity; import jakarta.persistence.Column; +import jakarta.persistence.Embedded; import jakarta.persistence.Entity; import jakarta.persistence.EnumType; import jakarta.persistence.Enumerated; -import jakarta.persistence.Table; -import jakarta.persistence.UniqueConstraint; import java.time.LocalDateTime; @@ -14,17 +13,10 @@ import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; -import org.checkerframework.checker.units.qual.C; @Entity @Getter -@NoArgsConstructor -@Table(uniqueConstraints = { - @UniqueConstraint( - name = "uni_coupon_1", - columnNames = {} - ) -}) +@NoArgsConstructor(access = AccessLevel.PROTECTED) public class Coupon extends BaseEntity { @Column(nullable = false, length = 100) @@ -51,19 +43,15 @@ public class Coupon extends BaseEntity { @Column(nullable = false) private long count; - @Column(nullable = false) - private LocalDateTime startTime; - - @Column(nullable = false) - private LocalDateTime endTime; + @Embedded + private CouponTime couponTime; @Column(nullable = false) private String accountId; @Builder(access = AccessLevel.PRIVATE) public Coupon(String workspaceId, String targetId, String name, CouponType couponType, - CouponTag couponTag, CouponStatus status, long count, LocalDateTime startTime, LocalDateTime endTime, - String accountId) { + CouponTag couponTag, CouponStatus status, CouponTime couponTime, long count, String accountId) { this.workspaceId = workspaceId; this.targetId = targetId; this.name = name; @@ -71,23 +59,22 @@ public Coupon(String workspaceId, String targetId, String name, CouponType coupo this.couponType = couponType; this.status = status; this.count = count; - this.startTime = startTime; - this.endTime = endTime; + this.couponTime = couponTime; this.accountId = accountId; } public static Coupon newInstance(String workspaceId, String targetId, String name, - CouponType couponType, CouponTag couponTag, long count, LocalDateTime startTime, LocalDateTime endTime) { + CouponType couponType, CouponTag couponTag, long count, CouponTime couponTime, String accountId) { return Coupon.builder() .couponType(couponType) .workspaceId(workspaceId) .targetId(targetId) .name(name) .couponTag(couponTag) + .couponTime(couponTime) + .accountId(accountId) .count(count) .status(CouponStatus.ACTIVE) - .startTime(startTime) - .endTime(endTime) .build(); } } diff --git a/threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/coupon/CouponTime.java b/threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/coupon/CouponTime.java new file mode 100644 index 0000000..74b9aa2 --- /dev/null +++ b/threedollar-core/threedollar-domain/src/main/java/com/threedollar/domain/coupon/CouponTime.java @@ -0,0 +1,33 @@ +package com.threedollar.domain.coupon; + +import jakarta.persistence.Column; +import jakarta.persistence.Embeddable; + +import java.time.LocalDateTime; + +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Embeddable +@NoArgsConstructor +@Getter +public class CouponTime { + + @Column(nullable = false) + private LocalDateTime startTime; + + @Column(nullable = false) + private LocalDateTime endTime; + + public CouponTime(LocalDateTime startTime, LocalDateTime endTime) { + validateCouponTime(startTime, endTime); + this.startTime = startTime; + this.endTime = endTime; + } + + private void validateCouponTime(LocalDateTime startTime, LocalDateTime endTime) { + if (startTime.isAfter(endTime)) { + throw new IllegalArgumentException("startTime is after endTime"); + } + } +}