delete comment
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user