-
Notifications
You must be signed in to change notification settings - Fork 299
[수강신청] 1단계 - 레거시 코드 리팩터링 #666
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+238
−31
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a29c2d5
refactor deleteQuestion 의 비즈니스 로직을 Question 객체로 이전
kakao-luka-redwing caa43b3
refactor 삭제 조건을 interface 로 빼고, 목적에 따라 delete 함수 분리
kakao-luka-redwing ce052a0
test QuestionDeletableAnswerTest
kakao-luka-redwing 60b0713
test QuestionDeletableWriterTest 추가
luku756 ed6e18e
refactor answer, question 함수 수정 및 테스트 추가
luku756 8c3d8c2
refactor delete 함수 수정
kakao-luka-redwing 797fbc1
refactor QuestionDeletable 에서 예외를 던지게 수정
kakao-luka-redwing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,20 @@ | ||
package nextstep.qna.domain; | ||
|
||
import nextstep.qna.CannotDeleteException; | ||
import nextstep.qna.domain.qustion.QuestionDeletable; | ||
import nextstep.qna.domain.qustion.QuestionDeletableAnswer; | ||
import nextstep.qna.domain.qustion.QuestionDeletableWriter; | ||
import nextstep.users.domain.NsUser; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class Question { | ||
|
||
private static final List<QuestionDeletable> deletableConditions = List.of( | ||
new QuestionDeletableAnswer(), new QuestionDeletableWriter()); | ||
|
||
private Long id; | ||
|
||
private String title; | ||
|
@@ -68,13 +76,40 @@ public void addAnswer(Answer answer) { | |
answers.add(answer); | ||
} | ||
|
||
public List<DeleteHistory> delete(NsUser loginUser) throws CannotDeleteException { | ||
checkDeletable(loginUser); | ||
|
||
List<DeleteHistory> deleteHistories = new ArrayList<>(); | ||
deleted = true; | ||
deleteHistories.add( | ||
new DeleteHistory(ContentType.QUESTION, id, writer, LocalDateTime.now())); | ||
for (Answer answer : answers) { | ||
deleteHistories.add(answer.delete()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
} | ||
return deleteHistories; | ||
} | ||
|
||
private void checkDeletable(NsUser loginUser) throws CannotDeleteException { | ||
for (QuestionDeletable condition : deletableConditions) { | ||
condition.checkDeletable(this, loginUser); | ||
} | ||
} | ||
|
||
public boolean isOwner(NsUser loginUser) { | ||
return writer.equals(loginUser); | ||
} | ||
|
||
public Question setDeleted(boolean deleted) { | ||
this.deleted = deleted; | ||
return this; | ||
public boolean answerEmpty() { | ||
return answers.isEmpty(); | ||
} | ||
|
||
public boolean allAnswerFromWriter() { | ||
for (Answer answer : answers) { | ||
if (answer.isNotOwner(writer)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public boolean isDeleted() { | ||
|
@@ -87,6 +122,7 @@ public List<Answer> getAnswers() { | |
|
||
@Override | ||
public String toString() { | ||
return "Question [id=" + getId() + ", title=" + title + ", contents=" + contents + ", writer=" + writer + "]"; | ||
return "Question [id=" + getId() + ", title=" + title + ", contents=" + contents | ||
+ ", writer=" + writer + "]"; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/nextstep/qna/domain/qustion/QuestionDeletable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package nextstep.qna.domain.qustion; | ||
|
||
import nextstep.qna.CannotDeleteException; | ||
import nextstep.qna.domain.Question; | ||
import nextstep.users.domain.NsUser; | ||
|
||
public interface QuestionDeletable { | ||
void checkDeletable(Question question, NsUser loginUser) throws CannotDeleteException; | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/nextstep/qna/domain/qustion/QuestionDeletableAnswer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package nextstep.qna.domain.qustion; | ||
|
||
import nextstep.qna.CannotDeleteException; | ||
import nextstep.qna.domain.Question; | ||
import nextstep.users.domain.NsUser; | ||
|
||
public class QuestionDeletableAnswer implements QuestionDeletable { | ||
|
||
static final String REASON = "다른 사람이 쓴 답변이 있어 삭제할 수 없습니다."; | ||
|
||
@Override | ||
public void checkDeletable(Question question, NsUser loginUser) throws CannotDeleteException { | ||
if (question.answerEmpty() || question.allAnswerFromWriter()) { | ||
return; | ||
} | ||
throw new CannotDeleteException(REASON); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/nextstep/qna/domain/qustion/QuestionDeletableWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package nextstep.qna.domain.qustion; | ||
|
||
import nextstep.qna.CannotDeleteException; | ||
import nextstep.qna.domain.Question; | ||
import nextstep.users.domain.NsUser; | ||
|
||
public class QuestionDeletableWriter implements QuestionDeletable { | ||
|
||
static final String REASON = "질문을 삭제할 권한이 없습니다."; | ||
|
||
@Override | ||
public void checkDeletable(Question question, NsUser loginUser) throws CannotDeleteException { | ||
if (question.isOwner(loginUser)) { | ||
return; | ||
} | ||
throw new CannotDeleteException(REASON); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,23 @@ | ||
package nextstep.qna.domain; | ||
|
||
import java.time.LocalDateTime; | ||
import nextstep.users.domain.NsUserTest; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class AnswerTest { | ||
public static final Answer A1 = new Answer(NsUserTest.JAVAJIGI, QuestionTest.Q1, "Answers Contents1"); | ||
public static final Answer A2 = new Answer(NsUserTest.SANJIGI, QuestionTest.Q1, "Answers Contents2"); | ||
|
||
@Test | ||
void 삭제() { | ||
Answer answer = new Answer(11L, NsUserTest.JAVAJIGI, QuestionTest.Q1, "Answers Contents1"); | ||
assertThat(answer.isDeleted()).isFalse(); | ||
|
||
DeleteHistory history = answer.delete(); | ||
assertThat(answer.isDeleted()).isTrue(); | ||
assertThat(history).isEqualTo( | ||
new DeleteHistory(ContentType.ANSWER, answer.getId(), answer.getWriter(), LocalDateTime.now())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,63 @@ | ||
package nextstep.qna.domain; | ||
|
||
import nextstep.qna.CannotDeleteException; | ||
import nextstep.users.domain.NsUserTest; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
public class QuestionTest { | ||
public static final Question Q1 = new Question(NsUserTest.JAVAJIGI, "title1", "contents1"); | ||
public static final Question Q2 = new Question(NsUserTest.SANJIGI, "title2", "contents2"); | ||
|
||
|
||
private Question question; | ||
private Answer answer; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
question = new Question(1L, NsUserTest.JAVAJIGI, "title1", "contents1"); | ||
answer = new Answer(11L, NsUserTest.JAVAJIGI, QuestionTest.Q1, "Answers Contents1"); | ||
question.addAnswer(answer); | ||
} | ||
|
||
@Test | ||
public void delete_성공() throws Exception { | ||
assertThat(question.isDeleted()).isFalse(); | ||
List<DeleteHistory> deleteHistories = question.delete(NsUserTest.JAVAJIGI); | ||
|
||
assertThat(question.isDeleted()).isTrue(); | ||
assertThat(deleteHistories).isEqualTo(Arrays.asList( | ||
new DeleteHistory(ContentType.QUESTION, question.getId(), question.getWriter(), LocalDateTime.now()), | ||
new DeleteHistory(ContentType.ANSWER, answer.getId(), answer.getWriter(), LocalDateTime.now()))); | ||
} | ||
|
||
@Test | ||
public void delete_다른_사람이_쓴_글() { | ||
assertThatThrownBy(() -> question.delete(NsUserTest.SANJIGI)).isInstanceOf(CannotDeleteException.class); | ||
} | ||
|
||
@Test | ||
public void delete_성공_질문자_답변자_같음() throws Exception { | ||
List<DeleteHistory> deleteHistories = question.delete(NsUserTest.JAVAJIGI); | ||
|
||
assertThat(question.isDeleted()).isTrue(); | ||
assertThat(answer.isDeleted()).isTrue(); | ||
|
||
assertThat(deleteHistories).isEqualTo(Arrays.asList( | ||
new DeleteHistory(ContentType.QUESTION, question.getId(), question.getWriter(), LocalDateTime.now()), | ||
new DeleteHistory(ContentType.ANSWER, answer.getId(), answer.getWriter(), LocalDateTime.now()))); | ||
} | ||
|
||
@Test | ||
public void delete_답변_중_다른_사람이_쓴_글() { | ||
assertThatThrownBy(() -> question.delete(NsUserTest.SANJIGI)).isInstanceOf(CannotDeleteException.class); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
src/test/java/nextstep/qna/domain/question/QuestionDeletableAnswerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package nextstep.qna.domain.question; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatNoException; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
import nextstep.qna.CannotDeleteException; | ||
import nextstep.qna.domain.AnswerTest; | ||
import nextstep.qna.domain.Question; | ||
import nextstep.qna.domain.qustion.QuestionDeletable; | ||
import nextstep.qna.domain.qustion.QuestionDeletableAnswer; | ||
import nextstep.users.domain.NsUserTest; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class QuestionDeletableAnswerTest { | ||
|
||
@Test | ||
void 답변없음_삭제가능() { | ||
Question question = new Question(NsUserTest.JAVAJIGI, "title1", "contents1"); | ||
QuestionDeletable questionDeletable = new QuestionDeletableAnswer(); | ||
|
||
assertThatNoException().isThrownBy(() -> questionDeletable.checkDeletable(question, question.getWriter())); | ||
} | ||
|
||
@Test | ||
void 작성자와_같은답변_삭제가능() { | ||
Question question = new Question(NsUserTest.JAVAJIGI, "title1", "contents1"); | ||
QuestionDeletable questionDeletable = new QuestionDeletableAnswer(); | ||
question.addAnswer(AnswerTest.A1); | ||
|
||
assertThatNoException().isThrownBy(() -> questionDeletable.checkDeletable(question, question.getWriter())); | ||
} | ||
|
||
@Test | ||
void 작성자와_다른답변_삭제불가() { | ||
Question question = new Question(NsUserTest.JAVAJIGI, "title1", "contents1"); | ||
QuestionDeletable questionDeletable = new QuestionDeletableAnswer(); | ||
question.addAnswer(AnswerTest.A2); | ||
|
||
assertThatThrownBy(() -> questionDeletable.checkDeletable(question, question.getWriter())).isInstanceOf( | ||
CannotDeleteException.class); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/test/java/nextstep/qna/domain/question/QuestionDeletableWriterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package nextstep.qna.domain.question; | ||
|
||
import nextstep.qna.CannotDeleteException; | ||
import nextstep.qna.domain.Question; | ||
import nextstep.qna.domain.qustion.QuestionDeletable; | ||
import nextstep.qna.domain.qustion.QuestionDeletableWriter; | ||
import nextstep.users.domain.NsUserTest; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatNoException; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
class QuestionDeletableWriterTest { | ||
|
||
@Test | ||
void 작성자라_삭제가능() { | ||
Question question = new Question(NsUserTest.JAVAJIGI, "title1", "contents1"); | ||
QuestionDeletable questionDeletable = new QuestionDeletableWriter(); | ||
|
||
assertThatNoException().isThrownBy(() -> questionDeletable.checkDeletable(question, NsUserTest.JAVAJIGI)); | ||
} | ||
|
||
@Test | ||
void 작성자가_아니라_삭제불가() { | ||
Question question = new Question(NsUserTest.JAVAJIGI, "title1", "contents1"); | ||
QuestionDeletable questionDeletable = new QuestionDeletableWriter(); | ||
|
||
assertThatThrownBy(() -> questionDeletable.checkDeletable(question, NsUserTest.SANJIGI)).isInstanceOf( | ||
CannotDeleteException.class); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍