diff --git a/src/main/java/com/hyetaekon/hyetaekon/answer/entity/Answer.java b/src/main/java/com/hyetaekon/hyetaekon/answer/entity/Answer.java index ddac75b..a642751 100644 --- a/src/main/java/com/hyetaekon/hyetaekon/answer/entity/Answer.java +++ b/src/main/java/com/hyetaekon/hyetaekon/answer/entity/Answer.java @@ -5,6 +5,10 @@ import com.hyetaekon.hyetaekon.user.entity.User; import jakarta.persistence.*; import lombok.*; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +import java.time.LocalDateTime; @Entity @@ -14,7 +18,8 @@ @NoArgsConstructor @AllArgsConstructor @Table(name = "answer") -public class Answer extends BaseEntity { +@EntityListeners(AuditingEntityListener.class) +public class Answer { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; // 답변 ID @@ -33,10 +38,30 @@ public class Answer extends BaseEntity { @Column(name = "selected", nullable = false) private boolean selected; // 채택 여부 + @CreatedDate + @Column(name = "created_at", nullable = false, updatable = false) + private LocalDateTime createdAt; + + @Column(name = "deleted_at") + private LocalDateTime deletedAt; + + @Column(name = "suspend_at") + private LocalDateTime suspendAt; + + // 삭제 처리 + public void delete() { + this.deletedAt = LocalDateTime.now(); + } + + // 정지 처리 + public void suspend() { + this.suspendAt = LocalDateTime.now(); + } + public String getDisplayContent() { - if (getDeletedAt() != null) { + if (this.deletedAt != null) { return "삭제된 답변입니다."; - } else if (getSuspendAt() != null) { + } else if (this.suspendAt != null) { return "관리자에 의해 삭제된 답변입니다."; } return content; diff --git a/src/main/java/com/hyetaekon/hyetaekon/comment/entity/Comment.java b/src/main/java/com/hyetaekon/hyetaekon/comment/entity/Comment.java index f33a837..2a40fe2 100644 --- a/src/main/java/com/hyetaekon/hyetaekon/comment/entity/Comment.java +++ b/src/main/java/com/hyetaekon/hyetaekon/comment/entity/Comment.java @@ -5,6 +5,8 @@ import com.hyetaekon.hyetaekon.user.entity.User; import jakarta.persistence.*; import lombok.*; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; import java.time.LocalDateTime; @@ -15,7 +17,8 @@ @NoArgsConstructor @AllArgsConstructor @Table(name = "comments") -public class Comment extends BaseEntity { +@EntityListeners(AuditingEntityListener.class) +public class Comment { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @@ -33,10 +36,30 @@ public class Comment extends BaseEntity { @Column(nullable = false, length = 1000) private String content; + @CreatedDate + @Column(name = "created_at", nullable = false, updatable = false) + private LocalDateTime createdAt; + + @Column(name = "deleted_at") + private LocalDateTime deletedAt; + + @Column(name = "suspend_at") + private LocalDateTime suspendAt; + + // 삭제 처리 + public void delete() { + this.deletedAt = LocalDateTime.now(); + } + + // 정지 처리 + public void suspend() { + this.suspendAt = LocalDateTime.now(); + } + public String getDisplayContent() { - if (getDeletedAt() != null) { + if (this.deletedAt != null) { return "삭제된 댓글입니다."; - } else if (getSuspendAt() != null) { + } else if (this.suspendAt != null) { return "관리자에 의해 삭제된 댓글입니다."; } return content; diff --git a/src/main/java/com/hyetaekon/hyetaekon/comment/service/CommentService.java b/src/main/java/com/hyetaekon/hyetaekon/comment/service/CommentService.java index 303625e..c1e8ef2 100644 --- a/src/main/java/com/hyetaekon/hyetaekon/comment/service/CommentService.java +++ b/src/main/java/com/hyetaekon/hyetaekon/comment/service/CommentService.java @@ -17,7 +17,6 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.time.LocalDateTime; @Service @RequiredArgsConstructor diff --git a/src/main/java/com/hyetaekon/hyetaekon/common/util/BaseEntity.java b/src/main/java/com/hyetaekon/hyetaekon/common/util/BaseEntity.java index 362555c..eff46bc 100644 --- a/src/main/java/com/hyetaekon/hyetaekon/common/util/BaseEntity.java +++ b/src/main/java/com/hyetaekon/hyetaekon/common/util/BaseEntity.java @@ -25,25 +25,4 @@ public class BaseEntity { @Column(name = "modified_at", nullable = false, columnDefinition = "DATETIME(0)") private LocalDateTime modifiedAt; - @Column(name = "deleted_at") - private LocalDateTime deletedAt; - - @Column(name = "suspend_at") - private LocalDateTime suspendAt; - - // 삭제 처리 - public void delete() { - this.deletedAt = LocalDateTime.now(); - } - - // 정지 처리 - public void suspend() { - this.suspendAt = LocalDateTime.now(); - } - - // 복원시 - public void restore() { - this.deletedAt = null; - this.suspendAt = null; - } } \ No newline at end of file diff --git a/src/main/java/com/hyetaekon/hyetaekon/post/entity/Post.java b/src/main/java/com/hyetaekon/hyetaekon/post/entity/Post.java index 7110312..65c7980 100644 --- a/src/main/java/com/hyetaekon/hyetaekon/post/entity/Post.java +++ b/src/main/java/com/hyetaekon/hyetaekon/post/entity/Post.java @@ -7,6 +7,9 @@ import com.hyetaekon.hyetaekon.user.entity.User; import jakarta.persistence.*; import lombok.*; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; import java.time.LocalDateTime; import java.util.ArrayList; @@ -19,7 +22,8 @@ @NoArgsConstructor @AllArgsConstructor @Table(name = "post") -public class Post extends BaseEntity { +@EntityListeners(AuditingEntityListener.class) +public class Post { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -69,6 +73,16 @@ public class Post extends BaseEntity { @Column(name = "category_id") private Long categoryId; + @CreatedDate + @Column(name = "created_at", nullable = false, updatable = false) + private LocalDateTime createdAt; + + @Column(name = "deleted_at") + private LocalDateTime deletedAt; + + @Column(name = "suspend_at") + private LocalDateTime suspendAt; + @Builder.Default @OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true) @@ -101,11 +115,20 @@ public void decrementCommentCnt() { this.commentCnt = Math.max(0, this.commentCnt - 1); } + // 삭제 처리 + public void delete() { + this.deletedAt = LocalDateTime.now(); + } + + // 정지 처리 + public void suspend() { + this.suspendAt = LocalDateTime.now(); + } public String getDisplayContent() { - if (getDeletedAt() != null) { + if (this.deletedAt != null) { return "삭제된 게시글입니다."; - } else if (getSuspendAt() != null) { + } else if (this.suspendAt != null) { return "관리자에 의해 삭제된 게시글입니다."; } return content;