-
Notifications
You must be signed in to change notification settings - Fork 423
Expand file tree
/
Copy pathQuestion.java
More file actions
101 lines (79 loc) · 2.48 KB
/
Question.java
File metadata and controls
101 lines (79 loc) · 2.48 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
package qna.domain;
import org.hibernate.annotations.Where;
import qna.CannotDeleteException;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
public class Question extends AbstractEntity {
@Column(length = 100, nullable = false)
private String title;
@Lob
private String contents;
@ManyToOne
@JoinColumn(foreignKey = @ForeignKey(name = "fk_question_writer"))
private User writer;
@OneToMany(mappedBy = "question", cascade = CascadeType.ALL)
@Where(clause = "deleted = false")
@OrderBy("id ASC")
private List<Answer> answers = new ArrayList<>();
private boolean deleted = false;
public Question() {
}
public Question(String title, String contents) {
this.title = title;
this.contents = contents;
}
public Question(String title, String contents, List<Answer> answers) {
this.title = title;
this.contents = contents;
this.answers = answers;
}
public Question(long id, String title, String contents) {
super(id);
this.title = title;
this.contents = contents;
}
public User getWriter() {
return writer;
}
public Question writeBy(User loginUser) {
this.writer = loginUser;
return this;
}
public void addAnswer(Answer answer) {
answer.toQuestion(this);
answers.add(answer);
}
private boolean isOwner(User loginUser) {
return writer.equals(loginUser);
}
public Question setDeleted(boolean deleted) {
this.deleted = deleted;
return this;
}
public boolean isDeleted() {
return deleted;
}
public List<Answer> getAnswers() {
return answers;
}
@Override
public String toString() {
return "Question [id=" + getId() + ", title=" + title + ", contents=" + contents + ", writer=" + writer + "]";
}
public void validate(User loginUser) throws CannotDeleteException {
validateQuestionAuthority(loginUser);
validateAnswerExists(loginUser);
}
private void validateQuestionAuthority(User loginUser) throws CannotDeleteException {
if (!isOwner(loginUser)) {
throw new CannotDeleteException("질문을 삭제할 권한이 없습니다.");
}
}
private void validateAnswerExists(User loginUser) throws CannotDeleteException {
for (Answer answer : answers) {
answer.validateAnswerExists(loginUser);
}
}
}