From 4107a87ee7ef9b1ef551be6ea4693f265cf73895 Mon Sep 17 00:00:00 2001 From: oof1lab Date: Tue, 31 Oct 2017 12:36:37 +0000 Subject: [PATCH] refactor: comments --- lib/xapi.js | 47 +++++++++++++++-------------------------------- 1 file changed, 15 insertions(+), 32 deletions(-) diff --git a/lib/xapi.js b/lib/xapi.js index c57b130f99..55c102f107 100644 --- a/lib/xapi.js +++ b/lib/xapi.js @@ -30,14 +30,17 @@ class Xapi { urlMiddleware(req, res, next) { + // get only request url from originalUrl let justUrl = req.originalUrl.split('?')[0] let pathSplit = justUrl.split('/') if (pathSplit.length >= 2 && pathSplit[1] === 'api') { if (pathSplit.length >= 5) { + // handle for relational routes req.app.locals._parentTable = pathSplit[2] req.app.locals._childTable = pathSplit[4] } else { + // handles rest of routes req.app.locals._tableName = pathSplit[2] } } @@ -66,30 +69,34 @@ class Xapi { root(req, res) { - let v = []; - v = this.mysql.getSchemaRoutes(false, req.protocol + '://' + req.get('host') + '/api/'); - v = v.concat(this.mysql.globalRoutesPrint(req.protocol + '://' + req.get('host') + '/api/')) - - res.json(v) + let routes = []; + routes = this.mysql.getSchemaRoutes(false, req.protocol + '://' + req.get('host') + '/api/'); + routes = routes.concat(this.mysql.globalRoutesPrint(req.protocol + '://' + req.get('host') + '/api/')) + res.json(routes) } setupRoutes() { - + // show routes for database schema this.app.get('/', this.asyncMiddleware(this.root.bind(this))) + + // show all resouces this.app.route('/api/tables') .get(this.asyncMiddleware(this.tables.bind(this))); - let resources = []; + /**************** START : setup routes for each table ****************/ + let resources = []; resources = this.mysql.getSchemaRoutes(true, '/api/'); + // iterate over each resource for (var j = 0; j < resources.length; ++j) { let routes = resources[j]['routes']; + // iterate over rach routes in resource and map function for (var i = 0; i < routes.length; ++i) { switch (routes[i]['routeType']) { @@ -142,36 +149,12 @@ class Xapi { } } } + /**************** END : setup routes for each table ****************/ - // this.app.route('/api/:tableName/describe') - // .get(this.asyncMiddleware(this.tableDescribe.bind(this))); - // - // /**************** START : basic apis ****************/ - // this.app.route('/api/:tableName/count') - // .get(this.asyncMiddleware(this.count.bind(this))); - // - // this.app.route('/api/:tableName') - // .get(this.asyncMiddleware(this.list.bind(this))) - // .post(this.asyncMiddleware(this.create.bind(this))); - // - // this.app.route('/api/:tableName/:id') - // .get(this.asyncMiddleware(this.read.bind(this))) - // .put(this.asyncMiddleware(this.update.bind(this))) - // .delete(this.asyncMiddleware(this.delete.bind(this))); - // - // this.app.route('/api/:tableName/:id/exists') - // .get(this.asyncMiddleware(this.exists.bind(this))); - // - // this.app.route('/api/:parentTable/:id/:childTable') - // .get(this.asyncMiddleware(this.nestedList.bind(this))); - // /**************** END : basic apis ****************/ - if (this.sqlConfig.dynamic === 1) { - this.app.route('/dynamic*') .post(this.asyncMiddleware(this.runQuery.bind(this))); - } }