Browse Source

Refactor: keeping list funcs dry.

pull/13/head
oof1lab 7 years ago
parent
commit
4235d39e0e
  1. 91
      lib/xapi.js

91
lib/xapi.js

@ -231,88 +231,89 @@ class Xapi {
}
async list(req, res) {
prepareListQuery(req, res, queryParamsObj, nested = false) {
let queryParamsObj = {}
queryParamsObj.query = 'select ';
queryParamsObj.params = [];
/**************** select columns ****************/
if (req.query._groupby) {
this.mysql.getColumnsForSelectStmtWithGrpBy(req.query, req.app.locals._tableName, queryParamsObj);
} else {
this.mysql.getColumnsForSelectStmt(req.app.locals._tableName, req.query, queryParamsObj);
}
/**************** tableName ****************/
/**************** add tableName ****************/
queryParamsObj.query += ' from ?? ';
if (nested) {
req.app.locals._tableName = req.app.locals._childTable;
queryParamsObj.params.push(req.app.locals._childTable);
queryParamsObj.query += ' where ';
/**************** add where foreign key ****************/
let whereClause = this.mysql.getForeignKeyWhereClause(req.app.locals._parentTable,
req.params.id,
req.app.locals._childTable);
if (!whereClause) {
return res.status(400).send({
error: "Table is made of composite primary keys - all keys were not in input"
})
}
queryParamsObj.query += whereClause;
this.mysql.getWhereClause(req.query._where, req.app.locals._tableName, queryParamsObj, ' and ');
} else {
queryParamsObj.params.push(req.app.locals._tableName);
/**************** where clause ****************/
/**************** add where clause ****************/
this.mysql.getWhereClause(req.query._where, req.app.locals._tableName, queryParamsObj, ' where ');
/**************** group by ****************/
}
/**************** add group by ****************/
this.mysql.getGroupByClause(req.query._groupby, req.app.locals._tableName, queryParamsObj);
/**************** having ****************/
/**************** add having ****************/
this.mysql.getHavingClause(req.query._having, req.app.locals._tableName, queryParamsObj);
/**************** order clause ****************/
/**************** add order clause ****************/
queryParamsObj.query += this.mysql.getOrderByClause(req.query, req.app.locals._tableName);
/**************** limit clause ****************/
/**************** add limit clause ****************/
queryParamsObj.query += ' limit ?,? '
queryParamsObj.params = queryParamsObj.params.concat(this.mysql.getLimitClause(req.query));
//console.log(queryParamsObj.query, queryParamsObj.params);
let results = await this.mysql.exec(queryParamsObj.query, queryParamsObj.params);
res.status(200).json(results);
}
async nestedList(req, res) {
async list(req, res) {
let queryParamsObj = {}
queryParamsObj.query = 'select ';
queryParamsObj.params = [];
queryParamsObj.query = ''
queryParamsObj.params = []
/**************** tableName ****************/
if (req.query._groupby) {
this.mysql.getColumnsForSelectStmtWithGrpBy(req.query, req.app.locals._tableName, queryParamsObj);
} else {
this.mysql.getColumnsForSelectStmt(req.app.locals._tableName, req.query, queryParamsObj);
}
queryParamsObj.query += ' from ?? where ';
queryParamsObj.params.push(req.app.locals._childTable);
this.prepareListQuery(req, res, queryParamsObj, false);
/**************** where foreign key ****************/
let whereClause = this.mysql.getForeignKeyWhereClause(req.app.locals._parentTable,
req.params.id,
req.app.locals._childTable);
let results = await this.mysql.exec(queryParamsObj.query, queryParamsObj.params);
res.status(200).json(results);
if (!whereClause) {
return res.status(400).send({
error: "Table is made of composite primary keys - all keys were not in input"
})
}
queryParamsObj.query += whereClause;
/**************** where conditions in query ****************/
this.mysql.getWhereClause(req.query._where, req.app.locals._tableName, queryParamsObj, ' and ');
/**************** group by ****************/
this.mysql.getGroupByClause(req.query._groupby, req.app.locals._tableName, queryParamsObj);
/**************** having ****************/
this.mysql.getHavingClause(req.query._having, req.app.locals._tableName, queryParamsObj);
async nestedList(req, res) {
/**************** order clause ****************/
queryParamsObj.query = queryParamsObj.query + this.mysql.getOrderByClause(req.query, req.app.locals._parentTable);
let queryParamsObj = {}
queryParamsObj.query = '';
queryParamsObj.params = [];
/**************** limit clause ****************/
queryParamsObj.query = queryParamsObj.query + ' limit ?,? '
queryParamsObj.params = queryParamsObj.params.concat(this.mysql.getLimitClause(req.query));
this.prepareListQuery(req, res, queryParamsObj, true)
let results = await this.mysql.exec(queryParamsObj.query, queryParamsObj.params);
res.status(200).json(results);

Loading…
Cancel
Save