From 4b3c304ee8a31bb20dc487b62f3d85350761a2a7 Mon Sep 17 00:00:00 2001 From: Maedi Prichard Date: Wed, 2 Dec 2020 00:36:23 +1100 Subject: [PATCH] Update server.js --- lib/web/server.js | 94 +++++++++++++++++++++++------------------------ 1 file changed, 45 insertions(+), 49 deletions(-) diff --git a/lib/web/server.js b/lib/web/server.js index 79b4faa..b0bff99 100644 --- a/lib/web/server.js +++ b/lib/web/server.js @@ -2,8 +2,9 @@ const express = require('express') const path = require('path') const vm = require("vm"); const fs = require('fs'); - const app = express(); + +// Config. const port = 3001; const db_path = 'db.js'; @@ -18,7 +19,7 @@ app.get('/', (req, res) => { }) //// -// READ. +// READ //// var load_db = () => { @@ -26,97 +27,92 @@ var load_db = () => { const data = fs.readFileSync(db_path); const script = new vm.Script(data); script.runInThisContext(); + return JSON.parse(db); } +//// +// WRITE +//// + +var save_db = (db) => { + // Save JS file to disk. + try { + fs.writeFileSync(db_path, 'var db = ' + JSON.stringify(JSON.stringify(db)) + ';'); + } + catch (err) { + console.log("ERROR: save_db() failed to save."); + console.error(err); + } + +} + //// // DELETE. //// -// Handle executions delete request. -app.post('/executions/delete', (req, res) => { +// Handle controls delete request. +app.post('/controls/delete', (req, res) => { - // Get reflection ID. + // Get execution ID that controls share. var exe_id = req.body.exe_id; - var number = req.body.number; - console.log(number); // Get database. db = load_db(); - for (let [index, reflection] of db.reflections.entries()) { - if (reflection.e == exe_id && reflection.n == number) { - console.log("DELETED:") - console.log(db.reflections[index]); - db.reflections.splice(index, 1); + // Delete controls. + for (let [index, control] of db.controls.entries()) { + if (control.exe_id == exe_id) { + console.log("DELETE CONTROL:"); + console.log(db.controls[index]); + db.controls.splice(index, 1); } - if (reflection.b != null && reflection.b == exe_id && reflection.n == number) { - console.log("DELETED:") - console.log(db.reflections[index]); - db.reflections.splice(index, 1); + if (control.base_id != null && control.base_id == exe_id) { + console.log("DELETE:"); + console.log(db.controls[index]); + db.controls.splice(index, 1); } } // Save database. - try { - fs.writeFileSync(db_path, 'var db = ' + JSON.stringify(JSON.stringify(db)) + ';'); - } - catch (err) { - console.error(err) - } + save_db(db) res.status(200).send({ message: 'ok' }); }) -// Handle reflections delete request. -app.post('/reflections/delete', (req, res) => { +// Handle control delete request. +app.post('/control/delete', (req, res) => { - // Get reflection ID. + // Get control ID. ref_id = req.body.ref_id; // Get database. db = load_db(); - // Delete reflection. - for (let [index, reflection] of db.reflections.entries()) { - if (reflection.r == ref_id) { - console.log("DELETED:") - console.log(db.reflections[index]); - db.reflections.splice(index, 1); + // Delete control. + for (let [index, control] of db.controls.entries()) { + if (control.r == ref_id) { + console.log("DELETE CONTROL:") + console.log(db.controls[index]); + db.controls.splice(index, 1); break; } } // Save database. - try { - fs.writeFileSync(db_path, 'var db = ' + JSON.stringify(JSON.stringify(db)) + ';'); - } - catch (err) { - console.error(err) - } + save_db(db) res.status(200).send({ message: 'ok' }); }) //// -// UPDATE. +// SERVE //// -// Handle reflections keep request. -app.post('/reflections/keep', (req, res) => { - console.log(req.body); -}) - // Listen for requests. app.listen(port, () => { console.log(`Example app listening at http://localhost:${port}`) }) - -//fs.readFile('student.json', (err, data) => { -// if (err) throw err; -// let student = JSON.parse(data); -// console.log(student); -//});