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