diff --git a/index.js b/index.js index e2e4cf5..f1dd4c0 100644 --- a/index.js +++ b/index.js @@ -78,36 +78,10 @@ server.get('/scenario_package', bodyParser.json(), function (req, res) { var sql = ""; var w = ""; var c = 1; + var d = 1; var args = []; var path = './route_sql/scenario_package.sql'; - var callback = function(arg){ - sql = arg; - console.log("-------------------------get scenario:------------------------------") - console.log(req.body); - //parse request body into a where clause - for (var i in req.body) { - if (c > 1) { - w = w + - ` - AND ` - } - w = w + i + " = '" + req.body[i] + "'"; - args.push(req.body[i]); - c = c + 1; - }; - //if there was no body sent, return with nothing - if (c == 1) { - res.send("no body was sent"); - return; - } - //parse the where clause into the main sql statement - sql = sql.replace(new RegExp("where_clause",'g'),w) - //execute the sql and send the result - //console.log(sql); - Postgres.FirstRow(sql,[],res) - }; - fs.readFile(path, 'utf8', function(err, data){ if (!err){ callback(data); @@ -117,6 +91,25 @@ server.get('/scenario_package', bodyParser.json(), function (req, res) { } }); + var callback = function(arg){ + sql = arg; + + //parse request body into a where clause + ({ c, w, d } = build_where(req, c, w, d, args)); + + //if there was no body sent, return with nothing + if (c == 1) { + res.send("no body was sent"); + return; + } + console.log("-------------------------get scenario:------------------------------") + console.log(req.body); + //parse the where clause into the main sql statement + sql = sql.replace(new RegExp("where_clause",'g'),w) + //execute the sql and send the result + //console.log(sql); + Postgres.FirstRow(sql,[],res) + }; }) server.post('/addmonth_v', bodyParser.json(), function (req, res) {