From 21e79dcc1eabebfd661a2fd38681348ae9428c4e Mon Sep 17 00:00:00 2001 From: JoelHMikael Date: Sun, 9 Jan 2022 20:17:16 +0200 Subject: Database implemented Some polishing wouldn't do bad though... --- server.js | 74 +++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 43 insertions(+), 31 deletions(-) (limited to 'server.js') diff --git a/server.js b/server.js index 26ab9bb..d92a8eb 100644 --- a/server.js +++ b/server.js @@ -1,7 +1,6 @@ const http = require("http"); const fs = require("fs"); const url = require("url"); -const parse = require("./parse.js"); const scrape = require("./scrape.js"); const SQL_DBS = require("./database.js"); const DBPARSE = require("./dbparse.js"); @@ -23,10 +22,11 @@ async function init() }; const errorPath = "./Cont/404/index.html"; + + + // await for needed things in async - let [shiftCont, classCont, foodsThisWeek, foodsNextWeek, dbcredentials] = await Promise.all([ - openFile("./shifts.txt"), - openFile("./classes.txt"), + let [foodsThisWeek, foodsNextWeek, dbcredentials] = await Promise.all([ scrape.food(scrape.link(1)), scrape.food(scrape.link(2)), openFile("../dblogin.txt") @@ -35,16 +35,6 @@ async function init() // get the MySQL DB connection const SQLDB = new SQL_DBS.Database(JSON.parse(dbcredentials)); - // get the food shift "database" - shiftCont = shiftCont.toString("utf-8").replaceAll("\r", ""); // \r because of the \r\n newline on windows which creates problems - classCont = classCont.toString("utf-8").replaceAll("\r", ""); - - await DBPARSE.classes(classCont, SQLDB); - await DBPARSE.build(shiftCont, SQLDB); - - let DB = parse.build(shiftCont); - parse.classes(classCont, DB); - // get the food "database" const foods = [foodsThisWeek, foodsNextWeek]; @@ -77,7 +67,6 @@ async function init() "path": path, "path404": errorPath, "query": q.query, - "db": DB, "foods": foods, "sqldb": SQLDB }; @@ -108,7 +97,7 @@ async function init() function validateIndex(sus) { - return antiXSS(parse.cluttered(sus)); + return antiXSS(DBPARSE.cluttered(sus)); } function antiXSS(sus) @@ -120,7 +109,7 @@ function antiXSS(sus) function isDir(path) { - return (parse.getNextChar(path.substring(1), ".") === -1); + return (DBPARSE.getNextChar(path.substring(1), ".") === -1); } @@ -152,7 +141,7 @@ async function buildMain(args) const query = args["query"]; const foods = args["foods"]; const index = query.index; - const DB = args["db"]; + const SQLDB = args["sqldb"]; const data = await openFile(path); let data_string = data.toString("utf-8"); @@ -160,9 +149,10 @@ async function buildMain(args) const d = new Date(); let day = d.getDay(); + day = (day + +(day === 0) * 7) - 1; const actualDay = day; - day = +((day === 0) || (day === 6)) + (+(!(day === 0) && !(day === 6)) * day); - if ((typeof query.day === "string") && (parseInt(query.day).toString() === query.day) && (!isNaN(parseInt(query.day))) && (parseInt(query.day) > 0) && (parseInt(query.day) < 6)) + day = +(!(day === 5) && !(day === 6)) * day; + if ((typeof query.day === "string") && (parseInt(query.day).toString() === query.day) && (!isNaN(parseInt(query.day))) && (parseInt(query.day) >= 0) && (parseInt(query.day) < 5)) day = parseInt(query.day); data_string = data_string.replace(`