Skip to content
Merged

Step1 #687

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
40 changes: 18 additions & 22 deletions src/main/java/nextstep/qna/domain/Answer.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package nextstep.qna.domain;

import java.time.LocalDateTime;

import nextstep.qna.CannotDeleteException;
import nextstep.qna.NotFoundException;
import nextstep.qna.UnAuthorizedException;
import nextstep.users.domain.NsUser;

import java.time.LocalDateTime;

public class Answer {
private Long id;

Expand All @@ -30,11 +31,11 @@ public Answer(NsUser writer, Question question, String contents) {

public Answer(Long id, NsUser writer, Question question, String contents) {
this.id = id;
if(writer == null) {
if (writer == null) {
throw new UnAuthorizedException();
}

if(question == null) {
if (question == null) {
throw new NotFoundException();
}

Expand All @@ -43,15 +44,6 @@ public Answer(Long id, NsUser writer, Question question, String contents) {
this.contents = contents;
}

public Long getId() {
return id;
}

public Answer setDeleted(boolean deleted) {
this.deleted = deleted;
return this;
}

public boolean isDeleted() {
return deleted;
}
Expand All @@ -60,20 +52,24 @@ public boolean isOwner(NsUser writer) {
return this.writer.equals(writer);
}

public NsUser getWriter() {
return writer;
}

public String getContents() {
return contents;
}

public void toQuestion(Question question) {
this.question = question;
}

@Override
public String toString() {
return "Answer [id=" + getId() + ", writer=" + writer + ", contents=" + contents + "]";
return "Answer [id=" + id + ", writer=" + writer + ", contents=" + contents + "]";
}

public DeleteHistory delete(NsUser loginUser, LocalDateTime createdDate) throws CannotDeleteException {
validate(loginUser);
this.deleted = true;
return new DeleteHistory(ContentType.ANSWER, this.id, this.writer, createdDate);
}

private void validate(NsUser loginUser) throws CannotDeleteException {
if (!isOwner(loginUser)) {
throw new CannotDeleteException("다른 사람이 쓴 답변이 있어 삭제할 수 없습니다.");
}
}
}
24 changes: 24 additions & 0 deletions src/main/java/nextstep/qna/domain/Answers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package nextstep.qna.domain;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import nextstep.qna.CannotDeleteException;
import nextstep.users.domain.NsUser;

public class Answers {
private final List<Answer> answers = new ArrayList<>();

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

public List<DeleteHistory> delete(NsUser loginUser, LocalDateTime createdDate) throws CannotDeleteException {
final List<DeleteHistory> deleteHistories = new ArrayList<>();
for (Answer answer : this.answers) {
deleteHistories.add(answer.delete(loginUser, createdDate));
}
return deleteHistories;
}
}
57 changes: 23 additions & 34 deletions src/main/java/nextstep/qna/domain/Question.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package nextstep.qna.domain;

import nextstep.users.domain.NsUser;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import nextstep.qna.CannotDeleteException;
import nextstep.users.domain.NsUser;

public class Question {
private Long id;

Expand All @@ -15,7 +16,7 @@ public class Question {

private NsUser writer;

private List<Answer> answers = new ArrayList<>();
private Answers answers = new Answers();

private boolean deleted = false;

Expand All @@ -37,42 +38,16 @@ public Question(Long id, NsUser writer, String title, String contents) {
this.contents = contents;
}

public Long getId() {
return id;
}

public String getTitle() {
return title;
}

public Question setTitle(String title) {
this.title = title;
return this;
}

public String getContents() {
return contents;
}

public Question setContents(String contents) {
this.contents = contents;
return this;
}

public NsUser getWriter() {
return writer;
}

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

public boolean isOwner(NsUser loginUser) {
private boolean isOwner(NsUser loginUser) {
return writer.equals(loginUser);
}

public Question setDeleted(boolean deleted) {
private Question setDeleted(boolean deleted) {
this.deleted = deleted;
return this;
}
Expand All @@ -81,12 +56,26 @@ public boolean isDeleted() {
return deleted;
}

public List<Answer> getAnswers() {
return answers;
public List<DeleteHistory> delete(NsUser loginUser, LocalDateTime createdDate) throws CannotDeleteException {
validate(loginUser);

setDeleted(true);

List<DeleteHistory> deleteHistories = new ArrayList<>();
deleteHistories.add(new DeleteHistory(ContentType.QUESTION, this.id, this.writer, createdDate));
deleteHistories.addAll(answers.delete(loginUser, createdDate));

return deleteHistories;
}

private void validate(NsUser loginUser) throws CannotDeleteException {
if (!isOwner(loginUser)) {
throw new CannotDeleteException("질문을 삭제할 권한이 없습니다.");
}
}

@Override
public String toString() {
return "Question [id=" + getId() + ", title=" + title + ", contents=" + contents + ", writer=" + writer + "]";
return "Question [id=" + id + ", title=" + title + ", contents=" + contents + ", writer=" + writer + "]";
}
}
38 changes: 11 additions & 27 deletions src/main/java/nextstep/qna/service/QnAService.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package nextstep.qna.service;

import nextstep.qna.CannotDeleteException;
import nextstep.qna.NotFoundException;
import nextstep.qna.domain.*;
import nextstep.users.domain.NsUser;
import java.time.LocalDateTime;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import nextstep.qna.CannotDeleteException;
import nextstep.qna.NotFoundException;
import nextstep.qna.domain.AnswerRepository;
import nextstep.qna.domain.Question;
import nextstep.qna.domain.QuestionRepository;
import nextstep.users.domain.NsUser;

@Service("qnaService")
public class QnAService {
Expand All @@ -26,24 +28,6 @@ public class QnAService {
@Transactional
public void deleteQuestion(NsUser loginUser, long questionId) throws CannotDeleteException {
Question question = questionRepository.findById(questionId).orElseThrow(NotFoundException::new);
if (!question.isOwner(loginUser)) {
throw new CannotDeleteException("질문을 삭제할 권한이 없습니다.");
}

List<Answer> answers = question.getAnswers();
for (Answer answer : answers) {
if (!answer.isOwner(loginUser)) {
throw new CannotDeleteException("다른 사람이 쓴 답변이 있어 삭제할 수 없습니다.");
}
}

List<DeleteHistory> deleteHistories = new ArrayList<>();
question.setDeleted(true);
deleteHistories.add(new DeleteHistory(ContentType.QUESTION, questionId, question.getWriter(), LocalDateTime.now()));
for (Answer answer : answers) {
answer.setDeleted(true);
deleteHistories.add(new DeleteHistory(ContentType.ANSWER, answer.getId(), answer.getWriter(), LocalDateTime.now()));
}
deleteHistoryService.saveAll(deleteHistories);
deleteHistoryService.saveAll(question.delete(loginUser, LocalDateTime.now()));
}
}
19 changes: 19 additions & 0 deletions src/test/java/nextstep/qna/domain/AnswerTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
package nextstep.qna.domain;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

import java.time.LocalDateTime;

import org.junit.jupiter.api.Test;

import nextstep.qna.CannotDeleteException;
import nextstep.users.domain.NsUserTest;

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 delete() throws CannotDeleteException {
final LocalDateTime createdDate = LocalDateTime.now();
assertThat(A1.delete(NsUserTest.JAVAJIGI, createdDate)).isEqualTo(new DeleteHistory(ContentType.ANSWER, null, NsUserTest.JAVAJIGI, createdDate));
}

@Test
void 다른_사람이_쓴_답변이_있어_삭제할_수_없습니다() {
assertThatExceptionOfType(CannotDeleteException.class).isThrownBy(() -> A1.delete(NsUserTest.SANJIGI, LocalDateTime.now()));
}
}
23 changes: 23 additions & 0 deletions src/test/java/nextstep/qna/domain/QuestionTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
package nextstep.qna.domain;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

import java.time.LocalDateTime;
import java.util.List;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import nextstep.qna.CannotDeleteException;
import nextstep.users.domain.NsUserTest;

@ExtendWith(MockitoExtension.class)
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");

@Test
void delete() throws CannotDeleteException {
final LocalDateTime createdDate = LocalDateTime.now();
assertThat(Q1.delete(NsUserTest.JAVAJIGI, createdDate)).isEqualTo(List.of(new DeleteHistory(ContentType.QUESTION, 0L, NsUserTest.JAVAJIGI, createdDate)));
}

@Test
void 다른_사람이_쓴_질문이_있어_삭제할_수_없습니다() {
assertThatExceptionOfType(CannotDeleteException.class).isThrownBy(() -> Q1.delete(NsUserTest.SANJIGI, LocalDateTime.now()));
}
}
Loading