delete article

This commit is contained in:
aisensiy 2017-08-15 14:35:29 +08:00
parent 6c41584abe
commit 9dbb008dcb
8 changed files with 75 additions and 2 deletions

View File

@ -13,6 +13,7 @@ import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal; 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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.PutMapping;
@ -45,7 +46,7 @@ public class ArticleApi {
@AuthenticationPrincipal User user, @AuthenticationPrincipal User user,
@Valid @RequestBody UpdateArticleParam updateArticleParam) { @Valid @RequestBody UpdateArticleParam updateArticleParam) {
return articleRepository.findBySlug(slug).map(article -> { return articleRepository.findBySlug(slug).map(article -> {
if (!AuthorizationService.canUpdateArticle(user, article)) { if (!AuthorizationService.canWriteArticle(user, article)) {
throw new NoAuthorizationException(); throw new NoAuthorizationException();
} }
article.update( article.update(
@ -56,6 +57,18 @@ public class ArticleApi {
return ResponseEntity.ok(articleQueryService.findBySlug(slug, user).get()); return ResponseEntity.ok(articleQueryService.findBySlug(slug, user).get());
}).orElseThrow(ResourceNotFoundException::new); }).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 @Getter

View File

@ -4,7 +4,7 @@ import io.spring.core.article.Article;
import io.spring.core.user.User; import io.spring.core.user.User;
public class AuthorizationService { 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()); return user.getId().equals(article.getUserId());
} }
} }

View File

@ -9,4 +9,7 @@ public interface ArticleRepository {
Optional<Article> findById(String id); Optional<Article> findById(String id);
Optional<Article> findBySlug(String slug); Optional<Article> findBySlug(String slug);
void remove(Article article);
} }

View File

@ -22,4 +22,6 @@ public interface ArticleMapper {
Article findBySlug(@Param("slug") String slug); Article findBySlug(@Param("slug") String slug);
void update(@Param("article") Article article); void update(@Param("article") Article article);
void delete(@Param("id") String id);
} }

View File

@ -43,4 +43,9 @@ public class MyBatisArticleRepository implements ArticleRepository {
public Optional<Article> findBySlug(String slug) { public Optional<Article> findBySlug(String slug) {
return Optional.ofNullable(articleMapper.findBySlug(slug)); return Optional.ofNullable(articleMapper.findBySlug(slug));
} }
@Override
public void remove(Article article) {
articleMapper.delete(article.getId());
}
} }

View File

@ -29,6 +29,9 @@
</set> </set>
where id = #{article.id} where id = #{article.id}
</update> </update>
<delete id="delete">
delete from articles where id = #{id}
</delete>
<sql id="selectArticle"> <sql id="selectArticle">
select select
A.id articleId, A.id articleId,

View File

@ -232,6 +232,45 @@ public class ArticlesApiTest extends TestWithCurrentUser {
.statusCode(403); .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<String, Object> 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<String, Object> prepareUpdateParam(final String title, final String body, final String description) { private HashMap<String, Object> prepareUpdateParam(final String title, final String body, final String description) {
return new HashMap<String, Object>() {{ return new HashMap<String, Object>() {{
put("article", new HashMap<String, Object>() {{ put("article", new HashMap<String, Object>() {{

View File

@ -64,4 +64,12 @@ public class MyBatisArticleRepositoryTest {
assertThat(fetched.getTitle(), is(newTitle)); assertThat(fetched.getTitle(), is(newTitle));
assertThat(fetched.getBody(), not("")); 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));
}
} }