2017-08-15 15:36:07 +07:00
|
|
|
package io.spring.api;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.annotation.JsonRootName;
|
2017-08-15 17:16:06 +07:00
|
|
|
import io.spring.api.exception.NoAuthorizationException;
|
2017-08-15 15:36:07 +07:00
|
|
|
import io.spring.api.exception.ResourceNotFoundException;
|
2017-08-18 16:08:27 +07:00
|
|
|
import io.spring.application.CommentQueryService;
|
2021-03-03 14:26:48 +07:00
|
|
|
import io.spring.application.data.CommentData;
|
2017-08-15 15:36:07 +07:00
|
|
|
import io.spring.core.article.Article;
|
|
|
|
import io.spring.core.article.ArticleRepository;
|
|
|
|
import io.spring.core.comment.Comment;
|
|
|
|
import io.spring.core.comment.CommentRepository;
|
2021-03-03 14:26:48 +07:00
|
|
|
import io.spring.core.service.AuthorizationService;
|
2017-08-15 15:36:07 +07:00
|
|
|
import io.spring.core.user.User;
|
2021-03-03 14:26:48 +07:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import javax.validation.Valid;
|
|
|
|
import javax.validation.constraints.NotBlank;
|
2017-08-15 15:36:07 +07:00
|
|
|
import lombok.Getter;
|
|
|
|
import lombok.NoArgsConstructor;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
2017-08-15 16:52:23 +07:00
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
2017-08-15 15:36:07 +07:00
|
|
|
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;
|
2017-08-15 17:16:06 +07:00
|
|
|
import org.springframework.web.bind.annotation.RequestMethod;
|
2017-08-15 15:36:07 +07:00
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
@RequestMapping(path = "/articles/{slug}/comments")
|
|
|
|
public class CommentsApi {
|
2021-03-03 14:26:48 +07:00
|
|
|
private ArticleRepository articleRepository;
|
|
|
|
private CommentRepository commentRepository;
|
|
|
|
private CommentQueryService commentQueryService;
|
2017-08-15 15:36:07 +07:00
|
|
|
|
2021-03-03 14:26:48 +07:00
|
|
|
@Autowired
|
|
|
|
public CommentsApi(
|
|
|
|
ArticleRepository articleRepository,
|
|
|
|
CommentRepository commentRepository,
|
|
|
|
CommentQueryService commentQueryService) {
|
|
|
|
this.articleRepository = articleRepository;
|
|
|
|
this.commentRepository = commentRepository;
|
|
|
|
this.commentQueryService = commentQueryService;
|
|
|
|
}
|
2017-08-15 15:36:07 +07:00
|
|
|
|
2021-03-03 14:26:48 +07:00
|
|
|
@PostMapping
|
|
|
|
public ResponseEntity<?> createComment(
|
|
|
|
@PathVariable("slug") String slug,
|
|
|
|
@AuthenticationPrincipal User user,
|
|
|
|
@Valid @RequestBody NewCommentParam newCommentParam) {
|
|
|
|
Article article = findArticle(slug);
|
|
|
|
Comment comment = new Comment(newCommentParam.getBody(), user.getId(), article.getId());
|
|
|
|
commentRepository.save(comment);
|
|
|
|
return ResponseEntity.status(201)
|
|
|
|
.body(commentResponse(commentQueryService.findById(comment.getId(), user).get()));
|
|
|
|
}
|
2017-08-15 16:52:23 +07:00
|
|
|
|
2021-03-03 14:26:48 +07:00
|
|
|
@GetMapping
|
|
|
|
public ResponseEntity getComments(
|
|
|
|
@PathVariable("slug") String slug, @AuthenticationPrincipal User user) {
|
|
|
|
Article article = findArticle(slug);
|
|
|
|
List<CommentData> comments = commentQueryService.findByArticleId(article.getId(), user);
|
|
|
|
return ResponseEntity.ok(
|
|
|
|
new HashMap<String, Object>() {
|
|
|
|
{
|
2017-08-15 16:52:23 +07:00
|
|
|
put("comments", comments);
|
2021-03-03 14:26:48 +07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-08-15 15:36:07 +07:00
|
|
|
|
2021-03-03 14:26:48 +07:00
|
|
|
@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)) {
|
2017-08-15 17:16:06 +07:00
|
|
|
throw new NoAuthorizationException();
|
2021-03-03 14:26:48 +07:00
|
|
|
}
|
|
|
|
commentRepository.remove(comment);
|
|
|
|
return ResponseEntity.noContent().build();
|
|
|
|
})
|
|
|
|
.orElseThrow(ResourceNotFoundException::new);
|
|
|
|
}
|
2017-08-15 17:16:06 +07:00
|
|
|
|
2021-03-03 14:26:48 +07:00
|
|
|
private Article findArticle(String slug) {
|
|
|
|
return articleRepository
|
|
|
|
.findBySlug(slug)
|
|
|
|
.map(article -> article)
|
|
|
|
.orElseThrow(ResourceNotFoundException::new);
|
|
|
|
}
|
2017-08-15 16:52:23 +07:00
|
|
|
|
2021-03-03 14:26:48 +07:00
|
|
|
private Map<String, Object> commentResponse(CommentData commentData) {
|
|
|
|
return new HashMap<String, Object>() {
|
|
|
|
{
|
|
|
|
put("comment", commentData);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2017-08-15 15:36:07 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Getter
|
|
|
|
@NoArgsConstructor
|
|
|
|
@JsonRootName("comment")
|
|
|
|
class NewCommentParam {
|
2021-03-03 14:26:48 +07:00
|
|
|
@NotBlank(message = "can't be empty")
|
|
|
|
private String body;
|
2017-08-15 15:36:07 +07:00
|
|
|
}
|