重构Article模块 新增Message模块 优化安全配置

This commit is contained in:
qingfeng1121
2025-10-10 14:39:43 +08:00
parent 92c604e1f5
commit fdb0608751
15 changed files with 694 additions and 156 deletions

View File

@@ -5,37 +5,63 @@ 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("/article")
@RequestMapping("/api/articles")
@Validated
public class ArticleController {
/**
*
*/
@Autowired
IArticleService ArticleService;
private IArticleService articleService;
@GetMapping("/{id}")
public ResponseMessage<Article> getArticle(@PathVariable Integer id) {
return articleService.getArticleById(id);
}
@GetMapping
public ResponseMessage<Iterable<Article>> getArticleAllByID(@Validated @RequestBody ArticleDto articleDto){
return ArticleService.getArticleAllByID(articleDto);
public ResponseMessage<List<Article>> getAllArticles() {
return articleService.getAllArticles();
}
@PutMapping
public ResponseMessage<Article> UpdateArticle(@RequestBody ArticleDto articleDto){
return ArticleService.SaveArticle(articleDto);
};
@PostMapping
public ResponseMessage<Article> AddArticle(@RequestBody ArticleDto articleDto){
return ArticleService.SaveArticle(articleDto);
@PreAuthorize("hasRole('AUTHOR')")
public ResponseMessage<Article> createArticle(@Valid @RequestBody ArticleDto articleDto) {
return articleService.saveArticle(articleDto);
}
@DeleteMapping
public ResponseMessage<Article> DeleteArticle(@RequestBody ArticleDto articleDto){
return ArticleService.deleteArticle(articleDto.getArticleid());
@PutMapping("/{id}")
@PreAuthorize("hasRole('AUTHOR') or hasRole('ADMIN')")
public ResponseMessage<Article> updateArticle(
@PathVariable Integer id,
@Valid @RequestBody ArticleDto articleDto) {
return articleService.updateArticle(id, articleDto);
}
@DeleteMapping("/{id}")
@PreAuthorize("hasRole('AUTHOR') or hasRole('ADMIN')")
public ResponseMessage<Article> deleteArticle(@PathVariable Integer id) {
return articleService.deleteArticle(id);
}
@GetMapping("/author/{authorId}")
public ResponseMessage<List<Article>> getArticlesByAuthor(@PathVariable Integer authorId) {
return articleService.getArticlesByAuthor(authorId);
}
@GetMapping("/category/{categoryId}")
public ResponseMessage<List<Article>> getArticlesByCategory(@PathVariable Integer categoryId) {
return articleService.getArticlesByCategory(categoryId);
}
@GetMapping("/popular")
public ResponseMessage<List<Article>> getMostViewedArticles() {
return articleService.getMostViewedArticles();
}
}