From b6bccfb07f22afc94bd9bede217119464d1c8fec Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Thu, 26 Sep 2019 15:59:24 +0200 Subject: [PATCH] giving more data to client to allow for later management --- index.js | 40 +++++++++++++++++++++++++--------------- updates/2.sql | 2 ++ web/src/App.vue | 26 +++++++++++++++++--------- 3 files changed, 44 insertions(+), 24 deletions(-) create mode 100644 updates/2.sql diff --git a/index.js b/index.js index 99263e4..57bcc09 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,3 @@ -const path = require('path'); const fs = require('fs'); const express = require('express'); const bodyParser = require('body-parser'); @@ -15,22 +14,29 @@ const pg = require('postgresupdater')( const app = express(); +const tryParseJSON = function(text) { + try { + return JSON.parse(text); + } catch { + console.log('invalid json'); + } +}; + app.use(bodyParser.text({ type: '*/*' })); app.post('/', (req, res, next) => { - console.log('post'); - if (req.query.json) { - // json mod requests - } else { - try { - const json = JSON.parse(req.body); - pg.query( - `INSERT INTO "Log" ("App", "Type", "Client", "Message", "Misc", "Stack") Values($1, $2, $3, $4, $5, $6)`, - [json.app, json.type, json.client, json.message, json.misc, json.stack] - ); - } catch { - console.log('invalid json', req.body); + const json = tryParseJSON(req.body); + if (typeof req.query.json != 'undefined') { + switch (json.action) { + case 'delete': { + pg.query('DELETE FROM "Log" WHERE ID = $1', json.data); + } } + } else { + pg.query( + `INSERT INTO "Log" ("App", "Type", "Client", "Message", "Misc", "Stack") Values($1, $2, $3, $4, $5, $6)`, + [json.app, json.type, json.client, json.message, json.misc, json.stack] + ); } res.status(201).end(); next(); @@ -39,16 +45,20 @@ app.post('/', (req, res, next) => { app.get('/', async (req, res, next) => { if (typeof req.query.json != 'undefined') { const data = await pg.query( - `SELECT "Timestamp", "Type", "App", "Client", "Message", "Misc", "Stack" FROM "LogView"` + `SELECT "ID", "Timestamp", "Type", "App", "Client", "Message", "Misc", "Stack" FROM "Log"` ); const rows = []; + const headings = data.rows.length > 0 ? Object.keys(data.rows[0]) : []; + const hiddenData = [0]; for (let row of data.rows) { rows.push(Object.values(row)); } res .status(200) .type('application/json') - .end(JSON.stringify(rows)); + .end( + JSON.stringify({ headings: headings, data: rows, hidden: hiddenData }) + ); } else { next(); } diff --git a/updates/2.sql b/updates/2.sql new file mode 100644 index 0000000..cb43871 --- /dev/null +++ b/updates/2.sql @@ -0,0 +1,2 @@ +-- drop view +DROP VIEW public."LogView"; diff --git a/web/src/App.vue b/web/src/App.vue index cf7531e..b31f3e0 100644 --- a/web/src/App.vue +++ b/web/src/App.vue @@ -1,9 +1,18 @@