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+ public class Question extends SoftDeletableModel {
1011 private Long id ;
1112
12- private String title ;
13+ private QuestionBody title ;
1314
14- private String contents ;
15+ private QuestionBody contents ;
1516
1617 private NsUser writer ;
1718
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 ;
19+ private Answers answers = new Answers ();
2520
2621 public Question () {
2722 }
@@ -31,6 +26,10 @@ public Question(NsUser writer, String title, String contents) {
3126 }
3227
3328 public Question (Long id , NsUser writer , String title , String contents ) {
29+ this (id , writer , new QuestionBody (title ), new QuestionBody (contents ));
30+ }
31+
32+ public Question (Long id , NsUser writer , QuestionBody title , QuestionBody contents ) {
3433 this .id = id ;
3534 this .writer = writer ;
3635 this .title = title ;
@@ -41,52 +40,56 @@ public Long getId() {
4140 return id ;
4241 }
4342
44- public String getTitle () {
45- return title ;
43+ public NsUser getWriter () {
44+ return writer ;
4645 }
4746
48- public Question setTitle ( String title ) {
49- this . title = title ;
50- return this ;
47+ public void addAnswer ( Answer answer ) {
48+ answer . toQuestion ( this ) ;
49+ this . answers . add ( answer ) ;
5150 }
5251
53- public String getContents ( ) {
54- return contents ;
52+ public boolean isOwner ( NsUser loginUser ) {
53+ return writer . equals ( loginUser ) ;
5554 }
5655
57- public Question setContents (String contents ) {
58- this .contents = contents ;
59- return this ;
56+ public boolean isDeleted () {
57+ return getDeleted ();
6058 }
6159
62- public NsUser getWriter () {
63- return writer ;
60+ private void validateOwner (NsUser loginUser ) throws CannotDeleteException {
61+ if (!this .isOwner (loginUser )) {
62+ throw new CannotDeleteException ("μ§λ¬Έμ μμ ν κΆνμ΄ μμ΅λλ€." );
63+ }
64+ updateDeleted ();
6465 }
6566
66- public void addAnswer ( Answer answer ) {
67- answer . toQuestion ( this );
68- answers .add ( answer );
67+ public void delete ( NsUser loginUser ) throws CannotDeleteException {
68+ validateOwner ( loginUser );
69+ answers .validateAnswerOwner ( loginUser );
6970 }
7071
71- public boolean isOwner ( NsUser loginUser ) {
72- return writer . equals ( loginUser );
73- }
72+ public List < DeleteHistory > toDeleteHistories () {
73+ List < DeleteHistory > deleteHistories = new ArrayList <>( );
74+ deleteHistories . add ( new DeleteHistory ( ContentType . QUESTION , this . id , this . writer , LocalDateTime . now ()));
7475
75- public Question setDeleted (boolean deleted ) {
76- this .deleted = deleted ;
77- return this ;
76+ List <DeleteHistory > answerDeleteHistories = addDeleteAnswerHistory ();
77+ deleteHistories .addAll (answerDeleteHistories );
78+
79+ return deleteHistories ;
7880 }
7981
80- public boolean isDeleted () {
81- return deleted ;
82+ private List < DeleteHistory > addDeleteAnswerHistory () {
83+ return this . answers . addDeleteAnswerHistory () ;
8284 }
8385
84- public List < Answer > getAnswers () {
85- return answers ;
86+ private void updateDeleted () {
87+ deleted () ;
8688 }
8789
8890 @ Override
8991 public String toString () {
9092 return "Question [id=" + getId () + ", title=" + title + ", contents=" + contents + ", writer=" + writer + "]" ;
9193 }
94+
9295}
0 commit comments