From 157f8f356d67757f8a04a8dd531e42383cff6539 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Wed, 13 Mar 2019 13:41:54 -0400 Subject: [PATCH] adjust scenario_package to reflect other route formats --- index.js | 47 ++++++++++++++++++++--------------------------- 1 file changed, 20 insertions(+), 27 deletions(-) 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) {