|
| 1 | +package com.wypl.jpacore; |
| 2 | + |
| 3 | +import java.time.LocalDateTime; |
| 4 | + |
| 5 | +import org.springframework.data.annotation.CreatedDate; |
| 6 | +import org.springframework.data.annotation.LastModifiedDate; |
| 7 | +import org.springframework.data.jpa.domain.support.AuditingEntityListener; |
| 8 | + |
| 9 | +import com.wypl.common.exception.WyplException; |
| 10 | + |
| 11 | +import jakarta.persistence.Column; |
| 12 | +import jakarta.persistence.EntityListeners; |
| 13 | +import jakarta.persistence.MappedSuperclass; |
| 14 | +import lombok.Getter; |
| 15 | +import lombok.NoArgsConstructor; |
| 16 | + |
| 17 | +@Getter |
| 18 | +@MappedSuperclass |
| 19 | +@NoArgsConstructor |
| 20 | +@EntityListeners(AuditingEntityListener.class) |
| 21 | +public abstract class JpaBaseEntity { |
| 22 | + |
| 23 | + @CreatedDate |
| 24 | + @Column(name = "created_at", nullable = false, updatable = false) |
| 25 | + private LocalDateTime createdAt; |
| 26 | + |
| 27 | + @LastModifiedDate |
| 28 | + @Column(name = "modified_at", nullable = false) |
| 29 | + private LocalDateTime modifiedAt; |
| 30 | + |
| 31 | + @Column(name = "deleted_at") |
| 32 | + private LocalDateTime deletedAt; |
| 33 | + |
| 34 | + public void delete() { |
| 35 | + if (isDeleted()) { |
| 36 | + throw new WyplException(JpaErrorCode.ALREADY_DELETED_ENTITY); |
| 37 | + } |
| 38 | + this.deletedAt = LocalDateTime.now(); |
| 39 | + } |
| 40 | + |
| 41 | + public void restore() { |
| 42 | + if (isNotDeleted()) { |
| 43 | + throw new WyplException(JpaErrorCode.NON_DELETED_ENTITY); |
| 44 | + } |
| 45 | + this.deletedAt = null; |
| 46 | + } |
| 47 | + |
| 48 | + public boolean isNotDeleted() { |
| 49 | + return deletedAt == null; |
| 50 | + } |
| 51 | + |
| 52 | + public boolean isDeleted() { |
| 53 | + return !isNotDeleted(); |
| 54 | + } |
| 55 | +} |
0 commit comments