Skip to content

Commit 290d7ac

Browse files
authored
Step1 - 레거시 코드 리팩터링 (#804)
* refactor: quest에 delete 메시지 보내기 * refactor: answer 리스트를 answers 에 포장하기 (일급 컬렉션) * refactor: delete 처리 전에 유효한지 체크 * refactor: 공통된 부분 처리 * refactor: 삭제 마크 하는 부분과 history 만드는 부분 분리 * refactor: 피드백 반영
1 parent de587b9 commit 290d7ac

File tree

9 files changed

+234
-102
lines changed

9 files changed

+234
-102
lines changed

src/main/java/nextstep/qna/domain/Answer.java

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
11
package nextstep.qna.domain;
22

3+
import nextstep.qna.CannotDeleteException;
34
import nextstep.qna.NotFoundException;
45
import nextstep.qna.UnAuthorizedException;
56
import nextstep.users.domain.NsUser;
67

78
import java.time.LocalDateTime;
89

9-
public class Answer {
10-
private Long id;
11-
12-
private NsUser writer;
13-
10+
public class Answer extends BaseEntity {
1411
private Question question;
15-
12+
private NsUser writer;
1613
private String contents;
1714

18-
private boolean deleted = false;
19-
20-
private LocalDateTime createdDate = LocalDateTime.now();
21-
22-
private LocalDateTime updatedDate;
23-
2415
public Answer() {
2516
}
2617

@@ -29,7 +20,7 @@ public Answer(NsUser writer, Question question, String contents) {
2920
}
3021

3122
public Answer(Long id, NsUser writer, Question question, String contents) {
32-
this.id = id;
23+
super(id);
3324
if(writer == null) {
3425
throw new UnAuthorizedException();
3526
}
@@ -43,35 +34,32 @@ public Answer(Long id, NsUser writer, Question question, String contents) {
4334
this.contents = contents;
4435
}
4536

46-
public Long getId() {
47-
return id;
48-
}
49-
50-
public Answer setDeleted(boolean deleted) {
51-
this.deleted = deleted;
52-
return this;
53-
}
54-
55-
public boolean isDeleted() {
56-
return deleted;
37+
public void delete(NsUser loginUser) throws CannotDeleteException {
38+
validateDeletableBy(loginUser);
39+
markAsDeleted();
5740
}
5841

5942
public boolean isOwner(NsUser writer) {
6043
return this.writer.equals(writer);
6144
}
6245

6346
public NsUser getWriter() {
64-
return writer;
65-
}
66-
67-
public String getContents() {
68-
return contents;
47+
return this.writer;
6948
}
7049

7150
public void toQuestion(Question question) {
7251
this.question = question;
7352
}
7453

54+
private void validateDeletableBy(NsUser loginUser) throws CannotDeleteException {
55+
if (!isOwner(loginUser)) {
56+
throw new CannotDeleteException("다른 사람이 쓴 답변이 있어 삭제할 수 없습니다.");
57+
}
58+
}
59+
public DeleteHistory createDeleteHistory() {
60+
return new DeleteHistory(ContentType.ANSWER, getId(), getWriter(), LocalDateTime.now());
61+
}
62+
7563
@Override
7664
public String toString() {
7765
return "Answer [id=" + getId() + ", writer=" + writer + ", contents=" + contents + "]";
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package nextstep.qna.domain;
2+
3+
import nextstep.qna.CannotDeleteException;
4+
import nextstep.users.domain.NsUser;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class Answers {
10+
private final List<Answer> answers;
11+
12+
public Answers() {
13+
this(new ArrayList<>());
14+
}
15+
16+
public Answers(Answer answer) {
17+
this(List.of(answer));
18+
}
19+
20+
public Answers(List<Answer> answers) {
21+
this.answers = answers;
22+
}
23+
24+
25+
public List<Answer> getAnswers() {
26+
return this.answers;
27+
}
28+
29+
public void add(Answer answer) {
30+
this.answers.add(answer);
31+
}
32+
33+
public void delete(NsUser loginUser) throws CannotDeleteException {
34+
for (Answer answer : answers) {
35+
answer.delete(loginUser);
36+
}
37+
}
38+
39+
public List<DeleteHistory> createDeleteHistories() {
40+
List<DeleteHistory> deleteHistories = new ArrayList<>();
41+
42+
for (Answer answer : answers) {
43+
deleteHistories.add(answer.createDeleteHistory());
44+
}
45+
46+
return deleteHistories;
47+
}
48+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package nextstep.qna.domain;
2+
3+
import java.time.LocalDateTime;
4+
5+
public abstract class BaseEntity {
6+
private Long id;
7+
8+
private LocalDateTime createdDate = LocalDateTime.now();
9+
10+
private LocalDateTime updatedDate;
11+
12+
private boolean deleted = false;
13+
14+
public BaseEntity() {}
15+
16+
public BaseEntity(Long id) {
17+
this.id = id;
18+
}
19+
20+
public Long getId() {
21+
return id;
22+
}
23+
24+
protected void markAsDeleted() {
25+
this.deleted = true;
26+
}
27+
28+
public boolean isDeleted() {
29+
return deleted;
30+
}
31+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package nextstep.qna.domain;
2+
3+
import nextstep.users.domain.NsUser;
4+
5+
public class PostContent {
6+
private final NsUser writer;
7+
private final String title;
8+
private final String contents;
9+
10+
public PostContent(NsUser writer, String title, String contents) {
11+
this.writer = writer;
12+
this.title = title;
13+
this.contents = contents;
14+
}
15+
16+
public NsUser getWriter() {
17+
return this.writer;
18+
}
19+
20+
public boolean isOwner(NsUser loginUser) {
21+
return writer.equals(loginUser);
22+
}
23+
}
Lines changed: 40 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,34 @@
11
package nextstep.qna.domain;
22

3+
import nextstep.qna.CannotDeleteException;
34
import nextstep.users.domain.NsUser;
45

56
import java.time.LocalDateTime;
67
import java.util.ArrayList;
78
import 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
}

src/main/java/nextstep/qna/service/QnAService.java

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import org.springframework.transaction.annotation.Transactional;
99

1010
import javax.annotation.Resource;
11-
import java.time.LocalDateTime;
12-
import java.util.ArrayList;
1311
import java.util.List;
1412

1513
@Service("qnaService")
@@ -26,24 +24,9 @@ public class QnAService {
2624
@Transactional
2725
public void deleteQuestion(NsUser loginUser, long questionId) throws CannotDeleteException {
2826
Question question = questionRepository.findById(questionId).orElseThrow(NotFoundException::new);
29-
if (!question.isOwner(loginUser)) {
30-
throw new CannotDeleteException("질문을 삭제할 권한이 없습니다.");
31-
}
32-
33-
List<Answer> answers = question.getAnswers();
34-
for (Answer answer : answers) {
35-
if (!answer.isOwner(loginUser)) {
36-
throw new CannotDeleteException("다른 사람이 쓴 답변이 있어 삭제할 수 없습니다.");
37-
}
38-
}
39-
40-
List<DeleteHistory> deleteHistories = new ArrayList<>();
41-
question.setDeleted(true);
42-
deleteHistories.add(new DeleteHistory(ContentType.QUESTION, questionId, question.getWriter(), LocalDateTime.now()));
43-
for (Answer answer : answers) {
44-
answer.setDeleted(true);
45-
deleteHistories.add(new DeleteHistory(ContentType.ANSWER, answer.getId(), answer.getWriter(), LocalDateTime.now()));
46-
}
47-
deleteHistoryService.saveAll(deleteHistories);
27+
28+
question.delete(loginUser);
29+
30+
deleteHistoryService.saveAll(question.createDeleteHistories());
4831
}
49-
}
32+
}

0 commit comments

Comments
 (0)