68 lines
2.2 KiB
Java
68 lines
2.2 KiB
Java
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<Article> getArticle(@PathVariable Integer id) {
|
|
return articleService.getArticleById(id);
|
|
}
|
|
|
|
@GetMapping
|
|
public ResponseMessage<List<Article>> getAllArticles() {
|
|
return articleService.getAllArticles();
|
|
}
|
|
|
|
@PostMapping
|
|
@PreAuthorize("hasRole('AUTHOR')")
|
|
public ResponseMessage<Article> createArticle(@Valid @RequestBody ArticleDto articleDto) {
|
|
return articleService.saveArticle(articleDto);
|
|
}
|
|
|
|
@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();
|
|
}
|
|
}
|