From dce75fb37c1d5481310dd42b3e0c0bce6d677fbe Mon Sep 17 00:00:00 2001 From: sunghyuki Date: Sun, 29 May 2022 14:56:25 +0900 Subject: [PATCH 1/8] =?UTF-8?q?=20test=20:=20=EC=9A=94=EA=B5=AC=EC=82=AC?= =?UTF-8?q?=ED=95=AD=20=EB=B3=80=EA=B2=BD=EC=97=90=20=EB=94=B0=EB=A5=B8=20?= =?UTF-8?q?=EC=A7=88=EB=AC=B8=20=EC=82=AD=EC=A0=9C=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=9D=B8=EC=88=98=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/ApiQuestionAcceptanceTest.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/test/java/codesquad/web/ApiQuestionAcceptanceTest.java b/src/test/java/codesquad/web/ApiQuestionAcceptanceTest.java index b79dad8..f3f9d1f 100644 --- a/src/test/java/codesquad/web/ApiQuestionAcceptanceTest.java +++ b/src/test/java/codesquad/web/ApiQuestionAcceptanceTest.java @@ -19,11 +19,6 @@ public class ApiQuestionAcceptanceTest extends AcceptanceTest { private Question newQuestion; - @Before - public void setUp() throws Exception { - newQuestion = defaultQuestion(); - } - @Test public void create() { Question newQuestion = defaultQuestion(); @@ -68,7 +63,7 @@ public void update() { } @Test - public void delete() { + public void delete_success() { String location = createResource(DEFAULT_QUESTION_URL, newQuestion); basicAuthTemplate().delete(location); @@ -77,6 +72,16 @@ public void delete() { assertThat(dbQuestion.isDeleted()).isTrue(); } + @Test + public void delete_fail() { + String location = createResource(DEFAULT_QUESTION_URL, newQuestion); + + template().delete(location); + + Question dbQuestion = getResource(location, Question.class); + assertT가hat(dbQuestion.isDeleted()).isFalse(); + } + @Test public void delete_다른_사람() { String location = createResource(DEFAULT_QUESTION_URL, newQuestion); From e5a9b3a7fc39daa4d230fba61cfe9cb3a6165934 Mon Sep 17 00:00:00 2001 From: sunghyuki Date: Sun, 29 May 2022 14:56:36 +0900 Subject: [PATCH 2/8] =?UTF-8?q?=20test=20:=20=EC=9A=94=EA=B5=AC=EC=82=AC?= =?UTF-8?q?=ED=95=AD=20=EB=B3=80=EA=B2=BD=EC=97=90=20=EB=94=B0=EB=A5=B8=20?= =?UTF-8?q?=EC=A7=88=EB=AC=B8=20=EC=82=AD=EC=A0=9C=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=9D=B8=EC=88=98=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/codesquad/web/ApiQuestionAcceptanceTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/codesquad/web/ApiQuestionAcceptanceTest.java b/src/test/java/codesquad/web/ApiQuestionAcceptanceTest.java index f3f9d1f..beaf285 100644 --- a/src/test/java/codesquad/web/ApiQuestionAcceptanceTest.java +++ b/src/test/java/codesquad/web/ApiQuestionAcceptanceTest.java @@ -79,7 +79,7 @@ public void delete_fail() { template().delete(location); Question dbQuestion = getResource(location, Question.class); - assertT가hat(dbQuestion.isDeleted()).isFalse(); + assertThat(dbQuestion.isDeleted()).isFalse(); } @Test From 152fa680d368cb5dd18b1de3c8f74e13e52bb105 Mon Sep 17 00:00:00 2001 From: sunghyuki Date: Sun, 29 May 2022 15:31:56 +0900 Subject: [PATCH 3/8] =?UTF-8?q?=20fix=20:=20=EB=B2=84=EA=B7=B8=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/codesquad/web/QuestionAcceptanceTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/codesquad/web/QuestionAcceptanceTest.java b/src/test/java/codesquad/web/QuestionAcceptanceTest.java index e1d21b6..3cee27f 100644 --- a/src/test/java/codesquad/web/QuestionAcceptanceTest.java +++ b/src/test/java/codesquad/web/QuestionAcceptanceTest.java @@ -92,7 +92,6 @@ public void update_with_login() { ResponseEntity response = basicAuthTemplate().postForEntity(String.format("/questions/%d", defaultQuestion().getId()), request, String.class); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.FOUND); - assertThat(response.getHeaders().getLocation().getPath()).startsWith("/questions"); } @Test From 8b78ae8e437136583a9d6014eac18d47da8e90a0 Mon Sep 17 00:00:00 2001 From: sunghyuki Date: Sun, 29 May 2022 15:51:49 +0900 Subject: [PATCH 4/8] =?UTF-8?q?=20test=20:=20=EC=9A=94=EA=B5=AC=EC=82=AC?= =?UTF-8?q?=ED=95=AD=20=EB=B3=80=EA=B2=BD=EC=97=90=20=EB=94=B0=EB=A5=B8=20?= =?UTF-8?q?=EB=8B=B5=EB=B3=80=20=EC=82=AD=EC=A0=9C=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EB=8B=A8=EC=9C=84=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/codesquad/domain/QuestionTest.java | 39 ++++++++++++++++++- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/test/java/codesquad/domain/QuestionTest.java b/src/test/java/codesquad/domain/QuestionTest.java index 8bff5ae..fcb3562 100644 --- a/src/test/java/codesquad/domain/QuestionTest.java +++ b/src/test/java/codesquad/domain/QuestionTest.java @@ -1,5 +1,7 @@ package codesquad.domain; +import codesquad.CannotDeleteException; +import codesquad.CannotDeleteException; import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; @@ -39,12 +41,45 @@ public void update() { } @Test - public void delete() { + public void delete() throws CannotDeleteException { User loginUser = UserTest.JAVAJIGI; Question question = newQuestion(1L, loginUser); - question.delete(); + question.delete(loginUser); assertThat(question.isDeleted()).isTrue(); } + + @Test(expected = CannotDeleteException.class) + public void delete_fail_when_loginUser_mismatch_writer() throws CannotDeleteException { + User loginUser = UserTest.JAVAJIGI; + User otherUser = UserTest.SANJIGI; + Question question = newQuestion(1L, loginUser); + + question.delete(otherUser); + } + + @Test + public void delete_success_when_loginUser_is_writer_correspond_to_answers() throws CannotDeleteException { + User loginUser = UserTest.JAVAJIGI; + Question question = newQuestion(1L, loginUser); + question.addAnswer(new Answer(loginUser, "하이")); + + assertThat(question.hasSameWriterWithAnswers()).isTrue(); + + question.delete(loginUser); + assertThat(question.isDeleted()).isTrue(); + } + + @Test(expected = CannotDeleteException.class) + public void delete_fail_when_loginUser_is_not_writer_correspond_to_answers() throws CannotDeleteException { + User loginUser = UserTest.JAVAJIGI; + User otherUser = UserTest.SANJIGI; + Question question = newQuestion(1L, loginUser); + question.addAnswer(new Answer(otherUser, "하이")); + + assertThat(question.hasSameWriterWithAnswers()).isFalse(); + + question.delete(loginUser); + } } From e8672c59a45b5f3ff69221ce2acf156eb9706c60 Mon Sep 17 00:00:00 2001 From: sunghyuki Date: Sun, 29 May 2022 15:52:51 +0900 Subject: [PATCH 5/8] =?UTF-8?q?feat=20:=20=EB=8B=B5=EB=B3=80=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80=20-=20?= =?UTF-8?q?=ED=95=B5=EC=8B=AC=20=EB=B9=84=EC=A6=88=EB=8B=88=EC=8A=A4=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=EC=9D=84=20Question=20=EB=8F=84=EB=A9=94?= =?UTF-8?q?=EC=9D=B8=EC=9D=B4=20=EA=B0=80=EC=A7=80=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/codesquad/domain/Question.java | 26 +++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/main/java/codesquad/domain/Question.java b/src/main/java/codesquad/domain/Question.java index a8ed2c5..9edfe26 100644 --- a/src/main/java/codesquad/domain/Question.java +++ b/src/main/java/codesquad/domain/Question.java @@ -1,5 +1,6 @@ package codesquad.domain; +import codesquad.CannotDeleteException; import org.hibernate.annotations.Where; import support.domain.AbstractEntity; import support.domain.UrlGeneratable; @@ -9,6 +10,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Objects; +import java.util.stream.Collectors; @Entity public class Question extends AbstractEntity implements UrlGeneratable { @@ -86,8 +88,25 @@ public void update(Question updatedQuestion) { this.writer = updatedQuestion.writer; } - public void delete() { + public List delete(User loginUser) throws CannotDeleteException { + + if (!this.isOwner(loginUser)) { + throw new CannotDeleteException("삭제 권한이 없습니다."); + } + + if(!hasSameWriterWithAnswers()) { + throw new CannotDeleteException("삭제 권한이 없습니다."); + } + + List deleteHistories = new ArrayList<>(); + + answers.stream() + .filter(answer -> answer.isOwner(writer)) + .forEach(answer -> deleteHistories.add(answer.delete(loginUser))); + this.deleted = true; + + return deleteHistories; } @Override @@ -108,4 +127,9 @@ public boolean equalsTitleAndContents(Question target) { return this.title.equals(target.getTitle()) && this.contents.equals(target.getContents()); } + + public boolean hasSameWriterWithAnswers() { + return answers.stream() + .allMatch(answer -> answer.isOwner(writer)); + } } From b8937553d2f0193d29df04f8ec42c7606179c4ba Mon Sep 17 00:00:00 2001 From: sunghyuki Date: Sun, 29 May 2022 15:55:34 +0900 Subject: [PATCH 6/8] =?UTF-8?q?feat=20:=20=EB=8B=B5=EB=B3=80=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20=EA=B8=B0=EB=8A=A5=20=EB=B0=98=ED=99=98=20=ED=83=80?= =?UTF-8?q?=EC=9E=85=EC=9D=84=20DeleteHistory=EB=A1=9C=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/codesquad/domain/Answer.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/codesquad/domain/Answer.java b/src/main/java/codesquad/domain/Answer.java index 461a781..36451b2 100644 --- a/src/main/java/codesquad/domain/Answer.java +++ b/src/main/java/codesquad/domain/Answer.java @@ -6,6 +6,7 @@ import javax.persistence.*; import javax.validation.constraints.Size; +import java.time.LocalDateTime; @Entity public class Answer extends AbstractEntity implements UrlGeneratable { @@ -75,11 +76,12 @@ public void updateContents(User loginUser, String updatedContents) { this.contents = updatedContents; } - public void delete(User loginUser) { + public DeleteHistory delete(User loginUser) { if (!isOwner(loginUser)) { throw new UnAuthorizedException(); } this.deleted = true; + return new DeleteHistory(ContentType.ANSWER, this.getId(), loginUser, LocalDateTime.now()); } @Override From 1f36495734a16bfb255d13f02ebfc51bea671a3f Mon Sep 17 00:00:00 2001 From: sunghyuki Date: Sun, 29 May 2022 15:56:47 +0900 Subject: [PATCH 7/8] =?UTF-8?q?feat=20:=20DeleteHistoryService=20=ED=81=B4?= =?UTF-8?q?=EB=9E=98=EC=8A=A4=20save=20=EB=A9=94=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/codesquad/service/DeleteHistoryService.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/codesquad/service/DeleteHistoryService.java b/src/main/java/codesquad/service/DeleteHistoryService.java index 772c798..a656817 100644 --- a/src/main/java/codesquad/service/DeleteHistoryService.java +++ b/src/main/java/codesquad/service/DeleteHistoryService.java @@ -20,4 +20,9 @@ public void saveAll(List deleteHistories) { deleteHistoryRepository.save(deleteHistory); } } + + @Transactional + public void save(DeleteHistory deleteHistory) { + deleteHistoryRepository.save(deleteHistory); + } } From 1cc9a2ad1242186a942e1b89344740f8dfbe56b8 Mon Sep 17 00:00:00 2001 From: sunghyuki Date: Sun, 29 May 2022 15:58:03 +0900 Subject: [PATCH 8/8] =?UTF-8?q?feat=20:=20=ED=95=B5=EC=8B=AC=20=EB=B9=84?= =?UTF-8?q?=EC=A6=88=EB=8B=88=EC=8A=A4=20=EB=A1=9C=EC=A7=81=EC=9D=84=20?= =?UTF-8?q?=EB=8F=84=EB=A9=94=EC=9D=B8=EC=9D=B4=20=EA=B0=80=EC=A7=80?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EB=B6=84=EB=A6=AC=ED=95=A8=EC=97=90=20?= =?UTF-8?q?=EB=94=B0=EB=A5=B8=20=EC=84=9C=EB=B9=84=EC=8A=A4=20=EB=A0=88?= =?UTF-8?q?=EC=9D=B4=EC=96=B4=20=EB=B9=84=EC=A6=88=EB=8B=88=EC=8A=A4=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/codesquad/service/QnaService.java | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/main/java/codesquad/service/QnaService.java b/src/main/java/codesquad/service/QnaService.java index 8052e36..2ea941a 100644 --- a/src/main/java/codesquad/service/QnaService.java +++ b/src/main/java/codesquad/service/QnaService.java @@ -55,14 +55,9 @@ public Question update(User loginUser, long id, Question updatedQuestion) throws @Transactional public Question deleteQuestion(User loginUser, long questionId) throws CannotDeleteException { - // TODO 삭제 기능 구현 Question savedQuestion = findById(questionId); - if (!savedQuestion.isOwner(loginUser)) { - throw new CannotDeleteException("삭제 권한이 없습니다."); - } - - savedQuestion.delete(); + deleteHistoryService.saveAll(savedQuestion.delete(loginUser)); return questionRepository.save(savedQuestion); } @@ -92,7 +87,7 @@ public Answer updateAnswer(User loginUser, long id, String updatedContents) { @Transactional public Answer deleteAnswer(User loginUser, long id) { Answer answer = findAnswerById(id); - answer.delete(loginUser); + deleteHistoryService.save(answer.delete(loginUser)); return answerRepository.save(answer); } }