Skip to content

Commit

Permalink
refactor: 1단계 - 질문 연관 답변 삭제 코드 Question 내부로 이동
Browse files Browse the repository at this point in the history
  • Loading branch information
mingulee-devel committed Oct 24, 2024
1 parent 4affb6a commit 2b2c602
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ Question
- [x] 질문 삭제 권한없음 확인
- [x] 작성자가 본인이 아닌 경우
- [x] answer에 다른 사람 답변이 있는 경우
- [ ] 질문 삭제 및 삭제 이력 남기기
- [ ] 질문 삭제
- [ ] 모든 답변 삭제
- [x] 질문 삭제 및 삭제 이력 남기기
- [x] 질문 삭제
- [x] 모든 답변 삭제

Answer
- [x] 작성자 본인인지 확인
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/nextstep/qna/domain/Question.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,20 @@ public boolean isDeleted() {
return deleted;
}

public List<Answer> getAnswers() {
return answers;
public List<DeleteHistory> delete() {
List<DeleteHistory> deleteHistories = new ArrayList<>();
this.setDeleted(true);
deleteHistories.add(new DeleteHistory(ContentType.QUESTION, this.id, this.writer, LocalDateTime.now()));
deleteHistories.addAll(deleteAnswers());
return deleteHistories;
}

public DeleteHistory delete() {
this.setDeleted(true);
return new DeleteHistory(ContentType.QUESTION, this.id, this.writer, LocalDateTime.now());
private List<DeleteHistory> deleteAnswers() {
List<DeleteHistory> deleteHistories = new ArrayList<>();
for (Answer answer : answers) {
deleteHistories.add(answer.delete());
}
return deleteHistories;
}

@Override
Expand Down
8 changes: 1 addition & 7 deletions src/main/java/nextstep/qna/service/QnAService.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,7 @@ public void deleteQuestion(NsUser loginUser, long questionId) throws CannotDelet
Question question = questionRepository.findById(questionId).orElseThrow(NotFoundException::new);
question.checkDeletePermission(loginUser);

List<Answer> answers = question.getAnswers();

List<DeleteHistory> deleteHistories = new ArrayList<>();
deleteHistories.add(question.delete());
for (Answer answer : answers) {
deleteHistories.add(answer.delete());
}
List<DeleteHistory> deleteHistories = question.delete();
deleteHistoryService.saveAll(deleteHistories);
}
}

0 comments on commit 2b2c602

Please sign in to comment.