-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNote.java
More file actions
177 lines (145 loc) · 5.18 KB
/
Note.java
File metadata and controls
177 lines (145 loc) · 5.18 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.projectlyrics.server.domain.note.entity;
import com.projectlyrics.server.domain.bookmark.domain.Bookmark;
import com.projectlyrics.server.domain.comment.domain.Comment;
import com.projectlyrics.server.domain.common.entity.BaseEntity;
import com.projectlyrics.server.domain.like.domain.Like;
import com.projectlyrics.server.domain.note.exception.TooManyDraftsException;
import com.projectlyrics.server.domain.song.entity.Song;
import com.projectlyrics.server.domain.user.entity.User;
import jakarta.persistence.*;
import java.util.stream.Collectors;
import lombok.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@Getter
@Entity
@Table(name = "notes")
@EqualsAndHashCode(of = "id", callSuper = false)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Note extends BaseEntity {
private static final long MAX_DRAFT_NUMBER = 20;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(length = 1000)
private String content;
@Enumerated(EnumType.STRING)
private NoteStatus noteStatus;
@Enumerated(EnumType.STRING)
@Column(nullable = false, columnDefinition = "VARCHAR(50) DEFAULT 'FREE'")
private NoteType noteType;
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name="lyrics_id")
private Lyrics lyrics;
@ManyToOne(fetch = FetchType.LAZY)
private User publisher;
@ManyToOne(fetch = FetchType.LAZY)
private Song song;
@OneToMany(mappedBy = "note", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Comment> comments = new ArrayList<>();
@OneToMany(mappedBy = "note", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Like> likes = new ArrayList<>();
@OneToMany(mappedBy = "note", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Bookmark> bookmarks = new ArrayList<>();
private Note(
Long id,
String content,
Lyrics lyrics,
NoteStatus noteStatus,
NoteType noteType,
User publisher,
Song song
) {
this.id = id;
this.content = content;
this.noteStatus = noteStatus;
this.noteType = noteType;
this.publisher = publisher;
this.song = song;
addLyrics(lyrics);
}
private Note(
String content,
Lyrics lyrics,
NoteStatus noteStatus,
NoteType noteType,
User publisher,
Song song
) {
this(null, content, lyrics, noteStatus, noteType, publisher, song);
}
public static Note create(NoteCreate noteCreate) {
return new Note(
noteCreate.content(),
Lyrics.of(noteCreate.lyrics(), noteCreate.background()),
noteCreate.status(),
noteCreate.noteType(),
noteCreate.publisher(),
noteCreate.song()
);
}
public static Note createWithId(Long id, NoteCreate noteCreate) {
return new Note(
id,
noteCreate.content(),
Lyrics.of(noteCreate.lyrics(), noteCreate.background()),
noteCreate.status(),
noteCreate.noteType(),
noteCreate.publisher(),
noteCreate.song()
);
}
private void addLyrics(Lyrics lyrics) {
if (!Objects.isNull(lyrics)) {
this.lyrics = lyrics;
}
}
public boolean isPublisher(Long userId) {
return publisher.getId().equals(userId);
}
public Note update(NoteUpdate noteUpdate) {
updateContent(noteUpdate.content());
updateLyrics(Lyrics.of(noteUpdate.lyrics(), noteUpdate.background()));
updateNoteStatus(noteUpdate.status());
return this;
}
private void updateContent(String content) {
if (!content.isEmpty()) {
this.content = content;
}
}
private void updateLyrics(Lyrics lyrics) {
this.lyrics = lyrics;
}
private void updateNoteStatus(NoteStatus noteStatus) {
if (!Objects.isNull(noteStatus)) {
this.noteStatus = noteStatus;
}
}
public static void checkDraftNumber(long draftNumber) {
if (draftNumber >= MAX_DRAFT_NUMBER) {
throw new TooManyDraftsException();
}
}
public boolean isLiked(Long userId) {
return likes.stream()
.anyMatch(like -> like.isUser(userId) && like.isInUse());
}
public boolean isBookmarked(Long userId) {
return bookmarks.stream()
.anyMatch(bookmark -> bookmark.isUser(userId) && bookmark.isInUse());
}
public List<Comment> getComments() {
return comments.stream().filter(comment -> comment.isInUse()).collect(Collectors.toList());
}
public List<Like> getLikes() {
return likes.stream().filter(like -> like.isInUse()).collect(Collectors.toList());
}
public List<Bookmark> getBookmarks() {
return bookmarks.stream().filter(bookmark -> bookmark.isInUse()).collect(Collectors.toList());
}
public void setComments(List<Comment> comments) {
this.comments = comments;
}
}