Commit 5153564d authored by Peter Lundin's avatar Peter Lundin
Browse files

Some functions work

parent caa9599f
......@@ -8,7 +8,7 @@ const connectionString = process.env.DATABASE_URL || 'postgres://jasper.u:Me4sgy
//const connectionString = process.env.DATABASE_URL || 'postgres://metrix:a001188@localhost:5432/metrix';
router.post('/classes', (req, res, next) => {
router.post('/class/:id', (req, res, next) => {
var pool = new pg.Pool({
connectionString: connectionString,
});
......@@ -22,12 +22,12 @@ router.post('/classes', (req, res, next) => {
}
// SQL Query > Select Data
var sql = "select display_name as \"DisplayText\", id as \"Value\" from cmdb.organisation where class_name = $1 order by sortorder;";
var parameter = [];
client.query(sql, parameter, function(err, result){
var sql = "select display_name as \"DisplayText\", id as \"Value\" from cmdb.ci_class where id = $1;";
var parameter = [id];
client.query(sql, [parameter], function(err, result){
done();
if (err){
return res.json(JSON.parse('{"Result":"ERROR", "Message":"Databasfel Options-Parents}'));
return res.json(JSON.parse('{"Result":"ERROR", "Message":"/classes"}'));
}
else {
var response = '{"Result":"OK", "Options":';
......@@ -56,12 +56,12 @@ router.post('/parents', (req, res, next) => {
}
// SQL Query > Select Data
var sql = "select display_name as \"DisplayText\", id as \"Value\" from cmdb.organisation order by id;";
var sql = "select display_name as \"DisplayText\", id as \"Value\" from cmdb.ci_family_organisation order by id;";
client.query(sql, [], function(err, result){
done();
if (err){
return res.json(JSON.parse('{"Result":"ERROR", "Message":"Databasfel Options-Parents}'));
return res.json(JSON.parse('{"Result":"ERROR", "Message":"/parents"}'));
}
else {
var response = '{"Result":"OK", "Options":';
......@@ -74,7 +74,7 @@ router.post('/parents', (req, res, next) => {
});
router.post('/levels', (req, res, next) => {
router.get('/class_list/:class_type_id', (req, res, next) => {
var pool = new pg.Pool({
connectionString: connectionString,
});
......@@ -88,12 +88,12 @@ router.post('/levels', (req, res, next) => {
}
// SQL Query > Select Data
var sql = "select display_name as \"DisplayText\", id as \"Value\" from cmdb.organisation_level order by id;";
client.query(sql, [], function(err, result){
var sql = "select display_name as \"DisplayText\", id as \"Value\" from cmdb.class where class_type_id =$1;";
var parameter = req.params['class_type_id'];
client.query(sql, [parameter], function(err, result){
done();
if (err){
return res.json(JSON.parse('{"Result":"ERROR", "Message":"Databasfel Options-Levels}'));
return res.json(JSON.parse('{"Result":"ERROR", "Message":"/levels"}'));
}
else {
var response = '{"Result":"OK", "Options":';
......@@ -106,41 +106,10 @@ router.post('/levels', (req, res, next) => {
});
router.post('/types', (req, res, next) => {
var pool = new pg.Pool({
connectionString: connectionString,
});
// Get a Postgres client from the connection pool
pool.connect(function(err, client, done){
// Handle connection errors
if(err) {
done();
console.log(err);
return res.status(500).json({success: false , data: err});
}
// SQL Query > Select Data
var sql = "select display_name as \"DisplayText\", id as \"Value\" from cmdb.organisation_type order by id;";
client.query(sql, [], function(err, result){
done();
if (err){
return res.json(JSON.parse('{"Result":"ERROR", "Message":"Databasfel Options-Levels}'));
}
else {
var response = '{"Result":"OK", "Options":';
response+= JSON.stringify(result.rows);
response+= ',"TotalRecordCount":' + result.rowCount + '}';
return res.json(JSON.parse(response));
}
});
});
});
router.post('/list/:level', (req, res, next) => {
router.get('/list/nodes/:centralNode', (req, res, next) => {
var pool = new pg.Pool({
connectionString: connectionString,
connectionString: connectionString
});
// Get a Postgres client from the connection pool
pool.connect(function(err, client, done){
......@@ -150,44 +119,39 @@ router.post('/list/:level', (req, res, next) => {
console.log(err);
return res.status(500).json({success: false , data: err});
}
var sql = "select * from cmdb.organisation where parent_organisation_id = $1;";
var level = [req.params['level']];
console.log(JSON.stringify(level));
client.query(sql, level, function(err, result){
done();
if (err){
return res.json(JSON.parse('{"Result":"ERROR", "Message":"Databasfel organisation list}'));
}
else {
var response = '{"Result":"OK", "Records":';
response+= JSON.stringify(result.rows);
response+= ',"TotalRecordCount":' + result.rowCount + '}';
return res.json(JSON.parse(response));
}
});
});
});
router.get('/list/nodes', (req, res, next) => {
var pool = new pg.Pool({
connectionString: connectionString,
});
// Get a Postgres client from the connection pool
pool.connect(function(err, client, done){
// Handle connection errors
if(err) {
done();
console.log(err);
return res.status(500).json({success: false , data: err});
}
var sql = "select id, concat((select display_name from cmdb.organisation_type as ot where ot.id=organisation_type), ': ', display_name) as label from cmdb.organisation;";
client.query(sql, [], function(err, result){
// all leaf
var sql = "WITH RECURSIVE org(id, display_name, parent_organisation_id, organisation_level) AS (";
sql+= "SELECT id, display_name, parent_organisation_id, organisation_level FROM cmdb.organisation org WHERE org.id = $1 ";
sql+= "UNION ";
sql+= "SELECT sub.id, sub.display_name, sub.parent_organisation_id, sub.organisation_level ";
sql+= "FROM org, cmdb.organisation sub ";
sql+= "WHERE org.id = sub.parent_organisation_id) ";
sql+= "SELECT id, display_name as label ";
sql+= "FROM org order by organisation_level ";
sql+= "limit 10; ";
/*
too root
WITH RECURSIVE org(id, display_name, parent_organisation_id) AS (
SELECT id, display_name, parent_organisation_id FROM cmdb.organisation org WHERE org.id = 6
UNION
SELECT sub.id, sub.display_name, sub.parent_organisation_id
FROM org, cmdb.organisation sub
WHERE sub.id = org.parent_organisation_id
)
SELECT id, display_name, parent_organisation_id
FROM org
limit 10
*/
// var sql = "select id, concat(id, '. ', (select display_name from cmdb.organisation_type as ot where// ot.id=organisation_type), ': ', display_name) as label from cmdb.organisation where id >0;";
var centralNode = [req.params['centralNode']];
client.query(sql, centralNode, function(err, result){
done();
if (err){
return res.json(JSON.parse('{"Result":"ERROR", "Message":"Databasfel organisation list nodes}'));
if (err){ //JSON.stringify(req.params)
console.log(err.message);
return res.json(JSON.parse('{"Result":"ERROR", "Message":"' + '.' + '"}'));
}
else {
return res.json(result.rows);
......@@ -196,24 +160,46 @@ router.get('/list/nodes', (req, res, next) => {
});
});
router.get('/list/edges', (req, res, next) => {
router.get('/list/edges/:centralNode', (req, res, next) => {
var pool = new pg.Pool({
connectionString: connectionString,
connectionString: connectionString
});
// Get a Postgres client from the connection pool
pool.connect(function(err, client, done){
// Handle connection errors
if(err) {
done();
console.log(err);
console.log(err.message);
return res.status(500).json({success: false , data: err});
}
var sql = "WITH RECURSIVE org(id, display_name, parent_organisation_id) AS (";
sql+= "SELECT id, display_name, parent_organisation_id FROM cmdb.organisation org WHERE org.id = $1 ";
sql+= "UNION ";
sql+= "SELECT sub.id, sub.display_name, sub.parent_organisation_id ";
sql+= "FROM org, cmdb.organisation sub ";
sql+= "WHERE org.id = sub.parent_organisation_id) ";
sql+= "SELECT id as from, parent_organisation_id as to ";
sql+= "FROM org ";
sql+= "limit 10; ";
/*
too root
WITH RECURSIVE org(id, display_name, parent_organisation_id) AS (
SELECT id, display_name, parent_organisation_id FROM cmdb.organisation org WHERE org.id = 6
UNION
SELECT sub.id, sub.display_name, sub.parent_organisation_id
FROM org, cmdb.organisation sub
WHERE sub.id = org.parent_organisation_id
)
SELECT id, display_name, parent_organisation_id
FROM org
limit 10
*/
var sql = "select id as from, parent_organisation_id as to from cmdb.organisation;";
client.query(sql, [], function(err, result){
var centralNode = [req.params['centralNode']];
client.query(sql, centralNode, function(err, result){
done();
if (err){
return res.json(JSON.parse('{"Result":"ERROR", "Message":"Databasfel organisation list}'));
return res.json(JSON.parse('{"Result":"ERROR", "Message":"/list/edges/:top"}'));
}
else {
return res.json(result.rows);
......@@ -225,14 +211,14 @@ router.get('/list/edges', (req, res, next) => {
router.post('/update', (req, res, next) => {
var id = req.body.id;
var display_name = req.body.display_name
var display_name = req.body.display_name;
var description = req.body.description;
var organisation_type = req.body.organisation_type
var organisation_level = req.body.organisation_level
var parent_organisation_id = req.body.parent_organisation_id
var organisation_type = req.body.organisation_type;
var organisation_level = req.body.organisation_level;
var parent_organisation_id = req.body.parent_organisation_id;
var pool = new pg.Pool({
connectionString: connectionString,
connectionString: connectionString
});
// Get a Postgres client from the connection pool
pool.connect(function(err, client, done){
......@@ -251,7 +237,7 @@ router.post('/update', (req, res, next) => {
client.query(sql, values, function(err, result){
done();
if (err){
return res.json(JSON.parse('{"Result":"ERROR", "Message":"Databasfel update Organisation"}'));
return res.json(JSON.parse('{"Result":"ERROR", "Message":"/update"}'));
}
else {
var response = '{"Result":"OK", "Records":';
......@@ -264,12 +250,4 @@ router.post('/update', (req, res, next) => {
});
router.post('/q', (req, res, next) => {
const results = '{"Result":"OK","Records":[{"0":"11-20","AgeInterval":"11-20","1":"5","Amount":"5"},{"0":"21-30","AgeInterval":"21-30","1":"40","Amount":"40"},{"0":"31-40","AgeInterval":"31-40","1":"48","Amount":"48"},{"0":"41-50","AgeInterval":"41-50","1":"43","Amount":"43"},{"0":"51-60","AgeInterval":"51-60","1":"27","Amount":"27"},{"0":"61-...","AgeInterval":"61-...","1":"66","Amount":"66"},{"0":"Totalt","AgeInterval":"Totalt","1":"229","Amount":"229"}],"TotalRecordCount":"6"}';
//const results = '{"Result":"OK","Records":[{"AgeInterval":"11-20","Amount":"5"},{"AgeInterval":"21-30","Amount":"40"}],"TotalRecordCount":"2"}';
//return [];
return res.json(JSON.parse(results));
});
module.exports = router;
var express = require('express');
var router = express.Router();
const pg = require('pg');
const jQuery = require('jquery');
const path = require('path');
const connectionString = process.env.DATABASE_URL || 'postgres://jasper.u:Me4sgyAp@postgresql-utv.smhi.se:5432/jasper';
router.post('/family_type', (req, res, next) => {
var pool = new pg.Pool({
connectionString: connectionString,
});
// Get a Postgres client from the connection pool
pool.connect(function(err, client, done){
// Handle connection errors
if(err) {
done();
console.log(err);
return res.status(500).json({success: false , data: err});
}
// SQL Query > Select Data
var sql = "select * from cmdb.family_type order by sort_order;";
var parameter = [];
client.query(sql, parameter, function(err, result){
done();
if (err){
return res.json(JSON.parse('{"Result":"ERROR", "Message":"/family_type"}'));
}
else {
var response = '{"Result":"OK", "Options":';
response+= JSON.stringify(result.rows);
response+= ',"TotalRecordCount":' + result.rowCount + '}';
return res.json(JSON.parse(response));
}
});
});
});
router.post('/family/:family_type_id', (req, res, next) => {
var pool = new pg.Pool({
connectionString: connectionString,
});
// Get a Postgres client from the connection pool
pool.connect(function(err, client, done){
// Handle connection errors
if(err) {
done();
console.log(err);
return res.status(500).json({success: false , data: err});
}
// SQL Query > Select Data
var sql = "select * from cmdb.family where family_type_id = $1 order by sort_order;";
var parameter = [req.params['family_type_id']];
client.query(sql, parameter, function(err, result){
done();
if (err){
console.log('paramter: ' + JSON.stringify(parameter));
return res.json(JSON.parse('{"Result":"ERROR", "Message":"/family/:family_type_id"}'));
}
else {
var response = '{"Result":"OK", "Options":';
response+= JSON.stringify(result.rows);
response+= ',"TotalRecordCount":' + result.rowCount + '}';
return res.json(JSON.parse(response));
}
});
});
});
module.exports = router;
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment