forecast_api/index.js

214 lines
5.8 KiB
JavaScript
Raw Normal View History

2019-01-31 16:32:34 -05:00
require('dotenv').config();
2019-01-31 14:19:42 -05:00
const express = require('express');
var bodyParser = require('body-parser');
2019-01-31 14:19:42 -05:00
const server = express();
2019-01-31 16:32:34 -05:00
const pg = require('pg');
2019-03-01 15:31:31 -05:00
//---------read sql files into variables----------------
var fs = require('fs');
var readline = require('readline');
//-------------------------------------------------------
2019-01-31 16:32:34 -05:00
var Postgres = new pg.Client({
user: process.env.user,
password: process.env.password,
host: process.env.host,
port: process.env.port,
database: process.env.database,
ssl: false,
application_name: "osm_api"
});
Postgres.connect();
2019-03-01 15:31:31 -05:00
Postgres.FirstRow = function (inSQL, args, inResponse) {
Postgres.query(inSQL, args, (err, res) => {
if (err === null) {
2019-01-31 16:32:34 -05:00
inResponse.json(res.rows[0]);
return;
}
2019-03-12 15:31:24 -04:00
console.log(err.stack);
2019-01-31 16:32:34 -05:00
inResponse.json(err.stack);
});
};
2019-03-05 23:57:31 -05:00
server.get('/', (req, res) => res.send('node.js express is up and running'))
server.get('/test_sql', function(req, res){
2019-03-06 05:54:30 -05:00
var path = './route_meta/scenario_package.sql'
2019-03-05 23:57:31 -05:00
var callback = function(arg){
res.send(arg)
};
fs.readFile(path, 'utf8', function(err, data){
if (!err){
callback(data);
} else {
callback(err);
}
});
});
2019-03-05 23:58:32 -05:00
server.get('/get_pool', bodyParser.json(), function (req, res) {
2019-03-12 15:03:21 -04:00
var sql = "";
2019-03-05 23:58:32 -05:00
var args = [req.body.quota_rep];
2019-03-12 15:03:21 -04:00
var path = './route_sql/get_pool.sql';
var callback = function(arg){
sql = arg;
console.log(req.body.quota_rep);
sql = sql.replace("rep_replace",req.body.quota_rep);
Postgres.FirstRow(sql,[],res)
};
fs.readFile(path, 'utf8', function(err, data){
if (!err){
callback(data);
} else {
console.log("fatal error pulling sql file")
callback(err);
}
});
2019-03-05 23:58:32 -05:00
})
2019-03-01 15:37:00 -05:00
server.get('/scenario_package', bodyParser.json(), function (req, res) {
2019-03-12 15:31:24 -04:00
var sql = "";
2019-03-01 15:37:00 -05:00
var w = "";
var c = 1;
var args = [];
2019-03-12 15:31:24 -04:00
var path = './route_sql/scenario_package.sql';
2019-03-01 15:37:00 -05:00
2019-03-12 15:31:24 -04:00
var callback = function(arg){
sql = arg;
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;
2019-03-01 15:37:00 -05:00
}
2019-03-12 15:31:24 -04:00
//parse the where clause into the main sql statement
sql = sql.replace(new RegExp("where_clause",'g'),w)
2019-03-12 15:31:24 -04:00
//execute the sql and send the result
Postgres.FirstRow(sql,[],res)
2019-03-01 15:37:00 -05:00
};
2019-03-12 15:31:24 -04:00
fs.readFile(path, 'utf8', function(err, data){
if (!err){
callback(data);
} else {
console.log("fatal error pulling sql file")
callback(err);
}
});
2019-03-01 15:37:00 -05:00
})
server.post('/addmonth_v', bodyParser.json(), function (req, res) {
2019-03-06 05:54:30 -05:00
2019-03-12 17:19:19 -04:00
var sql = "";
2019-03-06 05:54:30 -05:00
var w = "";
var c = 1;
var args = [];
2019-03-12 17:19:19 -04:00
var path = './route_sql/addmonth_vd.sql';
2019-03-12 17:19:19 -04:00
var callback = function(arg){
sql = arg
2019-03-06 09:53:33 -05:00
2019-03-12 17:19:19 -04:00
for (var i in req.body.scenario) {
//console.log(i);
///console.log(req.body[i]);
if (c > 1) {
w = w +
`
AND `
}
w = w + i + " = '" + req.body.scenario[i] + "'";
args.push(req.body.scenario[i]);
c = c + 1;
};
if (c == 1) {
res.send("no body was sent");
return;
2019-03-06 09:53:33 -05:00
}
2019-03-12 17:19:19 -04:00
console.log("volume adjust:");
console.log(req.body);
//console.log(args);
sql = sql.replace(new RegExp("scenario = target_scenario",'g'),w);
sql = sql.replace(new RegExp("target_increment",'g'),req.body.amount);
sql = sql.replace(new RegExp("target_month",'g'),req.body.month);
//console.log(sql);
Postgres.FirstRow(sql,[],res)
2019-03-06 09:53:33 -05:00
}
2019-03-06 10:10:02 -05:00
2019-03-12 17:19:19 -04:00
fs.readFile(path, 'utf8', function(err, data){
if (!err){
callback(data);
} else {
console.log("fatal error pulling sql file")
callback(err);
2019-03-06 10:10:02 -05:00
}
2019-03-12 17:19:19 -04:00
});
2019-03-06 10:10:02 -05:00
})
server.post('/addmonth_vp', bodyParser.json(), function (req, res) {
var sql = "";
var w = "";
var c = 1;
var args = [];
var path = './route_sql/addmonth_vupd.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 `
}
w = w + i + " = '" + req.body.scenario[i] + "'";
args.push(req.body.scenario[i]);
c = c + 1;
};
if (c == 1) {
res.send("no body was sent");
return;
}
console.log("volume adjust:");
console.log(req.body);
//console.log(args);
sql = sql.replace(new RegExp("where_clause",'g'),w);
sql = sql.replace(new RegExp("target_volume",'g'),req.body.qty);
sql = sql.replace(new RegExp("target_price",'g'),req.body.amount);
sql = sql.replace(new RegExp("target_month",'g'),req.body.month);
//console.log(sql);
Postgres.FirstRow(sql,[],res)
}
fs.readFile(path, 'utf8', function(err, data){
if (!err){
callback(data);
} else {
console.log("fatal error pulling sql file")
callback(err);
}
});
})
2019-03-06 10:10:02 -05:00
server.listen(3000, () => console.log('started'))