feat: first add
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user