后端完成博客管理接口

This commit is contained in:
2024-09-01 14:37:21 +08:00
parent 55b631cd9c
commit d533bd528d
4 changed files with 67 additions and 2 deletions

View File

@@ -0,0 +1,26 @@
import { API } from "../../Plugs/API/API";
import ServerStdResponse from "../../ServerStdResponse";
import MySQLConnection from '../../Plugs/MySQLConnection'
import Auth from "../../Plugs/Middleware/Auth";
// 删除博客
class DelBlog extends API {
constructor() {
super('DELETE', '/console/blog', Auth);
}
public async onRequset(data: any, res: any) {
let { id } = data;
if (!id) {
return res.json(ServerStdResponse.PARAMS_MISSING);
}
let execRes = await MySQLConnection.execute('DELETE FROM blog WHERE `id` = ?', [id]);
if (!execRes || execRes.affectedRows != 1) {
return res.json(ServerStdResponse.SERVER_ERROR);
}
return res.json({ ...ServerStdResponse.OK });
}
}
export default DelBlog;

View File

@@ -11,7 +11,7 @@ class GetBlogs extends API {
public async onRequset(data: any, res: any) { public async onRequset(data: any, res: any) {
// const { uuid } = data._jwt; // const { uuid } = data._jwt;
let resourcesRes = await MySQLConnection.execute("SELECT * FROM blog"); let resourcesRes = await MySQLConnection.execute("SELECT * FROM blog ORDER BY id DESC");
if (!resourcesRes) { if (!resourcesRes) {
return res.json(ServerStdResponse.SERVER_ERROR); return res.json(ServerStdResponse.SERVER_ERROR);
} }

View File

@@ -0,0 +1,35 @@
import { API } from "../../Plugs/API/API";
import ServerStdResponse from "../../ServerStdResponse";
import MySQLConnection from '../../Plugs/MySQLConnection'
import Auth from "../../Plugs/Middleware/Auth";
import crypto from 'crypto'
// 保存博客
class SaveBlog extends API {
constructor() {
super('POST', '/console/saveBlog', Auth);
}
public async onRequset(data: any, res: any) {
let { id, uuid, title, description, publish_time, src, access_level } = data;
if (!title || !description || !publish_time || !src || !access_level) {
return res.json(ServerStdResponse.PARAMS_MISSING);
}
let execRes: any;
if (id) {
// 保存
execRes = await MySQLConnection.execute('UPDATE blog SET title = ?, description = ?, publish_time = ?, src = ?, access_level = ? WHERE `id` = ?', [title, description, publish_time, src, access_level, id]);
} else {
// 新建
const uuid = crypto.createHash('md5').update(`${Math.random()}${Date.now()}`).digest('hex');
execRes = await MySQLConnection.execute('INSERT INTO blog (uuid, title, description, src, publish_time, access_level, visit_count, like_count) VALUES (?,?,?,?,?,?,?,?)', [uuid, title, description, src, publish_time, access_level, 0, 0]);
}
if (!execRes || execRes.affectedRows != 1) {
return res.json(ServerStdResponse.SERVER_ERROR);
}
return res.json({ ...ServerStdResponse.OK });
}
}
export default SaveBlog;

View File

@@ -21,6 +21,8 @@ import GetResources from "../APIs/Console/GetResources";
import GetBlogs from '../APIs/Console/GetBlogs' import GetBlogs from '../APIs/Console/GetBlogs'
import SaveResource from '../APIs/Console/SaveResource' import SaveResource from '../APIs/Console/SaveResource'
import DelResource from '../APIs/Console/DelResource' import DelResource from '../APIs/Console/DelResource'
import SaveBlog from '../APIs/Console/SaveBlog'
import DelBlog from '../APIs/Console/DelBlog'
class Server { class Server {
private logger = new Logger('Server'); private logger = new Logger('Server');
@@ -45,9 +47,11 @@ class Server {
this.apiLoader.add(Login); this.apiLoader.add(Login);
this.apiLoader.add(GetResources); this.apiLoader.add(GetResources);
this.apiLoader.add(GetBlogs)
this.apiLoader.add(SaveResource); this.apiLoader.add(SaveResource);
this.apiLoader.add(DelResource); this.apiLoader.add(DelResource);
this.apiLoader.add(GetBlogs)
this.apiLoader.add(SaveBlog);
this.apiLoader.add(DelBlog);
this.apiLoader.start(config.apiPort); this.apiLoader.start(config.apiPort);
} }