-
Notifications
You must be signed in to change notification settings - Fork 423
Expand file tree
/
Copy pathAnswer.java
More file actions
82 lines (62 loc) · 1.91 KB
/
Answer.java
File metadata and controls
82 lines (62 loc) · 1.91 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
package qna.domain;
import qna.CannotDeleteException;
import qna.NotFoundException;
import qna.UnAuthorizedException;
import javax.persistence.*;
@Entity
public class Answer extends AbstractEntity {
@ManyToOne(optional = false)
@JoinColumn(foreignKey = @ForeignKey(name = "fk_answer_writer"))
private User writer;
@ManyToOne(optional = false)
@JoinColumn(foreignKey = @ForeignKey(name = "fk_answer_to_question"))
private Question question;
@Lob
private String contents;
private boolean deleted = false;
public Answer() {
}
public Answer(User writer, Question question, String contents) {
this(null, writer, question, contents);
}
public Answer(Long id, User writer, Question question, String contents) {
super(id);
if(writer == null) {
throw new UnAuthorizedException();
}
if(question == null) {
throw new NotFoundException();
}
this.writer = writer;
this.question = question;
this.contents = contents;
}
public Answer setDeleted(boolean deleted) {
this.deleted = deleted;
return this;
}
public boolean isDeleted() {
return deleted;
}
private boolean isOwner(User writer) {
return this.writer.equals(writer);
}
public User 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 + "]";
}
public void validateAnswerExists(User loginUser) throws CannotDeleteException {
if (!isOwner(loginUser)) {
throw new CannotDeleteException("다른 사람이 쓴 답변이 있어 삭제할 수 없습니다.");
}
}
}