@yhiot/utils
Version:
225 lines (213 loc) • 5.53 kB
JavaScript
import Errcode, { EC } from '../Errcode';
import { memParse as parseQueryString } from '@yhiot/dbcached';
import _debug from 'debug';
const debug = _debug('app:preCRUD');
/**
* @api {GET} /apis/v1/mqtt/:model/:_id 通用获取单条记录
* @apiDescription model:`user,company,vehicle,device`
* @apiName getModel
* @apiGroup apiMqttV2
* @apiVersion 1.0.0
* @apiParam {String} model Mandatory modelname.
* @apiParam {String} _id Mandatory
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* errcode: 0,
* result: {
* "id": "5ba27cc3a70db45dd108b53f",
* "status": 0,
* ...
* }
* }
* @apiErrorExample {json} Error-Response:
* {
* errcode: !=0,
* message: "error message"
* }
*/
export async function preGetModel(ctx, next) {
let qstr = ctx.params.qs || ctx.request.querystring;
let options = parseQueryString(qstr);
let _id = ctx.params._id;
if (!_id) {
throw new Errcode('error! _id is null', EC.ERR_PARAM_ERROR);
}
ctx.state._type = 'get';
ctx.state._id = _id;
ctx.state._options = options;
await next();
}
/**
* @api {GET} /apis/v1/mqtt/:model 通用获取记录列表
* @apiDescription model:`user,company,vehicle,device`
* @apiName getModals
* @apiGroup apiMqttV2
* @apiVersion 1.0.0
* @apiParam {String} model Mandatory modelname.
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* errcode: 0,
* result: {
* total: 102, // 总条数,中间有记录插入删除改变状态,可能导致每次查到总量不一致
* items: [
* {
* id: "5bd863882da794797a73001e",
* status: 0,
* author: "5ba27cc3a70db45dd108b53f",
* ...
* },
* {
* id: "5bd00a2222580a229225245e",
* status: 0,
* author: "5ba27cc3a70db45dd108b53f",
* ...
* }
* ]
* } // result
* }
* @apiErrorExample {json} Error-Response:
* {
* errcode: !=0,
* message: "error message"
* }
*/
export async function preGetModels(ctx, next) {
// /apis/v1/mongo/query/log/info.method=$regex-POST
// /apis/v1/mongo/query/log/$or[0][info.method]=$regex-POST&$or[1][info.url]=$regex-api
//let options = qs.parse(ctx.params.qs, { allowDots: true });
let qstr = ctx.params.qs || ctx.request.querystring;
if (qstr) {
qstr = decodeURIComponent(qstr);
}
debug('getModels', qstr);
if (qstr && qstr.startsWith('?')) {
qstr = qstr.substr(1);
}
let options = parseQueryString(qstr);
// debug('options:', options);
ctx.state._type = 'retrieve';
ctx.state._options = options;
await next();
}
/**
* @api {POST} /apis/v1/mqtt/:model 通用创建一条记录
* @apiDescription model:`user,company,vehicle,device`
* @apiName createModal
* @apiGroup apiMqttV2
* @apiVersion 1.0.0
* @apiParam {String} model Mandatory modelname.
* @apiParam {String} [_id] Option id. Mandatory at device/vehicle.
* @apiParamExample {json} Request-Example:
* {
* status,
* ...,
* }
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* errcode: 0,
* result: {
* "id": "5ba27cc3a70db45dd108b53f",
* "status": 0,
* ...
* }
* }
* @apiErrorExample {json} Error-Response:
* {
* errcode: !=0,
* message: "error message"
* }
*/
export async function preCreateModel(ctx, next) {
let args = ctx.request.body;
if (!args) {
throw new Errcode('error! args is null', EC.ERR_PARAM_ERROR);
}
ctx.state._type = 'create';
ctx.state._args = args;
await next();
}
/**
* @api {PUT} /apis/v1/mqtt/:model/:_id 通用更新一条记录
* @apiDescription model:`user,company,vehicle,device`
* @apiName updateModal
* @apiGroup apiMqttV2
* @apiVersion 1.0.0
* @apiParam {String} model Mandatory modelname.
* @apiParam {String} _id Mandatory id.
* @apiParamExample {json} Request-Example:
* {
* status,
* ...,
* }
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* errcode: 0,
* result: {
* "id": "5ba27cc3a70db45dd108b53f",
* "status": 0,
* ...
* }
* }
*/
export async function preUpdateModel(ctx, next) {
let _id = ctx.params._id;
let args = ctx.request.body;
if (!_id) {
throw new Errcode('error! _id is null', EC.ERR_PARAM_ERROR);
}
if (!args) {
throw new Errcode('error! args is null', EC.ERR_PARAM_ERROR);
}
ctx.state._type = 'update';
ctx.state._id = _id;
ctx.state._args = args;
await next();
}
/**
* @api {DELETE} /apis/v1/mqtt/:model/:_id 通用删除一条记录
* @apiDescription model:`user,company,vehicle,device`
* @apiName removeModal
* @apiGroup apiMqttV2
* @apiVersion 1.0.0
* @apiParam {String} model Mandatory modelname.
* @apiParam {String} _id Mandatory id.
* @apiSuccessExample {json} Success-Response:
* HTTP/1.1 200 OK
* {
* errcode: 0,
* result: {
* "id": "5ba27cc3a70db45dd108b53f",
* "status": 0,
* ...
* }
* }
* @apiErrorExample {json} Error-Response:
* {
* errcode: !=0,
* message: "error message"
* }
*/
export async function preRemoveModel(ctx, next) {
let _id = ctx.params._id;
if (!_id) {
throw new Errcode('error! _id is null', EC.ERR_PARAM_ERROR);
}
ctx.state._type = 'remove';
ctx.state._id = _id;
await next();
}
export function postCRUDCreator(crudFunction) {
return async function crudResult(ctx) {
// debug('crudResult1:', ctx.state);
let result = await crudFunction(ctx.state);
// debug('crudResult2:', ctx.state, result);
ctx.body = {
errcode: 0,
result,
};
};
}