-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComment.java
96 lines (75 loc) · 2.8 KB
/
Comment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package nova.mjs.comment.entity;
import lombok.*;
import jakarta.persistence.*;
import nova.mjs.comment.likes.entity.CommentLike;
import nova.mjs.util.entity.BaseEntity;
import nova.mjs.community.entity.CommunityBoard;
import nova.mjs.member.Member;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@Entity
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "comment")
public class Comment extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "comments_id")
private long id; // 댓글 id
@Column(nullable = false, unique = true, updatable = false)
private UUID uuid;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "community_board_id", nullable = false)
private CommunityBoard communityBoard ; // 댓글이 속한 게시물
@ManyToOne
@JoinColumn(name = "member_id", nullable = false)
private Member member; // 작성자의 nickname, id
@Column(columnDefinition = "TEXT", nullable = false)
private String content; // 내용
@Column
private int likeCount; // 좋아요 수
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id") // 부모 댓글 ID 저장
private Comment parent;
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Comment> replies = new ArrayList<>();
@OneToMany(mappedBy = "comment", cascade = CascadeType.ALL, orphanRemoval = true)
private List<CommentLike> commentLike = new ArrayList<>();
public static Comment create(CommunityBoard communityBoard, Member member, String content) {
return Comment.builder()
.uuid(UUID.randomUUID())
.communityBoard(communityBoard)
.member(member)
.content(content)
.likeCount(0) // 기본값 설정
.build();
}
// 부모가 있는 댓글(대댓글) 생성
public static Comment createReply(Comment parent, Member member, String content) {
// 부모 댓글이 속한 CommunityBoard를 그대로 사용 (부모와 같은 게시글)
CommunityBoard board = parent.getCommunityBoard();
Comment reply = Comment.builder()
.uuid(UUID.randomUUID())
.communityBoard(board)
.member(member)
.content(content)
.likeCount(0)
.parent(parent) // 부모 설정
.build();
// 부모의 replies에도 연결
parent.getReplies().add(reply);
return reply;
}
// 게시물 좋아요
public void increaseLikeCommentCount() {
this.likeCount++;
}
public void decreaseLikeCommentCount() {
if (this.likeCount > 0) {
this.likeCount--;
}
}
}