Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/main/java/qna/domain/Answers.java
Original file line number Diff line number Diff line change
@@ -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<Answer> answers = new ArrayList<>();

public void add(Answer answer) {
answers.add(answer);
}

public List<Answer> 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;
}
}
15 changes: 12 additions & 3 deletions src/main/java/qna/domain/Question.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package qna.domain;

import org.hibernate.annotations.Where;
import qna.CannotDeleteException;

import javax.persistence.*;
import java.util.ArrayList;
Expand All @@ -21,7 +22,7 @@ public class Question extends AbstractEntity {
@OneToMany(mappedBy = "question", cascade = CascadeType.ALL)
@Where(clause = "deleted = false")
@OrderBy("id ASC")
private List<Answer> answers = new ArrayList<>();
private Answers answers = new Answers();

private boolean deleted = false;

Expand Down Expand Up @@ -84,12 +85,20 @@ public boolean isDeleted() {
return deleted;
}

public List<Answer> 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;
}
}
14 changes: 4 additions & 10 deletions src/main/java/qna/service/QnAService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Answer> answers = question.getAnswers();
for (Answer answer : answers) {
if (!answer.isOwner(loginUser)) {
throw new CannotDeleteException("다른 사람이 쓴 답변이 있어 삭제할 수 없습니다.");
}
}
Answers answers = question.getAnswers();
answers.validateItHasOtherAnswer(loginUser);

List<DeleteHistory> 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()));
}
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/qna/domain/AnswersTest.java
Original file line number Diff line number Diff line change
@@ -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", "[email protected]");
Answer answer = new Answer(otherUser, new Question(), "contents");
answers.add(answer);

User loginUser = new User(12L, "dal96k", "wjsdnals96", "woomin", "[email protected]");

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", "[email protected]");
Answer answer = new Answer(loginUser, new Question(), "contents");
answers.add(answer);

assertThat(answers.validateItHasOtherAnswer(loginUser)).isEqualTo(true);
}
}
29 changes: 29 additions & 0 deletions src/test/java/qna/domain/QuestionTest.java
Original file line number Diff line number Diff line change
@@ -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", "[email protected]");
question.writeBy(otherUser);

User loginUser = new User(12L, "dal96k", "wjsdnals96", "woomin", "[email protected]");

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", "[email protected]");
question.writeBy(loginUser);

assertThat(question.validateItIsOtherUser(loginUser)).isEqualTo(true);
}
}