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; /** * 文章控制器类,处理文章相关的HTTP请求 * 提供文章的增删改查功能,以及按作者、分类和浏览量获取文章的接口 */ @RestController @RequestMapping("/api/articles") @Validated public class ArticleController { @Autowired private IArticleService articleService; // 注入文章服务接口 /** * 根据ID获取单个文章 * @param id 文章ID * @return 返回包含文章信息的ResponseMessage对象 */ @GetMapping("/{id}") public ResponseMessage
getArticle(@PathVariable Integer id) { return articleService.getArticleById(id); } /** * 获取所有文章列表 * @return 返回包含文章列表的ResponseMessage对象 */ @GetMapping public ResponseMessage> getAllArticles() { return articleService.getAllArticles(); } /** * 创建新文章 * 仅限AUTHOR角色用户访问 * @param articleDto 包含文章数据的DTO对象 * @return 返回包含新创建文章信息的ResponseMessage对象 */ @PostMapping @PreAuthorize("hasRole('AUTHOR')") public ResponseMessage
createArticle(@Valid @RequestBody ArticleDto articleDto) { return articleService.saveArticle(articleDto); } /** * 更新现有文章 * 仅限AUTHOR或ADMIN角色用户访问 * @param id 要更新的文章ID * @param articleDto 包含更新后文章数据的DTO对象 * @return 返回包含更新后文章信息的ResponseMessage对象 */ @PutMapping("/{id}") @PreAuthorize("hasRole('AUTHOR') or hasRole('ADMIN')") public ResponseMessage
updateArticle( @PathVariable Integer id, @Valid @RequestBody ArticleDto articleDto) { return articleService.updateArticle(id, articleDto); } /** * 删除文章 * 仅限AUTHOR或ADMIN角色用户访问 * @param id 要删除的文章ID * @return 返回包含被删除文章信息的ResponseMessage对象 */ @DeleteMapping("/{id}") @PreAuthorize("hasRole('AUTHOR') or hasRole('ADMIN')") public ResponseMessage
deleteArticle(@PathVariable Integer id) { return articleService.deleteArticle(id); } /** * 根据作者ID获取其所有文章 * @param authorId 作者ID * @return 返回包含文章列表的ResponseMessage对象 */ @GetMapping("/author/{authorId}") public ResponseMessage> getArticlesByAuthor(@PathVariable Integer authorId) { return articleService.getArticlesByAuthor(authorId); } /** * 根据分类ID获取该分类下的所有文章 * @param categoryId 分类ID * @return 返回包含文章列表的ResponseMessage对象 */ @GetMapping("/category/{categoryId}") public ResponseMessage> getArticlesByCategory(@PathVariable Integer categoryId) { return articleService.getArticlesByCategory(categoryId); } /** * 获取浏览量最高的文章列表 * @return 返回包含热门文章列表的ResponseMessage对象 */ @GetMapping("/popular") public ResponseMessage> getMostViewedArticles() { return articleService.getMostViewedArticles(); } }