-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathAnswer.java
More file actions
92 lines (68 loc) · 2.09 KB
/
Answer.java
File metadata and controls
92 lines (68 loc) · 2.09 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
package nextstep.qna.domain;
import nextstep.qna.CannotDeleteException;
import nextstep.qna.NotFoundException;
import nextstep.qna.UnAuthorizedException;
import nextstep.users.domain.NsUser;
import java.time.LocalDateTime;
public class Answer {
private Long id;
private NsUser writer;
private Question question;
private String contents;
private boolean deleted = false;
private LocalDateTime createdDate = LocalDateTime.now();
private LocalDateTime updatedDate;
public Answer() {
}
public Answer(NsUser writer, Question question, String contents) {
this(null, writer, question, contents);
}
public Answer(Long id, NsUser writer, Question question, String contents) {
this.id = id;
if(writer == null) {
throw new UnAuthorizedException();
}
if(question == null) {
throw new NotFoundException();
}
this.writer = writer;
this.question = question;
this.contents = contents;
}
public void validateOwnership(NsUser loginUser) throws CannotDeleteException {
if (!isOwner(loginUser)) {
throw new CannotDeleteException("답변을 삭제할 권한이 없습니다.");
}
}
public DeleteHistory delete(NsUser loginUser) throws CannotDeleteException {
validateOwnership(loginUser);
deleted = true;
return DeleteHistory.ofAnswer(this);
}
public Long getId() {
return id;
}
public Answer setDeleted(boolean deleted) {
this.deleted = deleted;
return this;
}
public boolean isDeleted() {
return deleted;
}
public boolean isOwner(NsUser writer) {
return this.writer.equals(writer);
}
public NsUser getWriter() {
return writer;
}
public String getContents() {
return contents;
}
public void toQuestion(Question question) {
this.question = question;
}
@Override
public String toString() {
return "Answer [id=" + getId() + ", writer=" + writer + ", contents=" + contents + "]";
}
}