giving more data to client to allow for later management

This commit is contained in:
Timo Hocker 2019-09-26 15:59:24 +02:00
parent 078ad18862
commit b6bccfb07f
3 changed files with 44 additions and 24 deletions

View File

@ -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
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 {
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);
}
}
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();
}

2
updates/2.sql Normal file
View File

@ -0,0 +1,2 @@
-- drop view
DROP VIEW public."LogView";

View File

@ -1,9 +1,18 @@
<template>
<div id="app">
<div>
<table>
<tr v-for="(row,rowKey) in json" :key="rowKey">
<td v-for="(value,key) in row" :key="key">{{value}}</td>
<table v-if="json.headings && json.data">
<tr>
<th
v-for="(value, key) in json.headings.filter((val,ind)=>!json.hidden.includes(ind))"
:key="key"
>{{value}}</th>
</tr>
<tr v-for="(row,rowKey) in json.data" :key="rowKey">
<td
v-for="(value,key) in row.filter((val,ind)=>!json.hidden.includes(ind))"
:key="key"
>{{value}}</td>
</tr>
</table>
</div>
@ -22,7 +31,7 @@ export default {
name: "App",
data() {
return {
json: []
json: {}
};
},
mounted() {
@ -30,11 +39,6 @@ export default {
fetch("?json")
.then(res => res.json())
.then(json => (self.json = json));
},
watch: {
json() {
console.log(this.json);
}
}
};
</script>
@ -47,6 +51,10 @@ td {
padding: 5px;
}
th {
padding: 5px;
}
table {
border-collapse: collapse;
}