diff --git a/src/main/java/qna/domain/Answers.java b/src/main/java/qna/domain/Answers.java new file mode 100644 index 0000000..7cbfbf4 --- /dev/null +++ b/src/main/java/qna/domain/Answers.java @@ -0,0 +1,30 @@ +package qna.domain; + +import qna.CannotDeleteException; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class Answers { + private final List answers = new ArrayList<>(); + + public void add(Answer answer) { + answers.add(answer); + } + + public List getAnswers() { + return Collections.unmodifiableList(this.answers); + } + + public boolean validateItHasOtherAnswer(User loginUser) throws CannotDeleteException { + for (Answer answer : this.answers) { + + if (!answer.isOwner(loginUser)) { + throw new CannotDeleteException("다른 사람이 쓴 답변이 있어 삭제할 수 없습니다."); + } + } + + return true; + } +} diff --git a/src/main/java/qna/domain/Question.java b/src/main/java/qna/domain/Question.java index 1e8bb11..9c24f42 100644 --- a/src/main/java/qna/domain/Question.java +++ b/src/main/java/qna/domain/Question.java @@ -1,6 +1,7 @@ package qna.domain; import org.hibernate.annotations.Where; +import qna.CannotDeleteException; import javax.persistence.*; import java.util.ArrayList; @@ -21,7 +22,7 @@ public class Question extends AbstractEntity { @OneToMany(mappedBy = "question", cascade = CascadeType.ALL) @Where(clause = "deleted = false") @OrderBy("id ASC") - private List answers = new ArrayList<>(); + private Answers answers = new Answers(); private boolean deleted = false; @@ -84,12 +85,20 @@ public boolean isDeleted() { return deleted; } - public List getAnswers() { - return answers; + public Answers getAnswers() { + return this.answers; } @Override public String toString() { return "Question [id=" + getId() + ", title=" + title + ", contents=" + contents + ", writer=" + writer + "]"; } + + public boolean validateItIsOtherUser(User loginUser) throws CannotDeleteException { + if (!this.isOwner(loginUser)) { + throw new CannotDeleteException("질문을 삭제할 권한이 없습니다."); + } + + return true; + } } diff --git a/src/main/java/qna/service/QnAService.java b/src/main/java/qna/service/QnAService.java index 66821cd..b5842d6 100644 --- a/src/main/java/qna/service/QnAService.java +++ b/src/main/java/qna/service/QnAService.java @@ -35,21 +35,15 @@ public Question findQuestionById(Long id) { @Transactional public void deleteQuestion(User loginUser, long questionId) throws CannotDeleteException { Question question = findQuestionById(questionId); - if (!question.isOwner(loginUser)) { - throw new CannotDeleteException("질문을 삭제할 권한이 없습니다."); - } + question.validateItIsOtherUser(loginUser); - List answers = question.getAnswers(); - for (Answer answer : answers) { - if (!answer.isOwner(loginUser)) { - throw new CannotDeleteException("다른 사람이 쓴 답변이 있어 삭제할 수 없습니다."); - } - } + Answers answers = question.getAnswers(); + answers.validateItHasOtherAnswer(loginUser); List deleteHistories = new ArrayList<>(); question.setDeleted(true); deleteHistories.add(new DeleteHistory(ContentType.QUESTION, questionId, question.getWriter(), LocalDateTime.now())); - for (Answer answer : answers) { + for (Answer answer : answers.getAnswers()) { answer.setDeleted(true); deleteHistories.add(new DeleteHistory(ContentType.ANSWER, answer.getId(), answer.getWriter(), LocalDateTime.now())); } diff --git a/src/test/java/qna/domain/AnswersTest.java b/src/test/java/qna/domain/AnswersTest.java new file mode 100644 index 0000000..a5ea583 --- /dev/null +++ b/src/test/java/qna/domain/AnswersTest.java @@ -0,0 +1,35 @@ +package qna.domain; + +import org.junit.jupiter.api.Test; +import qna.CannotDeleteException; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class AnswersTest { + @Test + void validateItHasOtherAnswer_invalid() throws CannotDeleteException { + Answers answers = new Answers(); + + User otherUser = new User(11L, "applepickker", "abc112", "cho", "apple11@naver.com"); + Answer answer = new Answer(otherUser, new Question(), "contents"); + answers.add(answer); + + User loginUser = new User(12L, "dal96k", "wjsdnals96", "woomin", "dal96k@hanmail.net"); + + assertThatThrownBy(() -> + answers.validateItHasOtherAnswer(loginUser)) + .isInstanceOf(CannotDeleteException.class); + } + + @Test + void validateItHasOtherAnswer_valid() throws CannotDeleteException { + Answers answers = new Answers(); + + User loginUser = new User(11L, "dal96k", "wjsdnals96", "woomin", "dal96k@hanmail.net"); + Answer answer = new Answer(loginUser, new Question(), "contents"); + answers.add(answer); + + assertThat(answers.validateItHasOtherAnswer(loginUser)).isEqualTo(true); + } +} diff --git a/src/test/java/qna/domain/QuestionTest.java b/src/test/java/qna/domain/QuestionTest.java index b48c9a2..9bda609 100644 --- a/src/test/java/qna/domain/QuestionTest.java +++ b/src/test/java/qna/domain/QuestionTest.java @@ -1,6 +1,35 @@ package qna.domain; +import org.junit.jupiter.api.Test; +import qna.CannotDeleteException; + +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("title1", "contents1").writeBy(UserTest.JAVAJIGI); public static final Question Q2 = new Question("title2", "contents2").writeBy(UserTest.SANJIGI); + + @Test + void validateItIsOtherUser_invalid() { + Question question = new Question(11L, "wonder", "this contents"); + User otherUser = new User(11L, "applepickker", "abc112", "cho", "apple11@naver.com"); + question.writeBy(otherUser); + + User loginUser = new User(12L, "dal96k", "wjsdnals96", "woomin", "dal96k@hanmail.net"); + + assertThatThrownBy(() -> + question.validateItIsOtherUser(loginUser)) + .isInstanceOf(CannotDeleteException.class); + } + + @Test + void validateItIsOtherUser_valid() throws CannotDeleteException { + Question question = new Question(12, "wonder", "this contents"); + + User loginUser = new User(12L, "dal96k", "wjsdnals96", "woomin", "dal96k@hanmail.net"); + question.writeBy(loginUser); + + assertThat(question.validateItIsOtherUser(loginUser)).isEqualTo(true); + } }