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 ;
11-
12- private String title ;
13-
14- private String contents ;
10+ public class Question extends BaseEntity {
11+ private QuestionContent content ;
1512
1613 private NsUser writer ;
1714
18- private List < Answer > answers = new ArrayList <> ();
15+ private Answers answers = new Answers ();
1916
2017 private boolean deleted = false ;
2118
22- private LocalDateTime createdDate = LocalDateTime .now ();
23-
24- private LocalDateTime updatedDate ;
25-
2619 public Question () {
20+ super ();
2721 }
2822
2923 public Question (NsUser writer , String title , String contents ) {
3024 this (0L , writer , title , contents );
3125 }
3226
3327 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 ;
46- }
47-
48- public Question setTitle (String title ) {
49- this .title = title ;
50- return this ;
51- }
52-
53- public String getContents () {
54- return contents ;
28+ this (id , writer , new QuestionContent (title , contents ));
5529 }
5630
57- public Question setContents (String contents ) {
58- this .contents = contents ;
59- return this ;
31+ public Question (Long id , NsUser writer , QuestionContent content ) {
32+ super (id );
33+ this .writer = writer ;
34+ this .content = content ;
6035 }
6136
6237 public NsUser getWriter () {
@@ -72,21 +47,29 @@ public boolean isOwner(NsUser loginUser) {
7247 return writer .equals (loginUser );
7348 }
7449
75- public Question setDeleted (boolean deleted ) {
76- this .deleted = deleted ;
77- return this ;
78- }
79-
8050 public boolean isDeleted () {
8151 return deleted ;
8252 }
8353
84- public List <Answer > getAnswers () {
85- return answers ;
54+ public List <DeleteHistory > delete (NsUser loginUser ) throws CannotDeleteException {
55+ if (!isOwner (loginUser )) {
56+ throw new CannotDeleteException ("질문을 삭제할 권한이 없습니다." );
57+ }
58+
59+ List <DeleteHistory > deleteHistories = new ArrayList <>();
60+ deleteHistories .add (deleteQuestion ());
61+ deleteHistories .addAll (answers .delete (loginUser ));
62+
63+ return deleteHistories ;
64+ }
65+
66+ private DeleteHistory deleteQuestion () {
67+ this .deleted = true ;
68+ return new DeleteHistory (ContentType .QUESTION , getId (), writer , LocalDateTime .now ());
8669 }
8770
8871 @ Override
8972 public String toString () {
90- return "Question [id=" + getId () + ", title=" + title + ", contents=" + contents + ", writer=" + writer + "]" ;
73+ return "Question [id=" + getId () + ", title=" + content . getTitle () + ", contents=" + content . getContents () + ", writer=" + writer + "]" ;
9174 }
9275}
0 commit comments