143 lines
5.8 KiB
Java
143 lines
5.8 KiB
Java
package xin.merlin.myblog_server.controller;
|
|
|
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import xin.merlin.myblog_server.entity.Article;
|
|
import xin.merlin.myblog_server.entity.Comment;
|
|
import xin.merlin.myblog_server.entity.News;
|
|
import xin.merlin.myblog_server.entity.User;
|
|
import xin.merlin.myblog_server.service.impl.ArticleServiceImpl;
|
|
import xin.merlin.myblog_server.service.impl.CommentServiceImpl;
|
|
import xin.merlin.myblog_server.service.impl.NewsServiceImpl;
|
|
import xin.merlin.myblog_server.utils.JwtUtil;
|
|
import xin.merlin.myblog_server.utils.RequestBack;
|
|
import xin.merlin.myblog_server.utils.enums.ResultCode;
|
|
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Paths;
|
|
import java.nio.file.StandardCopyOption;
|
|
import java.util.UUID;
|
|
|
|
@RestController
|
|
@RequestMapping("/admin")
|
|
public class AdminController {
|
|
|
|
|
|
@Autowired
|
|
private JwtUtil jwtUtil;
|
|
|
|
@Autowired
|
|
private NewsServiceImpl newsService;
|
|
|
|
@Autowired
|
|
private ArticleServiceImpl articleService;
|
|
|
|
@Autowired
|
|
private CommentServiceImpl commentService;
|
|
|
|
@Value("${upload.dir}")
|
|
private String uploadDir;
|
|
|
|
//编辑,新增,删除新闻
|
|
@PostMapping("/update/news")
|
|
RequestBack editNews(@RequestBody News news, @RequestHeader("Authorization")String token) {
|
|
if(!jwtUtil.getUAccount(token.substring(7)).equals("admin")) return RequestBack.fail(ResultCode.USER_NOT_FOUND);
|
|
newsService.updateById(news);
|
|
return RequestBack.success(ResultCode.SUCCESS);
|
|
}
|
|
|
|
@PostMapping("/add/news")
|
|
RequestBack addNews(@RequestBody News news, @RequestHeader("Authorization")String token) {
|
|
if(!jwtUtil.getUAccount(token.substring(7)).equals("admin")) return RequestBack.fail(ResultCode.USER_NOT_FOUND);
|
|
newsService.save(news);
|
|
return RequestBack.success(ResultCode.SUCCESS);
|
|
}
|
|
|
|
@PostMapping("/delete/news")
|
|
RequestBack deleteNews(@RequestBody News news,@RequestHeader("Authorization")String token) {
|
|
if(!jwtUtil.getUAccount(token.substring(7)).equals("admin")) return RequestBack.fail(ResultCode.USER_NOT_FOUND);
|
|
newsService.removeById(news.getId());
|
|
return RequestBack.success(ResultCode.SUCCESS);
|
|
}
|
|
|
|
//编辑,新增,删除文章
|
|
@PostMapping("/update/article")
|
|
RequestBack editArticle(@RequestBody Article article, @RequestHeader("Authorization")String token) {
|
|
if(!jwtUtil.getUAccount(token.substring(7)).equals("admin")) return RequestBack.fail(ResultCode.USER_NOT_FOUND);
|
|
articleService.updateById(article);
|
|
return RequestBack.success(ResultCode.SUCCESS);
|
|
}
|
|
|
|
@PostMapping("/add/article")
|
|
RequestBack addArticle(@RequestBody Article article, @RequestHeader("Authorization")String token) {
|
|
if(!jwtUtil.getUAccount(token.substring(7)).equals("admin")) return RequestBack.fail(ResultCode.USER_NOT_FOUND);
|
|
articleService.save(article);
|
|
return RequestBack.success(ResultCode.SUCCESS);
|
|
}
|
|
|
|
@PostMapping("/delete/article")
|
|
RequestBack deleteArticle(@RequestBody Article article, @RequestHeader("Authorization")String token) {
|
|
if(!jwtUtil.getUAccount(token.substring(7)).equals("admin")) return RequestBack.fail(ResultCode.USER_NOT_FOUND);
|
|
articleService.removeById(article.getId());
|
|
return RequestBack.success(ResultCode.SUCCESS);
|
|
|
|
}
|
|
|
|
//获取、删除评论
|
|
@GetMapping("/get/comments")
|
|
RequestBack getComments(@RequestParam Integer current,@RequestParam Integer size,@RequestHeader("Authorization")String token) {
|
|
if(!jwtUtil.getUAccount(token.substring(7)).equals("admin")) return RequestBack.fail(ResultCode.USER_NOT_FOUND);
|
|
Page<Comment> page = new Page<>(current,size);
|
|
|
|
return RequestBack.success(ResultCode.SUCCESS,commentService.page(page));
|
|
}
|
|
|
|
@PostMapping("/delete/comment")
|
|
RequestBack addArticle(@RequestBody Comment comment, @RequestHeader("Authorization")String token) {
|
|
if(!jwtUtil.getUAccount(token.substring(7)).equals("admin")) return RequestBack.fail(ResultCode.USER_NOT_FOUND);
|
|
commentService.removeById(comment.getId());
|
|
return RequestBack.success(ResultCode.SUCCESS);
|
|
}
|
|
|
|
//获取,删除用户
|
|
@PostMapping("/get/users")
|
|
RequestBack getUsers(@RequestBody User user , @RequestHeader("Authorization")String token) {
|
|
if(!jwtUtil.getUAccount(token.substring(7)).equals("admin")) return RequestBack.fail(ResultCode.USER_NOT_FOUND);
|
|
commentService.removeById(user.getId());
|
|
return RequestBack.success(ResultCode.SUCCESS);
|
|
}
|
|
|
|
//上传照片
|
|
@PostMapping("/upload/img")
|
|
RequestBack uploadImg(@RequestHeader("Authorization")String token, @RequestParam("image") MultipartFile file) throws IOException {
|
|
if(!jwtUtil.getUAccount(token.substring(7)).equals("admin")) return RequestBack.fail(ResultCode.USER_NOT_FOUND);
|
|
if (file == null || file.isEmpty()) {
|
|
return RequestBack.fail(ResultCode.NOT_FOUND);
|
|
}
|
|
String original = file.getOriginalFilename();
|
|
String ext = "";
|
|
if (original != null && original.contains(".")) {
|
|
ext = original.substring(original.lastIndexOf('.'));
|
|
}
|
|
|
|
String filename = UUID.randomUUID().toString() + ext;
|
|
|
|
Path dirPath = Paths.get(uploadDir);
|
|
Files.createDirectories(dirPath);
|
|
|
|
Path target = dirPath.resolve(filename);
|
|
try (InputStream in = file.getInputStream()) {
|
|
Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING);
|
|
}
|
|
|
|
return RequestBack.success(ResultCode.SUCCESS, "https://blog.merlin.xin/app/uploads/"+filename);
|
|
}
|
|
}
|