delete comment

This commit is contained in:
aisensiy
2017-08-15 18:16:06 +08:00
parent f31bcbc6e0
commit 001d4e1eea
9 changed files with 85 additions and 14 deletions

View File

@@ -2,7 +2,9 @@ package io.spring.api;
import com.fasterxml.jackson.annotation.JsonRootName;
import io.spring.api.exception.InvalidRequestException;
import io.spring.api.exception.NoAuthorizationException;
import io.spring.api.exception.ResourceNotFoundException;
import io.spring.application.AuthorizationService;
import io.spring.application.comment.CommentData;
import io.spring.application.comment.CommentQueryService;
import io.spring.core.article.Article;
@@ -22,6 +24,7 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
@@ -70,6 +73,20 @@ public class CommentsApi {
}});
}
@RequestMapping(path = "{id}", method = RequestMethod.DELETE)
public ResponseEntity deleteComment(@PathVariable("slug") String slug,
@PathVariable("id") String commentId,
@AuthenticationPrincipal User user) {
Article article = findArticle(slug);
return commentRepository.findById(article.getId(), commentId).map(comment -> {
if (!AuthorizationService.canWriteComment(user, article, comment)) {
throw new NoAuthorizationException();
}
commentRepository.remove(comment);
return ResponseEntity.noContent().build();
}).orElseThrow(ResourceNotFoundException::new);
}
private Article findArticle(String slug) {
return articleRepository.findBySlug(slug).map(article -> article).orElseThrow(ResourceNotFoundException::new);
}

View File

@@ -1,10 +1,15 @@
package io.spring.application;
import io.spring.core.article.Article;
import io.spring.core.comment.Comment;
import io.spring.core.user.User;
public class AuthorizationService {
public static boolean canWriteArticle(User user, Article article) {
return user.getId().equals(article.getUserId());
}
public static boolean canWriteComment(User user, Article article, Comment comment) {
return user.getId().equals(article.getUserId()) || user.getId().equals(comment.getUserId());
}
}

View File

@@ -5,5 +5,7 @@ import java.util.Optional;
public interface CommentRepository {
void save(Comment comment);
Optional<Comment> findById(String id);
Optional<Comment> findById(String articleId, String id);
void remove(Comment comment);
}

View File

@@ -10,5 +10,7 @@ import org.springframework.stereotype.Component;
public interface CommentMapper {
void insert(@Param("comment") Comment comment);
Comment findById(@Param("id") String id);
Comment findById(@Param("articleId") String articleId, @Param("id") String id);
void delete(@Param("id") String id);
}

View File

@@ -22,7 +22,12 @@ public class MyBatisCommentRepository implements CommentRepository {
}
@Override
public Optional<Comment> findById(String id) {
return Optional.ofNullable(commentMapper.findById(id));
public Optional<Comment> findById(String articleId, String id) {
return Optional.ofNullable(commentMapper.findById(articleId, id));
}
@Override
public void remove(Comment comment) {
commentMapper.delete(comment.getId());
}
}

View File

@@ -12,6 +12,9 @@
#{comment.createdAt}
)
</insert>
<delete id="delete">
delete from comments where id = #{id}
</delete>
<select id="findById" resultMap="comment">
select
id commentId,
@@ -20,7 +23,7 @@
article_id commentArticleId,
created_at commentCreatedAt
from comments
where id = #{id}
where id = #{id} and article_id = #{articleId}
</select>
<resultMap id="comment" type="io.spring.core.comment.Comment">
<id column="commentId" property="id"/>