71 lines
2.9 KiB
Java
71 lines
2.9 KiB
Java
package io.spring.api;
|
|
|
|
import com.fasterxml.jackson.annotation.JsonRootName;
|
|
import io.spring.api.exception.InvalidRequestException;
|
|
import io.spring.api.exception.ResourceNotFoundException;
|
|
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;
|
|
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.RestController;
|
|
|
|
import javax.validation.Valid;
|
|
import javax.xml.ws.Response;
|
|
|
|
@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
|
|
public ResponseEntity<CommentData> createComment(@PathVariable("slug") String slug,
|
|
@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);
|
|
return ResponseEntity.status(201).body(commentQueryService.findById(comment.getId(), user).get());
|
|
}
|
|
|
|
private Article findArticle(String slug) {
|
|
return articleRepository.findBySlug(slug).map(article -> article).orElseThrow(ResourceNotFoundException::new);
|
|
}
|
|
}
|
|
|
|
@Getter
|
|
@NoArgsConstructor
|
|
@JsonRootName("comment")
|
|
class NewCommentParam {
|
|
@NotBlank(message = "can't be empty")
|
|
private String body;
|
|
}
|