-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathQuestion.java
More file actions
110 lines (81 loc) · 2.48 KB
/
Question.java
File metadata and controls
110 lines (81 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
102
103
104
105
106
107
108
109
110
package nextstep.qna.domain;
import nextstep.qna.CannotDeleteException;
import nextstep.users.domain.NsUser;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
public class Question {
private Long id;
private String title;
private String contents;
private NsUser writer;
private Answers answers;
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;
this.answers = new Answers();
}
public List<DeleteHistory> delete(NsUser loginUser) throws CannotDeleteException {
validateOwnership(loginUser);
this.deleted = true;
List<DeleteHistory> deleteHistories = new ArrayList<>();
deleteHistories.add(DeleteHistory.ofQuestion(this));
deleteHistories.addAll(answers.deleteAnswers(loginUser));
return deleteHistories;
}
private void validateOwnership(NsUser loginUser) throws CannotDeleteException {
if (!isOwner(loginUser)) {
throw new CannotDeleteException("질문을 삭제할 권한이 없습니다.");
}
}
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);
}
private boolean isOwner(NsUser loginUser) {
return writer.equals(loginUser);
}
public Question setDeleted(boolean deleted) {
return this;
}
public boolean isDeleted() {
return deleted;
}
public Answers getAnswers() {
return answers;
}
@Override
public String toString() {
return "Question [id=" + getId() + ", title=" + title + ", contents=" + contents + ", writer=" + writer + "]";
}
}