package com.qf.myafterprojecy.controller; import com.qf.myafterprojecy.pojo.Article; import com.qf.myafterprojecy.pojo.ResponseMessage; import com.qf.myafterprojecy.pojo.dto.ArticleDto; import com.qf.myafterprojecy.service.IArticleService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.util.List; @RestController @RequestMapping("/api/articles") @Validated public class ArticleController { @Autowired private IArticleService articleService; @GetMapping("/{id}") public ResponseMessage
getArticle(@PathVariable Integer id) { return articleService.getArticleById(id); } @GetMapping public ResponseMessage> getAllArticles() { return articleService.getAllArticles(); } @PostMapping @PreAuthorize("hasRole('AUTHOR')") public ResponseMessage
createArticle(@Valid @RequestBody ArticleDto articleDto) { return articleService.saveArticle(articleDto); } @PutMapping("/{id}") @PreAuthorize("hasRole('AUTHOR') or hasRole('ADMIN')") public ResponseMessage
updateArticle( @PathVariable Integer id, @Valid @RequestBody ArticleDto articleDto) { return articleService.updateArticle(id, articleDto); } @DeleteMapping("/{id}") @PreAuthorize("hasRole('AUTHOR') or hasRole('ADMIN')") public ResponseMessage
deleteArticle(@PathVariable Integer id) { return articleService.deleteArticle(id); } @GetMapping("/author/{authorId}") public ResponseMessage> getArticlesByAuthor(@PathVariable Integer authorId) { return articleService.getArticlesByAuthor(authorId); } @GetMapping("/category/{categoryId}") public ResponseMessage> getArticlesByCategory(@PathVariable Integer categoryId) { return articleService.getArticlesByCategory(categoryId); } @GetMapping("/popular") public ResponseMessage> getMostViewedArticles() { return articleService.getMostViewedArticles(); } }