|
4 | 4 | import nextstep.qna.UnAuthorizedException; |
5 | 5 | import nextstep.users.domain.NsUser; |
6 | 6 |
|
7 | | -import java.time.LocalDateTime; |
8 | | - |
9 | | -public class Answer { |
10 | | - private Long id; |
11 | | - |
12 | | - private NsUser writer; |
13 | | - |
| 7 | +public class Answer extends BaseEntity { |
| 8 | + private AnswerPost post; |
14 | 9 | private Question question; |
| 10 | + private boolean deleted; |
15 | 11 |
|
16 | | - private String contents; |
17 | | - |
18 | | - private boolean deleted = false; |
19 | | - |
20 | | - private LocalDateTime createdDate = LocalDateTime.now(); |
21 | | - |
22 | | - private LocalDateTime updatedDate; |
23 | | - |
24 | | - public Answer() { |
| 12 | + public Answer(Long id, AnswerPost post, Question question, boolean deleted) { |
| 13 | + super(id); |
| 14 | + validateQuestion(question); |
| 15 | + this.post = post; |
| 16 | + this.question = question; |
| 17 | + this.deleted = deleted; |
25 | 18 | } |
26 | 19 |
|
27 | 20 | public Answer(NsUser writer, Question question, String contents) { |
28 | 21 | this(null, writer, question, contents); |
29 | 22 | } |
30 | 23 |
|
31 | 24 | public Answer(Long id, NsUser writer, Question question, String contents) { |
32 | | - this.id = id; |
33 | | - if(writer == null) { |
| 25 | + this(id, new AnswerPost(writer, contents), question, false); |
| 26 | + validateWriter(writer); |
| 27 | + } |
| 28 | + |
| 29 | + private static void validateWriter(NsUser writer) { |
| 30 | + if (writer == null) { |
34 | 31 | throw new UnAuthorizedException(); |
35 | 32 | } |
| 33 | + } |
36 | 34 |
|
37 | | - if(question == null) { |
| 35 | + private static void validateQuestion(Question question) { |
| 36 | + if (question == null) { |
38 | 37 | throw new NotFoundException(); |
39 | 38 | } |
40 | | - |
41 | | - this.writer = writer; |
42 | | - this.question = question; |
43 | | - this.contents = contents; |
44 | | - } |
45 | | - |
46 | | - public Long getId() { |
47 | | - return id; |
48 | | - } |
49 | | - |
50 | | - public Answer setDeleted(boolean deleted) { |
51 | | - this.deleted = deleted; |
52 | | - return this; |
53 | 39 | } |
54 | 40 |
|
55 | 41 | public boolean isDeleted() { |
56 | 42 | return deleted; |
57 | 43 | } |
58 | 44 |
|
59 | | - public boolean isOwner(NsUser writer) { |
60 | | - return this.writer.equals(writer); |
| 45 | + public boolean isOwner(NsUser user) { |
| 46 | + return post.isOwner(user); |
61 | 47 | } |
62 | 48 |
|
63 | 49 | public NsUser getWriter() { |
64 | | - return writer; |
| 50 | + return post.getWriter(); |
65 | 51 | } |
66 | 52 |
|
67 | | - public String getContents() { |
68 | | - return contents; |
| 53 | + public void delete() { |
| 54 | + this.deleted = true; |
69 | 55 | } |
70 | 56 |
|
71 | | - public void toQuestion(Question question) { |
72 | | - this.question = question; |
| 57 | + public DeleteHistory createDeleteHistory() { |
| 58 | + return new DeleteHistory(ContentType.ANSWER, getId(), getWriter()); |
73 | 59 | } |
74 | 60 |
|
75 | 61 | @Override |
76 | 62 | public String toString() { |
77 | | - return "Answer [id=" + getId() + ", writer=" + writer + ", contents=" + contents + "]"; |
| 63 | + return "Answer [id=" + getId() + ", writer=" + getWriter() + ", contents=" + post.getContents() + "]"; |
78 | 64 | } |
79 | 65 | } |
0 commit comments