Skip to content

Commit

Permalink
Merge pull request #284 from choomi1217/step1
Browse files Browse the repository at this point in the history
🚀 1단계 - 레거시 코드 리팩터링
  • Loading branch information
shared-moon authored Dec 1, 2023
2 parents de587b9 + 4d6473d commit 24227ae
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 27 deletions.
8 changes: 8 additions & 0 deletions src/main/java/nextstep/qna/domain/Answer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package nextstep.qna.domain;

import nextstep.qna.CannotDeleteException;
import nextstep.qna.NotFoundException;
import nextstep.qna.UnAuthorizedException;
import nextstep.users.domain.NsUser;
Expand Down Expand Up @@ -76,4 +77,11 @@ public void toQuestion(Question question) {
public String toString() {
return "Answer [id=" + getId() + ", writer=" + writer + ", contents=" + contents + "]";
}

public Answer delete(NsUser writer) throws CannotDeleteException {
if (!this.isOwner(writer)) {
throw new CannotDeleteException("다른 사람이 쓴 답변이 있어 삭제할 수 없습니다.");
}
return this.setDeleted(true);
}
}
8 changes: 8 additions & 0 deletions src/main/java/nextstep/qna/domain/Question.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package nextstep.qna.domain;

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

import java.time.LocalDateTime;
Expand Down Expand Up @@ -89,4 +90,11 @@ public List<Answer> getAnswers() {
public String toString() {
return "Question [id=" + getId() + ", title=" + title + ", contents=" + contents + ", writer=" + writer + "]";
}

public Question delete(NsUser writer) throws CannotDeleteException {
if (!this.isOwner(writer)) {
throw new CannotDeleteException("질문을 삭제할 권한이 없습니다.");
}
return this.setDeleted(true);
}
}
18 changes: 18 additions & 0 deletions src/main/java/nextstep/qna/service/DeleteHistoryService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package nextstep.qna.service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import nextstep.qna.domain.Answer;
import nextstep.qna.domain.ContentType;
import nextstep.qna.domain.DeleteHistory;
import nextstep.qna.domain.DeleteHistoryRepository;
import nextstep.qna.domain.Question;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -18,4 +23,17 @@ public class DeleteHistoryService {
public void saveAll(List<DeleteHistory> deleteHistories) {
deleteHistoryRepository.saveAll(deleteHistories);
}

public void saveAll(Question question, List<Answer> answers) {
List<DeleteHistory> deleteHistories = new ArrayList<>();
deleteHistories.add(
new DeleteHistory(ContentType.QUESTION, question.getId(), question.getWriter(),
LocalDateTime.now()));
for (Answer answer : answers) {
deleteHistories.add(
new DeleteHistory(ContentType.ANSWER, answer.getId(), answer.getWriter(),
LocalDateTime.now()));
}
this.saveAll(deleteHistories);
}
}
45 changes: 20 additions & 25 deletions src/main/java/nextstep/qna/service/QnAService.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package nextstep.qna.service;

import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import nextstep.qna.CannotDeleteException;
import nextstep.qna.NotFoundException;
import nextstep.qna.domain.*;
import nextstep.qna.domain.Answer;
import nextstep.qna.domain.AnswerRepository;
import nextstep.qna.domain.Question;
import nextstep.qna.domain.QuestionRepository;
import nextstep.users.domain.NsUser;
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;

@Service("qnaService")
public class QnAService {

@Resource(name = "questionRepository")
private QuestionRepository questionRepository;

Expand All @@ -25,25 +27,18 @@ 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("다른 사람이 쓴 답변이 있어 삭제할 수 없습니다.");
Question deletedQuestion = questionRepository.findById(questionId)
.orElseThrow(NotFoundException::new)
.delete(loginUser);

List<Answer> deletedAnswers = deletedQuestion.getAnswers().stream().map(answer -> {
try {
return answer.delete(loginUser);
} catch (CannotDeleteException e) {
throw new RuntimeException(e);
}
}

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);
}).collect(Collectors.toList());

deleteHistoryService.saveAll(deletedQuestion, deletedAnswers);
}
}
33 changes: 31 additions & 2 deletions src/test/java/nextstep/qna/domain/AnswerTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,37 @@
package nextstep.qna.domain;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import nextstep.qna.CannotDeleteException;
import nextstep.users.domain.NsUser;
import nextstep.users.domain.NsUserTest;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

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");

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");

@DisplayName("답변 등록자와 로그인 유저가 일치할 땐 예외를 던지지 않습니다.")
@Test
void doesNotTrowExceptionWhenDeleteLoginUserMatchedWithAnswerWriter() {
NsUser writer = A1.getWriter();
assertThatNoException().isThrownBy(() -> A1.delete(writer));
assertThat(A1.isDeleted()).isTrue();
}

@DisplayName("답변 등록자와 로그인 유저가 일치하지 않은데 삭제하려고 하면 예외를 던집니다.")
@Test
void throwExceptionWhenDeleteLoginUserDoesNotMatchedWithAnswerWriter() {
NsUser writer = A1.getWriter();
assertThatThrownBy(() -> A2.delete(writer)).isInstanceOf(CannotDeleteException.class);
assertThat(A2.isDeleted()).isFalse();
}


}
27 changes: 27 additions & 0 deletions src/test/java/nextstep/qna/domain/QuestionTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
package nextstep.qna.domain;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import nextstep.qna.CannotDeleteException;
import nextstep.users.domain.NsUser;
import nextstep.users.domain.NsUserTest;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

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");

@DisplayName("삭제할 권한이 있다면 질문이 삭제됩니다.")
@Test
void canDeleteIfYouHavePermission() throws CannotDeleteException {
NsUser writer = Q1.getWriter();
assertThatNoException().isThrownBy(() -> Q1.delete(writer));
assertThat(Q1.isDeleted()).isTrue();
}

@DisplayName("삭제할 권한이 없다면 질문을 삭제할 수 없습니다.")
@Test
void canNotDeleteIfYouDontHavePermission() throws CannotDeleteException {
NsUser writer = Q1.getWriter();
assertThatThrownBy(() -> Q2.delete(writer)).isInstanceOf(CannotDeleteException.class);
assertThat(Q2.isDeleted()).isFalse();
}

}

0 comments on commit 24227ae

Please sign in to comment.