spring-boot-realworld-examp.../src/main/java/io/spring/api/ArticlesApi.java

67 lines
2.7 KiB
Java
Raw Normal View History

2017-08-15 09:47:18 +07:00
package io.spring.api;
2017-08-18 16:08:27 +07:00
import io.spring.application.ArticleQueryService;
2021-03-03 14:26:48 +07:00
import io.spring.application.Page;
import io.spring.application.article.ArticleCommandService;
import io.spring.application.article.NewArticleParam;
2017-08-15 09:47:18 +07:00
import io.spring.core.article.Article;
import io.spring.core.user.User;
2021-03-03 14:26:48 +07:00
import java.util.HashMap;
import javax.validation.Valid;
2017-08-15 09:47:18 +07:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
2017-08-17 13:27:29 +07:00
import org.springframework.web.bind.annotation.GetMapping;
2017-08-15 09:47:18 +07:00
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
2017-08-17 13:27:29 +07:00
import org.springframework.web.bind.annotation.RequestParam;
2017-08-15 09:47:18 +07:00
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(path = "/articles")
public class ArticlesApi {
private ArticleCommandService articleCommandService;
2021-03-03 14:26:48 +07:00
private ArticleQueryService articleQueryService;
@Autowired
public ArticlesApi(
ArticleCommandService articleCommandService, ArticleQueryService articleQueryService) {
this.articleCommandService = articleCommandService;
2021-03-03 14:26:48 +07:00
this.articleQueryService = articleQueryService;
}
@PostMapping
public ResponseEntity createArticle(
@Valid @RequestBody NewArticleParam newArticleParam, @AuthenticationPrincipal User user) {
Article article = articleCommandService.createArticle(newArticleParam, user);
2021-03-03 14:26:48 +07:00
return ResponseEntity.ok(
new HashMap<String, Object>() {
{
2017-08-15 16:52:23 +07:00
put("article", articleQueryService.findById(article.getId(), user).get());
2021-03-03 14:26:48 +07:00
}
});
}
@GetMapping(path = "feed")
public ResponseEntity getFeed(
@RequestParam(value = "offset", defaultValue = "0") int offset,
@RequestParam(value = "limit", defaultValue = "20") int limit,
@AuthenticationPrincipal User user) {
return ResponseEntity.ok(articleQueryService.findUserFeed(user, new Page(offset, limit)));
}
@GetMapping
public ResponseEntity getArticles(
@RequestParam(value = "offset", defaultValue = "0") int offset,
@RequestParam(value = "limit", defaultValue = "20") int limit,
@RequestParam(value = "tag", required = false) String tag,
@RequestParam(value = "favorited", required = false) String favoritedBy,
@RequestParam(value = "author", required = false) String author,
@AuthenticationPrincipal User user) {
return ResponseEntity.ok(
articleQueryService.findRecentArticles(
tag, author, favoritedBy, new Page(offset, limit), user));
}
2017-08-15 09:47:18 +07:00
}