-
Notifications
You must be signed in to change notification settings - Fork 297
/
Copy pathQuestion.java
129 lines (98 loc) · 3.2 KB
/
Question.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
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
package nextstep.qna.domain;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import nextstep.qna.CannotDeleteException;
import nextstep.users.domain.NsUser;
public class Question {
private Long id;
private String title;
private String contents;
private NsUser writer;
private List<Answer> answers = new ArrayList<>();
private boolean deleted = false;
private LocalDateTime createdDate = LocalDateTime.now();
private LocalDateTime updatedDate;
public Question() {
}
public Question(NsUser writer, String title, String contents) {
this(0L, writer, title, contents);
}
public Question(Long id, NsUser writer, String title, String contents) {
this.id = id;
this.writer = writer;
this.title = title;
this.contents = contents;
}
public Long getId() {
return id;
}
public String getTitle() {
return title;
}
public Question setTitle(String title) {
this.title = title;
return this;
}
public String getContents() {
return contents;
}
public Question setContents(String contents) {
this.contents = contents;
return this;
}
public NsUser getWriter() {
return writer;
}
public void addAnswer(Answer answer) {
answer.toQuestion(this);
answers.add(answer);
}
public boolean isOwner(NsUser 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 + "]";
}
private Question deleteQuestion(NsUser writer) throws CannotDeleteException {
if (!this.isOwner(writer)) {
throw new CannotDeleteException("질문을 삭제할 권한이 없습니다.");
}
return this.setDeleted(true);
}
private List<Answer> deleteAnswersOfThisQuestion(NsUser writer) {
return getAnswers().stream().map(answer -> {
try {
return answer.delete(writer);
} catch (CannotDeleteException e) {
throw new RuntimeException(e);
}
}).collect(Collectors.toList());
}
public List<DeleteHistory> deleteQuestionAndRelatedAnswer(NsUser writer, LocalDateTime now)
throws CannotDeleteException {
Question question = deleteQuestion(writer);
List<Answer> answers = deleteAnswersOfThisQuestion(writer);
List<DeleteHistory> deleteHistories = new ArrayList<>();
deleteHistories.add(
new DeleteHistory(ContentType.QUESTION, question.getId(), question.getWriter(), now));
for (Answer answer : answers) {
deleteHistories.add(
new DeleteHistory(ContentType.ANSWER, answer.getId(), answer.getWriter(), now));
}
return deleteHistories;
}
}