11package nextstep .qna .domain ;
22
3+ import nextstep .qna .CannotDeleteException ;
34import nextstep .users .domain .NsUser ;
45
56import java .time .LocalDateTime ;
67import java .util .ArrayList ;
78import java .util .List ;
89
9- public class Question {
10- private Long id ;
10+ public class Question extends BaseEntity {
11+ private PostContent postContent ;
1112
12- private String title ;
13-
14- private String contents ;
15-
16- private NsUser writer ;
17-
18- private List <Answer > answers = new ArrayList <>();
19-
20- private boolean deleted = false ;
21-
22- private LocalDateTime createdDate = LocalDateTime .now ();
23-
24- private LocalDateTime updatedDate ;
13+ private Answers answers = new Answers ();
2514
2615 public Question () {
2716 }
2817
2918 public Question (NsUser writer , String title , String contents ) {
30- this (0L , writer , title , contents );
19+ this (0L , new PostContent ( writer , title , contents ) );
3120 }
3221
3322 public Question (Long id , NsUser writer , String title , String contents ) {
34- this .id = id ;
35- this .writer = writer ;
36- this .title = title ;
37- this .contents = contents ;
38- }
39-
40- public Long getId () {
41- return id ;
42- }
43-
44- public String getTitle () {
45- return title ;
23+ this (id , new PostContent (writer , title , contents ));
4624 }
47-
48- public Question setTitle (String title ) {
49- this .title = title ;
50- return this ;
51- }
52-
53- public String getContents () {
54- return contents ;
55- }
56-
57- public Question setContents (String contents ) {
58- this .contents = contents ;
59- return this ;
25+ public Question (Long id , PostContent postContent ) {
26+ super (id );
27+ this .postContent = postContent ;
6028 }
6129
6230 public NsUser getWriter () {
63- return writer ;
31+ return postContent . getWriter () ;
6432 }
6533
6634 public void addAnswer (Answer answer ) {
@@ -69,24 +37,45 @@ public void addAnswer(Answer answer) {
6937 }
7038
7139 public boolean isOwner (NsUser loginUser ) {
72- return writer . equals (loginUser );
40+ return postContent . isOwner (loginUser );
7341 }
7442
75- public Question setDeleted ( boolean deleted ) {
76- this . deleted = deleted ;
77- return this ;
43+ public void markAsDeleted ( NsUser loginUser ) throws CannotDeleteException {
44+ validateDeletableBy ( loginUser ) ;
45+ markAsDeleted () ;
7846 }
7947
80- public boolean isDeleted () {
81- return deleted ;
48+ public void delete (NsUser loginUser ) throws CannotDeleteException {
49+ this .markAsDeleted (loginUser );
50+ answers .delete (loginUser );
51+ }
52+
53+ public List <DeleteHistory > createDeleteHistories () {
54+ List <DeleteHistory > histories = new ArrayList <>();
55+
56+ histories .add (new DeleteHistory (
57+ ContentType .QUESTION ,
58+ getId (),
59+ getWriter (),
60+ LocalDateTime .now ()
61+ ));
62+
63+ histories .addAll (answers .createDeleteHistories ());
64+
65+ return histories ;
8266 }
8367
84- public List <Answer > getAnswers () {
85- return answers ;
68+ public void validateDeletableBy (NsUser loginUser ) throws CannotDeleteException {
69+ if (!isOwner (loginUser )) {
70+ throw new CannotDeleteException ("질문을 삭제할 권한이 없습니다." );
71+ }
8672 }
8773
8874 @ Override
8975 public String toString () {
90- return "Question [id=" + getId () + ", title=" + title + ", contents=" + contents + ", writer=" + writer + "]" ;
76+ return "Question{" +
77+ "postContent=" + postContent +
78+ ", answers=" + answers +
79+ '}' ;
9180 }
9281}
0 commit comments