forecast_api/index.js
2019-02-14 18:47:59 +00:00

90 lines
2.1 KiB
JavaScript

require('dotenv').config();
const express = require('express');
var bodyParser = require('body-parser');
const server = express();
const pg = require('pg');
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();
Postgres.FirstRow = function(inSQL,args, inResponse)
{
Postgres.query(inSQL,args, (err, res) => {
if (err === null)
{
inResponse.json(res.rows[0]);
return;
}
inResponse.json(err.stack);
});
};
server.get('/list_sources', function (req,res) {
var sql = "select jsonb_agg(defn) from tps.srce";
console.log(req.query);
Postgres.FirstRow(sql,[],res);
})
server.get('/monthly_orders', bodyParser.json(), function (req,res) {
var w = "";
var c = 1;
var args = [];
for (var i in req.body) {
//console.log(i);
///console.log(req.body[i]);
if (c > 1) {
w = w +
`
AND `
}
w = w + i + " = '" + req.body[i] + "'";
args.push(req.body[i]);
c = c + 1;
};
if (c == 1) {
res.send("no body was sent");
return;
}
//console.log(w);
//console.log(args);
w =
`
WITH
ini AS (
SELECT
oseas
,extract('month' from odate) monthn
,sum(qty) qty
,sum(sales) sales
,sum(stdcost) stdcost
FROM
rlarp.osm_ppfa_varto_mv
WHERE
` + w +
`
GROUP BY
oseas
,extract('month' from odate)
)
SELECT
jsonb_agg(row_to_json(ini)::jsonb)
FROM
ini`
console.log(w);
Postgres.FirstRow(w,[],res)
//res.json("hi")
})
server.get('/', (req,res) => res.send('node.js express is running: 🎉'))
server.listen(3000,() => console.log('started'))