diff options
-rw-r--r-- | app.js | 2 | ||||
-rw-r--r-- | db.js | 6 | ||||
-rw-r--r-- | drop.sql | 1 | ||||
-rw-r--r-- | overview/public/index.html | 3 | ||||
-rw-r--r-- | prediction/index.js | 47 | ||||
-rw-r--r-- | prediction/public/index.html | 35 | ||||
-rw-r--r-- | prediction/public/script.js | 37 | ||||
-rw-r--r-- | prediction/public/style.css | 42 | ||||
-rw-r--r-- | tables.sql | 11 |
9 files changed, 184 insertions, 0 deletions
@@ -12,6 +12,7 @@ const poll = require("./poll"); const profile = require("./profile"); const admin = require("./admin"); const questions = require("./questions"); +const prediction = require("./prediction"); const app = express(); @@ -39,6 +40,7 @@ app.use("/quotes", checkUser, quotes); app.use("/poll", checkUser, poll); app.use("/profile", checkUser, profile); app.use("/questions", checkUser, questions); +app.use("/prediction", checkUser, prediction); app.use("/admin", checkAdmin, admin); // Lel app.use("/auth", auth); @@ -214,6 +214,12 @@ class DB { await this.query(tables[15]); } + async resetTeacherPredict() { + const tables = await this.getTables(); + await this.query("DROP TABLE IF EXISTS teacher_prediction"); + await this.query(tables[16]); + } + async regenerateUser(uid) { const pwd = nanoid.nanoid(8); const password = await bcrypt.hash(pwd, 10); @@ -11,6 +11,7 @@ DROP TABLE IF EXISTS question_answers; DROP TABLE IF EXISTS question_options; DROP TABLE IF EXISTS question_questions; DROP TABLE IF EXISTS profile_char; +DROP TABLE IF EXISTS teacher_prediction; DROP TABLE IF EXISTS users; DROP TABLE IF EXISTS types; DROP TABLE IF EXISTS class; diff --git a/overview/public/index.html b/overview/public/index.html index 03c67c4..24ae6d5 100644 --- a/overview/public/index.html +++ b/overview/public/index.html @@ -53,6 +53,9 @@ <a href="/questions" class="pure-menu-link">Fragen</a> </li> <li class="pure-menu-item"> + <a href="/prediction" class="pure-menu-link">Lehrerauswahl</a> + </li> + <li class="pure-menu-item"> <a href="/images" target="_blank" class="pure-menu-link">Klassenbilder</a> </li> </ul> diff --git a/prediction/index.js b/prediction/index.js new file mode 100644 index 0000000..c5bb81e --- /dev/null +++ b/prediction/index.js @@ -0,0 +1,47 @@ +const express = require("express"); +const db = require("../db"); +const app = express.Router(); +const { checkUser } = require("../auth"); + +app.use("/", checkUser, express.static(__dirname + "/public")); + +app.get("/api/get", checkUser, async (req, res) => { + try { + const answer = (await db.query("SELECT * FROM teacher_prediction WHERE user_id = ?", [req.session.uid]))[0] || {}; + // console.log(answer); + res.json(answer); + } catch (e) { + console.error(e); + res.json({ success: false }); + } +}); + +app.post("/api/set", checkUser, async (req, res) => { + const { teacher } = req.body; + if (!teacher) return res.json({ success: false }); + try { + const isTeacher = (await db.query("SELECT id FROM users WHERE type_id = 2 AND id = ?", [teacher])).length > 0; + if (!isTeacher) return res.json({ success: false }); + await db.query("INSERT INTO teacher_prediction (user_id, teacher_id) VALUE (?,?)", [req.session.uid, teacher]); + res.json({ success: true }); + } catch (e) { + console.error(e); + res.json({ success: false }); + } +}); + +app.put("/api/set", checkUser, async (req, res) => { + const { teacher } = req.body; + if (!teacher) return res.json({ success: false }); + try { + const isTeacher = (await db.query("SELECT id FROM users WHERE type_id = 2 AND id = ?", [teacher])).length > 0; + if (!isTeacher) return res.json({ success: false }); + await db.query("UPDATE teacher_prediction SET teacher_id = ? WHERE user_id = ?", [+teacher, req.session.uid]); + res.json({ success: true }); + } catch (e) { + console.error(e); + res.json({ success: false }); + } +}); + +module.exports = app; diff --git a/prediction/public/index.html b/prediction/public/index.html new file mode 100644 index 0000000..46b920e --- /dev/null +++ b/prediction/public/index.html @@ -0,0 +1,35 @@ +<!DOCTYPE html> +<html> + <head> + <meta charset="UTF-8" /> + <meta name="viewport" content="width=device-width, initial-scale=1" /> + <link + rel="stylesheet" + href="https://unpkg.com/purecss@2.0.3/build/pure-min.css" + integrity="sha384-cg6SkqEOCV1NbJoCu11+bm0NvBRc8IYLRGXkmNrqUBfTjmMYwNKPWBTIKyw9mHNJ" + crossorigin="anonymous" + /> + <link rel="stylesheet" href="style.css" type="text/css" media="all" /> + + <title>Zitate</title> + </head> + <body> + <div class="pure-menu pure-menu-horizontal"> + <a href="/" class="pure-menu-item pure-menu-link">Home</a> + <a href="/auth/api/logout" class="pure-menu-item pure-menu-link">Logout</a> + </div> + + <main> + <div class="pure-form pure-form-stacked"> + <fieldset> + <legend>Hier sieht mich mein Lehrer in 10 Jahren:</legend> + <label for="prediction">Lehrer</label> + <select name="teacher" id="prediction"></select> + <button type="submit" class="pure-button pure-button-primary">Hinzufügen</button> + </fieldset> + </div> + </main> + + <script src="script.js" charset="utf-8"></script> + </body> +</html> diff --git a/prediction/public/script.js b/prediction/public/script.js new file mode 100644 index 0000000..a4d0527 --- /dev/null +++ b/prediction/public/script.js @@ -0,0 +1,37 @@ +const dropdown = document.getElementById("prediction"); +const submit = document.querySelector('button[type="submit"]'); +let method = "POST"; + +dropdown.insertAdjacentHTML( + "beforeend", + '<option disabled value="" selected="true" disabled>Lehrer auswählen...</option>', +); + +function appendOption(response) { + response.forEach((elem, i) => { + dropdown.insertAdjacentHTML( + "beforeend", + `<option value="${elem["id"]}">${elem["name"]} ${elem["middlename"] ? elem["middlename"] + " " : ""}${elem["surname"]}</option>`, + ); + }); +} + +function selectOption(response) { + if (Object.keys(response).length > 0) { + dropdown.value = response.teacher_id; + method = "PUT"; + } +} + +fetch("/auth/api/list?class=teacher") + .then((response) => response.json()) + .then((response) => appendOption(response)) + .then(() => fetch("api/get")) + .then((response) => response.json()) + .then(selectOption); + +submit.addEventListener("click", async (e) => { + const teacher = dropdown.value; + const body = JSON.stringify({ teacher }); + await fetch("api/set", { method, body, headers: { "Content-Type": "application/json" } }); +});
\ No newline at end of file diff --git a/prediction/public/style.css b/prediction/public/style.css new file mode 100644 index 0000000..b34819a --- /dev/null +++ b/prediction/public/style.css @@ -0,0 +1,42 @@ +html, +body { + padding: 0; + margin: 0; + height: 100%; + width: 100%; + color: #424242; + line-height: 1.6; + background-color: #eec0c6; + background-image: linear-gradient(315deg, #eec0c6 0%, #7ee8fa 74%); +} + +div { + background: white; +} + +main { + position: absolute; + max-height: calc(100% - 140px); + overflow-y: auto; + width: 50%; + left: 50%; + top: 50%; + -webkit-transform: translate(-50%, -50%); + transform: translate(-50%, -50%); + padding: 20px; + border-radius: 10px; + background: white; +} + +input, +button, +select { + width: 100%; + color: #424242; +} + +@media only screen and (max-width: 700px) { + main { + width: calc(100% - 20%); + } +} @@ -176,3 +176,14 @@ CREATE TABLE IF NOT EXISTS profile_char CONSTRAINT `fk_char_user2` FOREIGN KEY (user_id) REFERENCES users (id) ) ENGINE = InnoDB DEFAULT CHARSET = utf8; + +CREATE TABLE IF NOT EXISTS teacher_prediction +( + id INTEGER PRIMARY KEY AUTO_INCREMENT, + user_id INTEGER NOT NULL UNIQUE, + teacher_id INTEGER NOT NULL, + + CONSTRAINT `fk_teacher_user` FOREIGN KEY (user_id) REFERENCES users (id), + CONSTRAINT `fk_teacher_teacher` FOREIGN KEY (teacher_id) REFERENCES users (id) +) ENGINE = InnoDB + DEFAULT CHARSET = utf8;
\ No newline at end of file |