From 9dbb008dcbca1ebb30bd731bc0dab43795fb8bf8 Mon Sep 17 00:00:00 2001 From: aisensiy Date: Tue, 15 Aug 2017 14:35:29 +0800 Subject: [PATCH] delete article --- src/main/java/io/spring/api/ArticleApi.java | 15 ++++++- .../application/AuthorizationService.java | 2 +- .../core/article/ArticleRepository.java | 3 ++ .../infrastructure/article/ArticleMapper.java | 2 + .../article/MyBatisArticleRepository.java | 5 +++ src/main/resources/mapper/ArticleMapper.xml | 3 ++ .../java/io/spring/api/ArticlesApiTest.java | 39 +++++++++++++++++++ .../article/MyBatisArticleRepositoryTest.java | 8 ++++ 8 files changed, 75 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/spring/api/ArticleApi.java b/src/main/java/io/spring/api/ArticleApi.java index de25b87..86075c4 100644 --- a/src/main/java/io/spring/api/ArticleApi.java +++ b/src/main/java/io/spring/api/ArticleApi.java @@ -13,6 +13,7 @@ import lombok.NoArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PutMapping; @@ -45,7 +46,7 @@ public class ArticleApi { @AuthenticationPrincipal User user, @Valid @RequestBody UpdateArticleParam updateArticleParam) { return articleRepository.findBySlug(slug).map(article -> { - if (!AuthorizationService.canUpdateArticle(user, article)) { + if (!AuthorizationService.canWriteArticle(user, article)) { throw new NoAuthorizationException(); } article.update( @@ -56,6 +57,18 @@ public class ArticleApi { return ResponseEntity.ok(articleQueryService.findBySlug(slug, user).get()); }).orElseThrow(ResourceNotFoundException::new); } + + @DeleteMapping + public ResponseEntity deleteArticle(@PathVariable("slug") String slug, + @AuthenticationPrincipal User user) { + return articleRepository.findBySlug(slug).map(article -> { + if (!AuthorizationService.canWriteArticle(user, article)) { + throw new NoAuthorizationException(); + } + articleRepository.remove(article); + return ResponseEntity.noContent().build(); + }).orElseThrow(ResourceNotFoundException::new); + } } @Getter diff --git a/src/main/java/io/spring/application/AuthorizationService.java b/src/main/java/io/spring/application/AuthorizationService.java index be22cda..b1de760 100644 --- a/src/main/java/io/spring/application/AuthorizationService.java +++ b/src/main/java/io/spring/application/AuthorizationService.java @@ -4,7 +4,7 @@ import io.spring.core.article.Article; import io.spring.core.user.User; public class AuthorizationService { - public static boolean canUpdateArticle(User user, Article article) { + public static boolean canWriteArticle(User user, Article article) { return user.getId().equals(article.getUserId()); } } diff --git a/src/main/java/io/spring/core/article/ArticleRepository.java b/src/main/java/io/spring/core/article/ArticleRepository.java index 9eaa8ed..1c01904 100644 --- a/src/main/java/io/spring/core/article/ArticleRepository.java +++ b/src/main/java/io/spring/core/article/ArticleRepository.java @@ -9,4 +9,7 @@ public interface ArticleRepository { Optional
findById(String id); Optional
findBySlug(String slug); + + + void remove(Article article); } diff --git a/src/main/java/io/spring/infrastructure/article/ArticleMapper.java b/src/main/java/io/spring/infrastructure/article/ArticleMapper.java index 370fa9f..239f781 100644 --- a/src/main/java/io/spring/infrastructure/article/ArticleMapper.java +++ b/src/main/java/io/spring/infrastructure/article/ArticleMapper.java @@ -22,4 +22,6 @@ public interface ArticleMapper { Article findBySlug(@Param("slug") String slug); void update(@Param("article") Article article); + + void delete(@Param("id") String id); } diff --git a/src/main/java/io/spring/infrastructure/article/MyBatisArticleRepository.java b/src/main/java/io/spring/infrastructure/article/MyBatisArticleRepository.java index 8f6f0ec..52e2665 100644 --- a/src/main/java/io/spring/infrastructure/article/MyBatisArticleRepository.java +++ b/src/main/java/io/spring/infrastructure/article/MyBatisArticleRepository.java @@ -43,4 +43,9 @@ public class MyBatisArticleRepository implements ArticleRepository { public Optional
findBySlug(String slug) { return Optional.ofNullable(articleMapper.findBySlug(slug)); } + + @Override + public void remove(Article article) { + articleMapper.delete(article.getId()); + } } diff --git a/src/main/resources/mapper/ArticleMapper.xml b/src/main/resources/mapper/ArticleMapper.xml index c2bd3e6..19c609b 100644 --- a/src/main/resources/mapper/ArticleMapper.xml +++ b/src/main/resources/mapper/ArticleMapper.xml @@ -29,6 +29,9 @@ where id = #{article.id} + + delete from articles where id = #{id} + select A.id articleId, diff --git a/src/test/java/io/spring/api/ArticlesApiTest.java b/src/test/java/io/spring/api/ArticlesApiTest.java index 03ca3a0..a34e7db 100644 --- a/src/test/java/io/spring/api/ArticlesApiTest.java +++ b/src/test/java/io/spring/api/ArticlesApiTest.java @@ -232,6 +232,45 @@ public class ArticlesApiTest extends TestWithCurrentUser { .statusCode(403); } + @Test + public void should_delete_article_success() throws Exception { + String title = "title"; + String body = "body"; + String description = "description"; + + Article article = new Article(title, description, body, new String[]{"java", "spring", "jpg"}, user.getId()); + when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article)); + + given() + .header("Authorization", "Token " + token) + .when() + .delete("/articles/{slug}", article.getSlug()) + .then() + .statusCode(204); + + verify(articleRepository).remove(eq(article)); + } + + @Test + public void should_403_if_not_author_delete_article() throws Exception { + String title = "new-title"; + String body = "new body"; + String description = "new description"; + Map updateParam = prepareUpdateParam(title, body, description); + + User anotherUser = new User("test@test.com", "test", "123123", "", ""); + + Article article = new Article(title, description, body, new String[]{"java", "spring", "jpg"}, anotherUser.getId()); + + when(articleRepository.findBySlug(eq(article.getSlug()))).thenReturn(Optional.of(article)); + given() + .header("Authorization", "Token " + token) + .when() + .delete("/articles/{slug}", article.getSlug()) + .then() + .statusCode(403); + } + private HashMap prepareUpdateParam(final String title, final String body, final String description) { return new HashMap() {{ put("article", new HashMap() {{ diff --git a/src/test/java/io/spring/infrastructure/article/MyBatisArticleRepositoryTest.java b/src/test/java/io/spring/infrastructure/article/MyBatisArticleRepositoryTest.java index 8702838..41c3d0c 100644 --- a/src/test/java/io/spring/infrastructure/article/MyBatisArticleRepositoryTest.java +++ b/src/test/java/io/spring/infrastructure/article/MyBatisArticleRepositoryTest.java @@ -64,4 +64,12 @@ public class MyBatisArticleRepositoryTest { assertThat(fetched.getTitle(), is(newTitle)); assertThat(fetched.getBody(), not("")); } + + @Test + public void should_delete_article() throws Exception { + articleRepository.save(article); + + articleRepository.remove(article); + assertThat(articleRepository.findById(article.getId()).isPresent(), is(false)); + } } \ No newline at end of file