feat: first add

This commit is contained in:
2025-01-19 13:22:58 +08:00
commit 7f53f82fd4
16 changed files with 2337 additions and 0 deletions
+144
View File
@@ -0,0 +1,144 @@
import CURDHelper from "./CURDHelper.mjs";
export default class RouterFactor {
name = "";
db = null;
router = null;
constructor({
router: _router,
db: _db,
name: _name
}) {
this.router = _router;
this.db = _db;
this.name = _name;
}
/**
* 创建列表数据路由
*/
createGetList() {
const {
name,
db,
router
} = this;
router.post(`/api/${name}/list`, async (ctx) => {
// 设置头类型, 如果不设置,会直接下载该页面
ctx.type = 'application/json';
const body = ctx.request.body;
const data = await CURDHelper.retrieveList({
db,
name,
params: body
});
ctx.body = data;
});
}
/**
* 创建详情路由
*/
createGetDetail() {
const {
name,
db,
router
} = this;
router.get(`/api/${name}`, async (ctx) => {
// 设置头类型, 如果不设置,会直接下载该页面
ctx.type = 'application/json';
const query = ctx.request.query;
const data = await CURDHelper.retrieveSingle({
db,
name,
params: query
});
ctx.body = data;
});
}
/**
* 修改数据
*/
createUpdate() {
const {
name,
db,
router
} = this;
router.post(`/api/${name}/upd`, async (ctx) => {
// 设置头类型, 如果不设置,会直接下载该页面
ctx.type = 'application/json';
const body = ctx.request.body;
const data = await CURDHelper.update({
db,
name,
data: body
});
ctx.body = data;
});
}
/**
* 新增数据
*/
createAdd() {
const {
name,
db,
router
} = this;
router.post(`/api/${name}/add`, async (ctx) => {
// 设置头类型, 如果不设置,会直接下载该页面
ctx.type = 'application/json';
const body = ctx.request.body;
const data = await CURDHelper.create({
db,
name,
data: body
});
ctx.body = data;
});
}
/**
* 删除数据
*/
createDelete() {
const {
name,
db,
router
} = this;
router.post(`/api/${name}/delete`, async (ctx) => {
// 设置头类型, 如果不设置,会直接下载该页面
ctx.type = 'application/json';
const body = ctx.request.body;
const data = await CURDHelper.delete({
db,
name,
data: body
});
ctx.body = data;
});
}
creaetBaseRouter() {
this.createGetList();
this.createGetDetail();
this.createAdd();
this.createUpdate();
this.createDelete();
}
}