diff --git a/index.js b/index.js index 80db038..70414ac 100644 --- a/index.js +++ b/index.js @@ -129,34 +129,10 @@ server.post('/addmonth_v', bodyParser.json(), function (req, res) { var path = './route_sql/addmonth_vd.sql'; var callback = function(arg){ - sql = arg - - for (var i in req.body.scenario) { - //console.log(i); - ///console.log(req.body[i]); - if (c > 1) { - w = w + - ` - AND ` - } - if (Array.isArray(req.body.scenario[i])){ - //if the scenario key has a value that is an array of items, push it into an `IN` statement - //iter = [stage1, stage2] --> SQL --> iter IN ('stag1', stage2') - w = w + i + " IN ("; - for (var j in req.body.scenario[i]){ - if (d>1){ - w = w + ","; - } - w = w + "'" + req.body.scenario[i][j] + "'"; - d = d + 1; - } - w = w + ")" - } else { - w = w + i + " = '" + req.body.scenario[i] + "'"; - } - args.push(req.body.scenario[i]); - c = c + 1; - }; + sql = arg; + //buile where clause expression + ({ c, w, d } = build_where(req, c, w, d, args)); + if (c == 1) { res.send("no body was sent"); return; @@ -373,4 +349,36 @@ server.post('/scale_vp', bodyParser.json(), function (req, res) { }); }) +function build_where(req, c, w, d, args) { + for (var i in req.body.scenario) { + //console.log(i); + ///console.log(req.body[i]); + if (c > 1) { + w = w + + ` + AND `; + } + if (Array.isArray(req.body.scenario[i])) { + //if the scenario key has a value that is an array of items, push it into an `IN` statement + //iter = [stage1, stage2] --> SQL --> iter IN ('stag1', stage2') + w = w + i + " IN ("; + for (var j in req.body.scenario[i]) { + if (d > 1) { + w = w + ","; + } + w = w + "'" + req.body.scenario[i][j] + "'"; + d = d + 1; + } + w = w + ")"; + } + else { + w = w + i + " = '" + req.body.scenario[i] + "'"; + } + args.push(req.body.scenario[i]); + c = c + 1; + } + ; + return { c, w, d }; +} + server.listen(3000, () => console.log('started')) \ No newline at end of file