Added comments

This commit is contained in:
2025-06-09 09:48:31 +02:00
parent 0c76b852eb
commit 8b673c67d3
7 changed files with 40 additions and 1 deletions
+27
View File
@@ -10,6 +10,7 @@ const messages = require("../config/messages.conf.js");
const express = require('express');
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) {
res.render("error", {
page_title: app_conf.name,
@@ -22,11 +23,14 @@ function errorPageRenderer(res, emoji, message) {
})
}
// Remove SQL row endpoint
router.delete("/remove", async (req, res) => {
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
// Get the row Name as request parameter
if (req.query["element"]) {
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"]}';`);
if (remove.run()["changes"]) {
res.status(200).send();
@@ -42,21 +46,26 @@ router.delete("/remove", async (req, res) => {
}
});
// Upload SQL database
router.post("/upload", async (req, res) => {
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
// Check if file received
if (!req.files || !req.files.file) {
res.status(422).send();
} else {
const uploadedFile = req.files.file;
// Close current database
if (db.getDB()) {
db.getDB().close();
}
// Overwrite current database file with the new one
fs.copyFile(uploadedFile.tempFilePath, db_conf.path, (error) => {
if (error) {
logger.error(error);
} else {
// Create new database (connect to db) object and store it
try {
db.setDB(new Database(db_conf.path, { fileMustExist: true }));
} catch (error) {
@@ -68,17 +77,21 @@ router.post("/upload", async (req, res) => {
res.status(201).send();
});
// Download current database file
router.get("/download", async (req, res) => {
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
res.download(db_conf.path);
});
// Add row to database
router.post("/add", async (req, res) => {
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
// Check if row name and row data received
if (req.query["element"] && req.body) {
try {
// Constructing and executing SQL statement
let keys = Object.keys(req.body);
let values = `'${req.query["element"]}',`;
@@ -107,11 +120,14 @@ router.post("/add", async (req, res) => {
}
});
// Edit SQL row
router.post("/edit", async (req, res) => {
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
// Check if row name and row data received
if (req.query["element"] && req.body) {
try {
// Constructing and executing SQL statement
let keys = Object.keys(req.body);
let values = "";
@@ -140,10 +156,12 @@ router.post("/edit", async (req, res) => {
}
});
// Add row to database page renderer
router.get("/add", async (req, res) => {
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
try {
// Get all SQL columns to create HTML table
const select_get = db.getDB().prepare(`SELECT * FROM ${db_conf.table_name};`);
const row = select_get.get();
@@ -151,6 +169,10 @@ router.get("/add", async (req, res) => {
var table_header = `<th>${sql_header[0]}</th>\n`;
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) {
var column_options = {};
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) => {
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"]) {
try {
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) => {
logger.info(`${req.method}: "${req.url}" => ${req.get("User-Agent")}`);
try {
// Get all rows from SQL and creating HTML table
const select = db.getDB().prepare(`SELECT * FROM ${db_conf.table_name}`);
const rows = select.all();
@@ -315,6 +341,7 @@ router.get("/", async (req, res) => {
}
});
// About page for github
router.get("/about", (req, res) => {
res.render("about", {
page_title: app_conf.name,
+2 -1
View File
@@ -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)));
/*
* Main endpoint - might implement authentication step here...
* Main endpoint - authentication happens here
* Rewritting the whole url to fix logout bug
*/
router.get("/", (req, res) => {
res.redirect(`${app_conf.protocol}://${app_conf.host}:${app_conf.port}${app_conf.app_url}`);