From e52bcb760f36b68495692ac5c5a5b68e8dafc33b Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Fri, 2 Oct 2020 21:12:23 +0200 Subject: Added polls --- .env.example | 1 + app.js | 4 +++- db.js | 10 ++++++++++ overview/public/index.html | 1 + poll.txt | 34 ++++++++++++++++++++++++++++++++++ poll/index.js | 34 ++++++++++++++++++++++++++++++++++ poll/public/index.html | 35 +++++++++++++++++++++++++++++++++++ poll/public/script.js | 40 ++++++++++++++++++++++++++++++++++++++++ poll/public/style.css | 35 +++++++++++++++++++++++++++++++++++ quotes/index.js | 2 +- tables.sql | 31 +++++++++++++++++++++++++++---- 11 files changed, 221 insertions(+), 6 deletions(-) create mode 100644 poll.txt create mode 100644 poll/index.js create mode 100644 poll/public/index.html create mode 100644 poll/public/script.js create mode 100644 poll/public/style.css diff --git a/.env.example b/.env.example index dd6613d..ef55330 100644 --- a/.env.example +++ b/.env.example @@ -2,3 +2,4 @@ DBHost= DBName= DBUser= DBPassword= +sessionSecret= diff --git a/app.js b/app.js index adda55c..23e2715 100644 --- a/app.js +++ b/app.js @@ -5,6 +5,7 @@ const session = require("express-session"); const { auth, checkUser } = require("./auth"); const motto = require("./motto"); const quotes = require("./quotes"); +const poll = require("./poll"); const app = express(); @@ -15,7 +16,7 @@ const redisClient = redis.createClient(); app.use( session({ store: new RedisStore({ client: redisClient }), - secret: "keyboard cat", + secret: process.env.sessionSecret, resave: false, saveUninitialized: true, cookie: { secure: false }, @@ -28,6 +29,7 @@ app.use(express.json()); app.use("/", express.static(__dirname + "/overview/public")); app.use("/motto", checkUser, motto); app.use("/quotes", checkUser, quotes); +app.use("/poll", checkUser, poll); app.use("/auth", auth); app.listen(5005, () => console.log("Server started on http://localhost:5005")); diff --git a/db.js b/db.js index da082dc..58673bc 100644 --- a/db.js +++ b/db.js @@ -40,6 +40,16 @@ class DB { "INSERT INTO class (name) VALUES ('TGM13.1'), ('TGM13.2'), ('TGTM13.1'), ('TGI13.1'), ('TGI13.2')" ); + fs.readFile(__dirname + "/poll.txt", "utf8", (err, data) => { + if (err) throw err; + + const questions = data.split("\n"); + questions.forEach((question) => { + if (question.length > 0) + this.query("INSERT INTO ranking_questions (question, type_id) VALUE (?,?)", [question, 2]); + }); + }); + const classes = data.split("--"); const userPasswords = {}; console.log("Generating users"); diff --git a/overview/public/index.html b/overview/public/index.html index 82d5fac..bc44530 100644 --- a/overview/public/index.html +++ b/overview/public/index.html @@ -27,6 +27,7 @@
Falls ihr uns bei der Entwicklung helfen wollt, Fehler gefunden habt oder Fragen an uns habt, könnt ihr diff --git a/poll.txt b/poll.txt new file mode 100644 index 0000000..19583ae --- /dev/null +++ b/poll.txt @@ -0,0 +1,34 @@ +...wohnt bei 30 noch bei Mama? +...schläft im Unterricht immer? +...landet am ehesten im Knast? +...ist der größte Pumper? +...hat die schrägste Lache? +...ist der Lehrerliebling? +...stellt die blödesten Fragen? +...korrigiert die meisten Lehrerfehler? +...ist am meisten in sein Handy verliebt? +...später Millionär*in? +...hat den besten Style? +...hat die schönsten Heftaufschriebe? +...findet die besten Ausreden? +...macht die besten Witze? +...wird als erstes heiraten? +...hat die größte Sauklaue? +...öffnet zuerst eine eigene Firma? +...ist nie krank? +...hat immer gute Laune? +...ist die größte Labertasche? +...ist der Albtraum aller Lehrer? +...ist ein tiefes, stilles Wasser? +...heult trotz 14 Punkten? +...isst durchgehend? +...ist der größte Schleimer? +...kommt immer zu spät? +...ist der größte Schnorrer? +...ist "dauerdicht"? +...ist die schönste/hübscheste Person? +...ist der größte Streber? +...ist durchgehend krank? +...schwänzt am meisten? +...macht nie Hausaufgaben? +...feiert am meisten? diff --git a/poll/index.js b/poll/index.js new file mode 100644 index 0000000..ab9ee6f --- /dev/null +++ b/poll/index.js @@ -0,0 +1,34 @@ +const express = require("express"); +const db = require("../db"); +const app = express.Router(); +const { checkUser } = require("../auth"); + +app.use("/", checkUser, express.static(__dirname + "/public")); + +app.post("/api/answer", checkUser, async (req, res) => { + if (!req.body.answer || !req.body.question) return res.send("error"); + try { + await db.query("INSERT INTO ranking_answers (question_id, user_id, answer_id) VALUE (?,?,?)", [ + parseInt(req.body.question), + req.session.uid, + parseInt(req.body.answer), + ]); + res.redirect("/poll"); + } catch (e) { + console.error(e); + res.json("error"); + } +}); + +app.get("/api/get", checkUser, async (req, res) => { + // TODO: Add teacher questions + const question = ( + await db.query( + "SELECT q.id, q.question, t.name FROM ranking_questions AS q INNER JOIN types AS t ON type_id = t.id WHERE q.id NOT IN (SELECT question_id FROM ranking_answers WHERE user_id = ?) AND t.name = 'pupil' LIMIT 1", + [req.session.uid] + ) + )[0]; + res.json(question); +}); + +module.exports = app; diff --git a/poll/public/index.html b/poll/public/index.html new file mode 100644 index 0000000..8530b76 --- /dev/null +++ b/poll/public/index.html @@ -0,0 +1,35 @@ + + +
+ + + + + +