diff options
author | Joel Kronqvist <joelkronqvist@proton.me> | 2023-06-21 16:47:34 +0000 |
---|---|---|
committer | Joel Kronqvist <joelkronqvist@proton.me> | 2023-06-21 16:47:34 +0000 |
commit | e4bd0977da3621a8d3f82cab949a6734b169b71e (patch) | |
tree | 92f8079047b5d6bc951cb1eee02c3d2ceca339f3 | |
parent | c6eb85c138c267db08103de28a3a7c6dffee6c4f (diff) | |
download | LYLLRuoka-e4bd0977da3621a8d3f82cab949a6734b169b71e.tar.gz LYLLRuoka-e4bd0977da3621a8d3f82cab949a6734b169b71e.zip |
Exception updation & minor polishing (but it's still WIP)
-rw-r--r-- | Cont/404/index.html | 2 | ||||
-rw-r--r-- | Cont/custom-message/index.html | 2 | ||||
-rw-r--r-- | Cont/devs/index.html | 2 | ||||
-rw-r--r-- | Cont/index.html | 3 | ||||
-rw-r--r-- | Cont/panel/index.html (renamed from Cont/updation/index.html) | 12 | ||||
-rw-r--r-- | parseExceptions.js | 7 | ||||
-rw-r--r-- | server.js | 85 | ||||
-rwxr-xr-x | setup.sh | 2 | ||||
-rw-r--r-- | update.js | 7 |
9 files changed, 86 insertions, 36 deletions
diff --git a/Cont/404/index.html b/Cont/404/index.html index 6b82d01..ce0ddda 100644 --- a/Cont/404/index.html +++ b/Cont/404/index.html @@ -20,6 +20,8 @@ <footer> <a href="https://www.github.com/JoelHMikael/FoodJS">Lähdekoodi</a> <span>|</span> + <a href="/panel">Hallintapaneeli</a> + <span>|</span> <a href="/devs">Ota yhteyttä</a> </footer> diff --git a/Cont/custom-message/index.html b/Cont/custom-message/index.html index 791513d..a4b728a 100644 --- a/Cont/custom-message/index.html +++ b/Cont/custom-message/index.html @@ -19,6 +19,8 @@ <footer> <a href="https://www.github.com/JoelHMikael/FoodJS">Lähdekoodi</a> <span>|</span> + <a href="/panel">Hallintapaneeli</a> + <span>|</span> <a href="/devs">Ota yhteyttä</a> </footer> diff --git a/Cont/devs/index.html b/Cont/devs/index.html index a4abbdb..667f539 100644 --- a/Cont/devs/index.html +++ b/Cont/devs/index.html @@ -20,6 +20,8 @@ <footer> <a href="https://www.github.com/JoelHMikael/FoodJS">Lähdekoodi</a> <span>|</span> + <a href="/panel">Hallintapaneeli</a> + <span>|</span> <a href="/devs">Ota yhteyttä</a> </footer> diff --git a/Cont/index.html b/Cont/index.html index edd72f5..10a0928 100644 --- a/Cont/index.html +++ b/Cont/index.html @@ -37,7 +37,6 @@ <br> <input type="submit" id="send" class="highlight" value="Löydä vuoro"> <br> - <a href="/updation" id="updationlink">Päivitä ruoat</a> </form> <br> @@ -62,6 +61,8 @@ <footer> <a href="https://www.github.com/JoelHMikael/LYLLRuoka">Lähdekoodi</a> <span>|</span> + <a href="/panel">Hallintapaneeli</a> + <span>|</span> <a href="/devs">Ota yhteyttä</a> </footer> </body> diff --git a/Cont/updation/index.html b/Cont/panel/index.html index 49167bc..7715e9a 100644 --- a/Cont/updation/index.html +++ b/Cont/panel/index.html @@ -18,13 +18,15 @@ <form method="POST"> <p class=shadow>Liitä allaolevaan tekstikenttään ruokailuvuorot (Wilma-viestin teksti):</p> <br> - <textarea name="shifts" rows="24" style="text-align: left;"></textarea> - <br> - <p class="shadow">Kiitos paljon! Kun painat sivun lopusta 'Päivitä'-painiketta, toimii ruokailuvuorohaku (toivon mukaan) jälleen. Jos sinulla on yhtään enemmän aikaa, voit myös suorittaa allaolevan vaiheen. Sen suoritettuasi ruokailuvuoroja voi hakea opettajan ja kurssikoodin lisäksi luokkatilan perusteella.</p> + <textarea name="shifts" rows="24" style="text-align: left;">\(shifts\)</textarea> <br> <p class="shadow">Liitä allaolevaan tekstikenttään kurssitarjottimista tämä jakso. Voit avata kurssitarjottimet Excelissä tai Libreoffice Calcissa, valita alueen, jossa on jakson kurssit ja CTRL+C CTRL+V tähän. Toista sama kaikille jakson alueille kaikista vuoden kurssitarjottimista.</p> <br> - <textarea name="classes" rows="24" style="text-align: left;"></textarea> + <textarea name="classes" rows="24" style="text-align: left;">\(classes\)</textarea> + <br> + <br> + <p class="shadow">Alta voit muokata ilmoituksia.</p> + <textarea name="exceptions" rows="16" style="text-align: left;">\(exceptions\)</textarea> <br> <input type="submit" id="send" class="highlight" value="Päivitä"> <br> @@ -35,6 +37,8 @@ <footer> <a href="https://www.github.com/JoelHMikael/LYLLRuoka">Lähdekoodi</a> <span>|</span> + <a href="/panel">Hallintapaneeli</a> + <span>|</span> <a href="/devs">Ota yhteyttä</a> </footer> </body> diff --git a/parseExceptions.js b/parseExceptions.js index 63cd6d2..42a1c50 100644 --- a/parseExceptions.js +++ b/parseExceptions.js @@ -50,9 +50,10 @@ assert.deepEqual( async function updateExceptions(exceptions, DB) { await DB.query_raw('DELETE FROM exceptions'); let dbOperations = []; - for (line of exceptions.split('\n')) { - if (line === '' || line[0] === '#') - continue + for (let line of exceptions.split('\n')) { + if ((line === '') || (line === '\r') || (line[0] === '#')) { + continue; + } const [start, end, header, message] = parseLine(line); dbOperations.push(DB.execute( 'INSERT INTO exceptions VALUE (?, ?, ?, ?)', @@ -2,6 +2,7 @@ const https = require("https"); const http = require("http"); const url = require("url"); const food = require("./food.js"); +const fs = require("node:fs/promises"); const SQL_DBS = require("./database.js"); const DBPARSE = require("./dbparse.js"); const open = require("./Functions/open.js"); @@ -9,13 +10,16 @@ const strFuncs = require("./Functions/stringFuncs.js"); const dateFuncs = require("./Functions/dateFuncs.js"); const updateDB = require("./update.js"); +const SHIFTPATH = "../Updation/shifts.txt"; +const CLASSPATH = "../Updation/classes.txt"; +const EXCEPTIONPATH = "../Updation/exceptions.txt"; async function init() { const build = { "./Cont/index.html": buildMain, "./Cont/index.css": buildDefault, - "./Cont/updation/index.html": buildDefault, + "./Cont/panel/index.html": buildPanel, "./Cont/devs/index.html": buildDevs, "./Cont/devs/index.css": buildDefault, "./Cont/404/index.css": buildDefault, @@ -25,8 +29,6 @@ async function init() "./Cont/Images/favicon.ico": buildImage, }; const errorPath = "./Cont/404/index.html"; - const SHIFTPATH = "../Updation/shifts.txt"; - const CLASSPATH = "../Updation/classes.txt"; const startDate = new Date(); let visitorCount = 0; @@ -50,7 +52,7 @@ async function init() // Update... // ...shifts and classes - await updateDB.update(SQLDB, SHIFTPATH, CLASSPATH); + await updateDB.update(SQLDB, SHIFTPATH, CLASSPATH, EXCEPTIONPATH); // ...foods dateFuncs.run_at_monday_mornings(() => food.build(SQLDB)); if ((new Date()).getDay() !== 1) // update if it's not monday. if it's monday, it has already been run by the scheduler above. @@ -76,36 +78,51 @@ async function init() let shifts = ""; let classes = ""; try { - shifts = decodeURIComponent(q.get("shifts")).replaceAll("+", " "); - classes = decodeURIComponent(q.get("classes")).replaceAll("+", " "); + shifts = decodeURIComponent(q.get("shifts")).replaceAll("+", " ").replaceAll('\r\n', '\n'); + classes = decodeURIComponent(q.get("classes")).replaceAll("+", " ").replaceAll('\r\n', '\n'); + exceptions = decodeURIComponent(q.get('exceptions').replaceAll("+", ' ')).replaceAll('\r\n', '\n'); } catch { console.log("Malformed url, presumably"); res.writeHead(400); - res.end(); + const cont = await buildCustomMessage("400: Virheellinen pyyntö", "Pyyntö sisälsi todennäköisesti merkkikoodeja, jotka eivät viittaa mihinkään olemassaolevaan merkkiin."); + res.end(cont); + return; } if (shifts === null || classes === null) { res.writeHead(400); - res.end("Avainta 'shifts' ja/tai 'classes' ei löytynyt pyynnöstä."); + const cont = await buildCustomMessage("400: Virheellinen pyyntö", "Avaimia 'shifts' ja/tai 'classes' ei löytynyt pyynnöstä"); + res.end(cont); + return; } - let shiftfile = await fs.open(SHIFTPATH, "w"); + let shiftfile = await fs.open(`${SHIFTPATH}.tmp`, "w"); await shiftfile.write(shifts); shiftfile.close(); - if (classes != "") { - let classfile = await fs.open(CLASSPATH, "w"); - await classfile.write(classes) - classfile.close(); - } + + let classfile = await fs.open(`${CLASSPATH}.tmp`, "w"); + await classfile.write(classes) + classfile.close(); + + let exceptionfile = await fs.open(`${EXCEPTIONPATH}.tmp`, "w"); + await exceptionfile.write(exceptions); + exceptionfile.close(); try { - await updateDB.update(SQLDB, SHIFTPATH, CLASSPATH); + await updateDB.update(SQLDB, `${SHIFTPATH}.tmp`, `${CLASSPATH}.tmp`, `${EXCEPTIONPATH}.tmp`); + await Promise.all([ + fs.rename(`${SHIFTPATH}.tmp`, SHIFTPATH), + fs.rename(`${CLASSPATH}.tmp`, CLASSPATH), + fs.rename(`${EXCEPTIONPATH}.tmp`, EXCEPTIONPATH) + ]); + res.writeHead(200); + const cont = await buildCustomMessage("Kiitos!", "Päivitysprosessi näyttää onnistuneen."); + res.end(cont); } catch(e) { - res.writeHead(400); - res.end("Virhe tietojen käsittelyssä. Ota yhteys kehittäjään."); console.log(e); + res.writeHead(400); + const cont = await buildCustomMessage("500: Virhe palvelimella", "Ruokailuvuorojen päivitysprosessi palautti virheen. Jos ruokailuvuorot on kirjoitettu jotenkin eri lailla kuin edellisissä viesteissä, selittynee virhe sillä. Jos pienellä muokkauksella et saa päivitystä toimimaan, ota yhteys kehittäjään."); + res.end(cont); return; } - res.writeHead(200); - res.end("Kiitos paljon! Näyttää siltä, että tiedot saatiin päivitettyä."); }); return; } @@ -287,8 +304,8 @@ async function buildMain(args) res["shift"] = "Kurssilla/opettajalla/luokalla ei ole ruokailua päivällä tai kurssia ei ole olemassa!"; // Show message if the normal schedule isn't in place - const examInfo = await SQLDB.query("SELECT * FROM exams"); - for(let week = 0; week < examInfo.length; week++) + const exceptionInfo = await SQLDB.query("SELECT * FROM exceptions"); + for(let week = 0; week < exceptionInfo.length; week++) { // get the date of the requested day const nextDate = new Date( @@ -299,11 +316,11 @@ async function buildMain(args) if (dateFuncs.between( nextDate, - new Date(examInfo[week].start), - new Date(examInfo[week].end) + new Date(exceptionInfo[week].start), + new Date(exceptionInfo[week].end) )) { - const message = `<div class="shift-result float-block">${examInfo[week].message}</div>`; + const message = `<div class="shift-result float-block"><h2>${exceptionInfo[week].header}</h2>${(exceptionInfo[week].message.length > 0) ? '<br>' : ''}${exceptionInfo[week].message}</div>`; data_string = strFuncs.replaceElement(data_string, "div id=\"shift-result\" class=\"float-block\"", message); } } @@ -382,6 +399,26 @@ async function buildCustomMessage(header, message) { return data; } +async function buildPanel(args) +{ + let data = await open.file(args["path"]); + data = data.toString("utf-8"); + + let shifts = await open.file(SHIFTPATH); + shifts = shifts.toString("utf-8"); + + let classes = await open.file(CLASSPATH); + classes = classes.toString("utf-8"); + + let exceptions = await open.file(EXCEPTIONPATH); + exceptions = exceptions.toString("utf-8"); + return build_replace(data, { + "shifts": shifts, + "classes": classes, + "exceptions": exceptions + }); +} + async function build404(args) { args["path"] = args["path"].substring("./Cont".length); @@ -89,7 +89,7 @@ if [ "$certans" != "n" ]; then fi echo 'Setting up MySQL database...' -echo -e "CREATE DATABASE lyllruoka;\nUSE lyllruoka;\n\nCREATE TABLE shiftnames (\n\tday INT,\n\tid INT,\n\tname VARCHAR(128) NOT NULL,\n\tPRIMARY KEY (day, id)\n);\nCREATE TABLE classes (\n\tcourse VARCHAR(6) PRIMARY KEY,\n\tclass VARCHAR(4)\n);\nCREATE TABLE shifts (\n\tday INT,\n\tshift INT,\n\tcourse VARCHAR(6),\n\tteacher VARCHAR(4),\n\tclass VARCHAR(4),\n\tPRIMARY KEY (day, course)\n);\nCREATE TABLE devs (\n\tid INT PRIMARY KEY AUTO_INCREMENT,\n\tname VARCHAR(30) NOT NULL,\n\tdescription VARCHAR(128),\n\tcontact VARCHAR(40) DEFAULT ''\n);\nCREATE TABLE stats (\n start DATE PRIMARY KEY,\n uptime INT,\n requests INT,\n requests_per_day INT\n);\nCREATE TABLE exams (\n\tstart DATE,\n\tend DATE,\n\tmessage VARCHAR(256),\n\tPRIMARY KEY (start, end)\n);\nCREATE TABLE foods ( \n week INT,\n day INT,\n vegetarian TINYINT,\n header VARCHAR(15),\n dateString VARCHAR(13),\n food VARCHAR(280)\n);\nCREATE USER '$name'@'localhost' IDENTIFIED BY '$passw';\nGRANT ALL ON lyllruoka.* TO '$name'@'localhost';\n" | sudo mysql > /dev/null +echo -e "CREATE DATABASE lyllruoka;\nUSE lyllruoka;\n\nCREATE TABLE shiftnames (\n\tday INT,\n\tid INT,\n\tname VARCHAR(128) NOT NULL,\n\tPRIMARY KEY (day, id)\n);\nCREATE TABLE classes (\n\tcourse VARCHAR(6) PRIMARY KEY,\n\tclass VARCHAR(4)\n);\nCREATE TABLE shifts (\n\tday INT,\n\tshift INT,\n\tcourse VARCHAR(6),\n\tteacher VARCHAR(4),\n\tclass VARCHAR(4),\n\tPRIMARY KEY (day, course)\n);\nCREATE TABLE devs (\n\tid INT PRIMARY KEY AUTO_INCREMENT,\n\tname VARCHAR(30) NOT NULL,\n\tdescription VARCHAR(128),\n\tcontact VARCHAR(40) DEFAULT ''\n);\nCREATE TABLE stats (\n start DATE PRIMARY KEY,\n uptime INT,\n requests INT,\n requests_per_day INT\n);\nCREATE TABLE exceptions (\n\tstart DATE,\n\tend DATE,\n\theader VARCHAR(64),\n\tmessage VARCHAR(256),\n\tPRIMARY KEY (start, end)\n);\nCREATE TABLE foods ( \n week INT,\n day INT,\n vegetarian TINYINT,\n header VARCHAR(15),\n dateString VARCHAR(13),\n food VARCHAR(256)\n);\nCREATE USER '$name'@'localhost' IDENTIFIED BY '$passw';\nGRANT ALL ON lyllruoka.* TO '$name'@'localhost';\n" | sudo mysql > /dev/null echo -e "{\n\t\"host\": \"localhost\",\n\t\"user\": \"$name\",\n\t\"password\": \"$passw\",\n\t\"database\": \"lyllruoka\"\n}" > dblogin.txt echo -e 'Done!\n' @@ -1,6 +1,7 @@ -const openFile = require("./Functions/open.js").file; -const parseClasses = require("./parseClasses.js").classes; -const parse = require("./dbparse.js"); +const openFile = require("./Functions/open.js").file; +const parseClasses = require("./parseClasses.js").classes; +const parse = require("./dbparse.js"); +const updateExceptions = require('./parseExceptions.js').updateExceptions; // Run this if you want to build the database from text files async function buildDB(dbconnection, shiftPath, classPath, exceptionPath) { |