Added comments
This commit is contained in:
@@ -2,8 +2,10 @@ const Database = require("better-sqlite3");
|
|||||||
const logger = require("../utils/logger.util");
|
const logger = require("../utils/logger.util");
|
||||||
const db_conf = require("../config/db.conf");
|
const db_conf = require("../config/db.conf");
|
||||||
|
|
||||||
|
// Holds the database class object
|
||||||
var db;
|
var db;
|
||||||
|
|
||||||
|
// Connect to SQLite database
|
||||||
function connect() {
|
function connect() {
|
||||||
try {
|
try {
|
||||||
logger.info("Connecting to Database...");
|
logger.info("Connecting to Database...");
|
||||||
@@ -14,10 +16,12 @@ function connect() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Database object getter
|
||||||
function getDB() {
|
function getDB() {
|
||||||
return db;
|
return db;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Database object setter
|
||||||
function setDB(newDb) {
|
function setDB(newDb) {
|
||||||
db = newDb;
|
db = newDb;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ const messages = require("../config/messages.conf.js");
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
|
// Gets an Express response object an emoji code and a string message and renders as error message
|
||||||
function errorPageRenderer(res, emoji, message) {
|
function errorPageRenderer(res, emoji, message) {
|
||||||
res.render("error", {
|
res.render("error", {
|
||||||
page_title: app_conf.name,
|
page_title: app_conf.name,
|
||||||
@@ -22,11 +23,14 @@ function errorPageRenderer(res, emoji, message) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remove SQL row endpoint
|
||||||
router.delete("/remove", async (req, res) => {
|
router.delete("/remove", async (req, res) => {
|
||||||
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
|
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
|
||||||
|
|
||||||
|
// Get the row Name as request parameter
|
||||||
if (req.query["element"]) {
|
if (req.query["element"]) {
|
||||||
try {
|
try {
|
||||||
|
// Create and run SQL statement to remove the row
|
||||||
const remove = db.getDB().prepare(`DELETE FROM ${db_conf.table_name} WHERE Name='${req.query["element"]}';`);
|
const remove = db.getDB().prepare(`DELETE FROM ${db_conf.table_name} WHERE Name='${req.query["element"]}';`);
|
||||||
if (remove.run()["changes"]) {
|
if (remove.run()["changes"]) {
|
||||||
res.status(200).send();
|
res.status(200).send();
|
||||||
@@ -42,21 +46,26 @@ router.delete("/remove", async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Upload SQL database
|
||||||
router.post("/upload", async (req, res) => {
|
router.post("/upload", async (req, res) => {
|
||||||
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
|
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
|
||||||
|
|
||||||
|
// Check if file received
|
||||||
if (!req.files || !req.files.file) {
|
if (!req.files || !req.files.file) {
|
||||||
res.status(422).send();
|
res.status(422).send();
|
||||||
} else {
|
} else {
|
||||||
const uploadedFile = req.files.file;
|
const uploadedFile = req.files.file;
|
||||||
|
|
||||||
|
// Close current database
|
||||||
if (db.getDB()) {
|
if (db.getDB()) {
|
||||||
db.getDB().close();
|
db.getDB().close();
|
||||||
}
|
}
|
||||||
|
// Overwrite current database file with the new one
|
||||||
fs.copyFile(uploadedFile.tempFilePath, db_conf.path, (error) => {
|
fs.copyFile(uploadedFile.tempFilePath, db_conf.path, (error) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
logger.error(error);
|
logger.error(error);
|
||||||
} else {
|
} else {
|
||||||
|
// Create new database (connect to db) object and store it
|
||||||
try {
|
try {
|
||||||
db.setDB(new Database(db_conf.path, { fileMustExist: true }));
|
db.setDB(new Database(db_conf.path, { fileMustExist: true }));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -68,17 +77,21 @@ router.post("/upload", async (req, res) => {
|
|||||||
res.status(201).send();
|
res.status(201).send();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Download current database file
|
||||||
router.get("/download", async (req, res) => {
|
router.get("/download", async (req, res) => {
|
||||||
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
|
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
|
||||||
|
|
||||||
res.download(db_conf.path);
|
res.download(db_conf.path);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Add row to database
|
||||||
router.post("/add", async (req, res) => {
|
router.post("/add", async (req, res) => {
|
||||||
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
|
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
|
||||||
|
|
||||||
|
// Check if row name and row data received
|
||||||
if (req.query["element"] && req.body) {
|
if (req.query["element"] && req.body) {
|
||||||
try {
|
try {
|
||||||
|
// Constructing and executing SQL statement
|
||||||
let keys = Object.keys(req.body);
|
let keys = Object.keys(req.body);
|
||||||
let values = `'${req.query["element"]}',`;
|
let values = `'${req.query["element"]}',`;
|
||||||
|
|
||||||
@@ -107,11 +120,14 @@ router.post("/add", async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Edit SQL row
|
||||||
router.post("/edit", async (req, res) => {
|
router.post("/edit", async (req, res) => {
|
||||||
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
|
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
|
||||||
|
|
||||||
|
// Check if row name and row data received
|
||||||
if (req.query["element"] && req.body) {
|
if (req.query["element"] && req.body) {
|
||||||
try {
|
try {
|
||||||
|
// Constructing and executing SQL statement
|
||||||
let keys = Object.keys(req.body);
|
let keys = Object.keys(req.body);
|
||||||
let values = "";
|
let values = "";
|
||||||
|
|
||||||
@@ -140,10 +156,12 @@ router.post("/edit", async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Add row to database page renderer
|
||||||
router.get("/add", async (req, res) => {
|
router.get("/add", async (req, res) => {
|
||||||
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
|
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Get all SQL columns to create HTML table
|
||||||
const select_get = db.getDB().prepare(`SELECT * FROM ${db_conf.table_name};`);
|
const select_get = db.getDB().prepare(`SELECT * FROM ${db_conf.table_name};`);
|
||||||
const row = select_get.get();
|
const row = select_get.get();
|
||||||
|
|
||||||
@@ -151,6 +169,10 @@ router.get("/add", async (req, res) => {
|
|||||||
var table_header = `<th>${sql_header[0]}</th>\n`;
|
var table_header = `<th>${sql_header[0]}</th>\n`;
|
||||||
sql_header.splice(0, 1);
|
sql_header.splice(0, 1);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Getting all distinct values for each column except the value from the
|
||||||
|
* current column to add as options to the HTML selects.
|
||||||
|
*/
|
||||||
if (row) {
|
if (row) {
|
||||||
var column_options = {};
|
var column_options = {};
|
||||||
for (const key of sql_header) {
|
for (const key of sql_header) {
|
||||||
@@ -199,9 +221,11 @@ router.get("/add", async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Edit SQL row page
|
||||||
router.get("/edit", async (req, res) => {
|
router.get("/edit", async (req, res) => {
|
||||||
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
|
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
|
||||||
|
|
||||||
|
// Same as ADD GET REQUEST but Name is constant as it identifies the row
|
||||||
if (req.query["element"]) {
|
if (req.query["element"]) {
|
||||||
try {
|
try {
|
||||||
const select_get = db.getDB().prepare(`SELECT * FROM ${db_conf.table_name} WHERE Name='${req.query["element"]}';`);
|
const select_get = db.getDB().prepare(`SELECT * FROM ${db_conf.table_name} WHERE Name='${req.query["element"]}';`);
|
||||||
@@ -270,10 +294,12 @@ router.get("/edit", async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Main page
|
||||||
router.get("/", async (req, res) => {
|
router.get("/", async (req, res) => {
|
||||||
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
|
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Get all rows from SQL and creating HTML table
|
||||||
const select = db.getDB().prepare(`SELECT * FROM ${db_conf.table_name}`);
|
const select = db.getDB().prepare(`SELECT * FROM ${db_conf.table_name}`);
|
||||||
const rows = select.all();
|
const rows = select.all();
|
||||||
|
|
||||||
@@ -315,6 +341,7 @@ router.get("/", async (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// About page for github
|
||||||
router.get("/about", (req, res) => {
|
router.get("/about", (req, res) => {
|
||||||
res.render("about", {
|
res.render("about", {
|
||||||
page_title: app_conf.name,
|
page_title: app_conf.name,
|
||||||
|
|||||||
@@ -8,7 +8,8 @@ router.use(app_conf.paths.css.web, express.static(path.join(path.dirname(require
|
|||||||
router.use(app_conf.paths.js.web, express.static(path.join(path.dirname(require.main.filename), app_conf.paths.js.src)));
|
router.use(app_conf.paths.js.web, express.static(path.join(path.dirname(require.main.filename), app_conf.paths.js.src)));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Main endpoint - might implement authentication step here...
|
* Main endpoint - authentication happens here
|
||||||
|
* Rewritting the whole url to fix logout bug
|
||||||
*/
|
*/
|
||||||
router.get("/", (req, res) => {
|
router.get("/", (req, res) => {
|
||||||
res.redirect(`${app_conf.protocol}://${app_conf.host}:${app_conf.port}${app_conf.app_url}`);
|
res.redirect(`${app_conf.protocol}://${app_conf.host}:${app_conf.port}${app_conf.app_url}`);
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ var shutting_down = false;
|
|||||||
* Static middleware - serve css and js files to public
|
* Static middleware - serve css and js files to public
|
||||||
*/
|
*/
|
||||||
app.use(express.json());
|
app.use(express.json());
|
||||||
|
// Set temp directory and upload size limit
|
||||||
app.use(fileUpload({
|
app.use(fileUpload({
|
||||||
limits: { fileSize: 102400 },
|
limits: { fileSize: 102400 },
|
||||||
useTempFiles: true,
|
useTempFiles: true,
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ commit.ondblclick = async function () {
|
|||||||
if (sql_row[1].cells[0].firstChild.value) {
|
if (sql_row[1].cells[0].firstChild.value) {
|
||||||
let req_body = {};
|
let req_body = {};
|
||||||
|
|
||||||
|
// Constructing request body by getting the values of each HTML select
|
||||||
for (let i = 1; i < sql_row[1].cells.length; i++) {
|
for (let i = 1; i < sql_row[1].cells.length; i++) {
|
||||||
req_body[sql_row[0].cells[i].firstChild.textContent] = sql_row[1].cells[i].firstChild[sql_row[1].cells[i].firstChild.selectedIndex].text;
|
req_body[sql_row[0].cells[i].firstChild.textContent] = sql_row[1].cells[i].firstChild[sql_row[1].cells[i].firstChild.selectedIndex].text;
|
||||||
}
|
}
|
||||||
@@ -41,8 +42,10 @@ commit.ondblclick = async function () {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Parsing distinct SQL values from backend
|
||||||
column_options = JSON.parse(column_options);
|
column_options = JSON.parse(column_options);
|
||||||
|
|
||||||
|
// Adding options to HTML selects
|
||||||
for (let i = 1; i < sql_row[1].cells.length; i++) {
|
for (let i = 1; i < sql_row[1].cells.length; i++) {
|
||||||
let opts = column_options[sql_row[0].cells[i].textContent];
|
let opts = column_options[sql_row[0].cells[i].textContent];
|
||||||
|
|
||||||
@@ -52,6 +55,7 @@ for (let i = 1; i < sql_row[1].cells.length; i++) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// On right click add custom value - custom value is always the last one in HTML select
|
||||||
sql_row[1].cells[i].firstChild.oncontextmenu = async function (ev) {
|
sql_row[1].cells[i].firstChild.oncontextmenu = async function (ev) {
|
||||||
ev.preventDefault();
|
ev.preventDefault();
|
||||||
let new_value = prompt("Enter a new value");
|
let new_value = prompt("Enter a new value");
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ setEventListeners(commit, async () => {
|
|||||||
if (changes) {
|
if (changes) {
|
||||||
let req_body = {};
|
let req_body = {};
|
||||||
|
|
||||||
|
// Constructing request body by adding ONLY updated values
|
||||||
for (let i = 1; i < sql_row[1].cells.length; i++) {
|
for (let i = 1; i < sql_row[1].cells.length; i++) {
|
||||||
if (sql_row[1].cells[i].firstChild.selectedIndex != 0) {
|
if (sql_row[1].cells[i].firstChild.selectedIndex != 0) {
|
||||||
req_body[sql_row[0].cells[i].firstChild.textContent] = sql_row[1].cells[i].firstChild[sql_row[1].cells[i].firstChild.selectedIndex].text;
|
req_body[sql_row[0].cells[i].firstChild.textContent] = sql_row[1].cells[i].firstChild[sql_row[1].cells[i].firstChild.selectedIndex].text;
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ function setEventListeners(element, cb) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Upload SQL database
|
||||||
setEventListeners(upload, async () => {
|
setEventListeners(upload, async () => {
|
||||||
const input = document.getElementById("file_input");
|
const input = document.getElementById("file_input");
|
||||||
const upload = (file) => {
|
const upload = (file) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user