diff options
author | LarsVomMars | 2021-01-21 12:09:39 +0100 |
---|---|---|
committer | LarsVomMars | 2021-01-21 12:09:39 +0100 |
commit | fa4026fd87b17cc2c69f23da516f7399aae20f67 (patch) | |
tree | 5ea34aafcc06632ce8e66551b1b1d4d89ba9d51c | |
parent | cc4f65bd351566e020eceaab8a6c362fe1385404 (diff) |
BACKEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEND
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | poll/index.js | 89 | ||||
-rw-r--r-- | poll/public/index.html | 11 | ||||
-rw-r--r-- | poll/public/script.js | 82 |
4 files changed, 107 insertions, 76 deletions
@@ -2,6 +2,7 @@ *lock* node_* .vscode/ +.idea/ *.env *.csv users.json diff --git a/poll/index.js b/poll/index.js index 84af052..54a6721 100644 --- a/poll/index.js +++ b/poll/index.js @@ -5,54 +5,63 @@ 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 || !req.query.type) return res.send("error"); - if (req.body.answer == req.session.uid) return res.send("error"); +app.get("/api/question/:id", checkUser, async (req, res) => { try { - if (req.query.type == "pupil") { - const user_class = (await db.query("SELECT class_id FROM users WHERE id = ?", [req.session.uid]))[0] - .class_id; - const answer_class = ( - await db.query("SELECT class_id FROM users WHERE id = ?", [parseInt(req.body.answer)]) - )[0].class_id; - if (user_class != answer_class) return res.send("error"); - } else if (req.query.type == "teacher") { - const answer_type = ( - await db.query( - "SELECT t.name FROM users AS u INNER JOIN types AS t ON u.type_id = t.id WHERE u.id = ?", - [parseInt(req.body.answer)], - ) - )[0].name; - if (answer_type != "teacher") return res.send("error"); + const questions = await db.query(`SELECT rq.id, rq.question + FROM ranking_questions rq + INNER JOIN types t on rq.type_id = t.id + WHERE t.name = ?`, [req.query.type]); + const id = req.params.id; + if (id >= 0 && id < questions.length) { + const question = questions[id]; + const answers = await db.query(`SELECT * + FROM ranking_answers + WHERE question_id = ? + AND user_id = ?`, [question.id, req.session.uid]); + question.answer = answers.length > 0 ? answers[0].answer_id : undefined; + res.json(question); // 😜 } else { - return res.send("error"); + res.json({}); } - - 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?type=" + req.query.type); } catch (e) { console.error(e); - res.send("error"); + res.json({ success: false }); } }); -app.get("/api/get", checkUser, async (req, res) => { - try { - 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 = ? LIMIT 1", - [req.session.uid, req.query.type], - ) - )[0]; - res.json(question); - } catch (e) { - console.error(e); - res.send("error"); - } +app.post("/api/answer/:type", checkUser, async (req, res) => { + return await answer(req, res, "INSERT INTO ranking_answers (answer_id, question_id, user_id) VALUE (?,?,?)"); }); +app.put("/api/answer/:type", checkUser, async (req, res) => { + return await answer(req, res, "UPDATE ranking_answers SET answer_id = ? WHERE question_id = ? AND user_id = ?"); +}); + +// TODO: Puzzle bar + +async function answer(req, res, qu) { + const type = req.params.type; + const types = ["pupil", "teacher"]; + const fail = { success: false }; + if (types.includes(type)) { + const { question, answer } = req.body; + if (+answer === +req.session.uid) return res.json(fail); + try { + const answerTypes = await db.query("SELECT type_id FROM users WHERE id = ?", [question]); + if (!answerTypes.length > 0) return res.json(fail); + if (type !== types[answerTypes[0].type_id - 1]) return res.json(fail); + if (type === types[0]) { + const userClass = (await db.query("SELECT class_id FROM users WHERE id = ?", [req.session.uid]))[0].class_id; + const answerUsers = await db.query("SELECT class_id FROM users WHERE id = ?", [answer]); + if (!answerUsers.length > 0 || userClass !== answerUsers[0].class_id) return res.json(fail); + } else if (type !== types[1]) return res.json(fail) + await db.query(qu, [answer, question, req.session.uid]); + res.json({ success: true }); + } catch (e) { + console.error(e); + res.json(fail); + } + } else res.json(fail); +} + module.exports = app; diff --git a/poll/public/index.html b/poll/public/index.html index 59e5939..26c7684 100644 --- a/poll/public/index.html +++ b/poll/public/index.html @@ -20,19 +20,22 @@ </div> <main> - <form class="pure-form pure-form-stacked" action="api/answer" method="post"> +<!-- <form class="pure-form pure-form-stacked" action="api/answer" method="post">--> + <div class="pure-form pure-form-stacked"> <fieldset> <!-- TODO: Add progress --> <legend>Schüler-Ranking</legend> <p>Welche/r Schüler/in...</p> <label id="question_label" for="question"></label> <input name="question" id="question" hidden /> - <br /> <label for="answer">Antwort</label> <select name="answer" id="answer" required></select> - <button type="submit" class="pure-button pure-button-primary">Antworten</button> + <button type="submit" id="submit-btn" class="pure-button pure-button-primary">Antworten</button> + <button id="skip-btn" class="pure-button pure-button-danger">Weiter</button> </fieldset> - </form> + </div> + +<!-- </form>--> </main> <script src="script.js" charset="utf-8"></script> diff --git a/poll/public/script.js b/poll/public/script.js index 5cca71a..dc46578 100644 --- a/poll/public/script.js +++ b/poll/public/script.js @@ -1,19 +1,26 @@ -const type = getParameterByName("type"); +const query = new URL(window.location.href).searchParams; +const type = query.get("type"); +const qid = query.get("qid") || 0; +let method = "POST"; + const dropdown = document.getElementById("answer"); const question_input = document.getElementById("question"); const question_label = document.getElementById("question_label"); +const submit = document.getElementById("submit-btn"); +const skip = document.getElementById("skip-btn"); if (!["teacher", "pupil"].includes(type)) window.location.href = "/"; dropdown.insertAdjacentHTML( "beforeend", - '<option value="" selected="true" disabled>' + - (type == "teacher" ? "Lehrer" : "Schüler") + - "/in auswählen...</option>", + '<option value="" selected disabled>' + + (type === "teacher" ? "Lehrer" : "Schüler") + + "/in auswählen...</option>", ); -document.querySelector("legend").innerText = type == "teacher" ? "Lehrer-Ranking" : "Schüler-Ranking"; -document.querySelector("p").innerText = "Welche/r " + (type == "teacher" ? "Lehrer/in" : "Schüler/in") + "..."; -document.querySelector("form").setAttribute("action", "api/answer?type=" + type); +document.querySelector("legend").innerText = type === "teacher" ? "Lehrer-Ranking" : "Schüler-Ranking"; +document.querySelector("p").innerText = "Welche/r " + (type === "teacher" ? "Lehrer/in" : "Schüler/in") + "..."; + +skip.addEventListener("click", () => getNext(parseInt(qid) + 1)); function appendOption(response) { response.forEach((elem) => { @@ -26,35 +33,46 @@ function appendOption(response) { }); } -fetch("/auth/api/list" + (type == "teacher" ? "?class=teacher" : "")) +fetch("/auth/api/list" + (type === "teacher" ? "?class=teacher" : "")) .then((response) => response.json()) .then((response) => appendOption(response)); -fetch("/poll/api/get?type=" + type) - .then(async (response) => { - let json; - try { - return await response.json(); - } catch (e) { - document.querySelector("p").innerText = ""; - question_label.innerText = "Du hast bereits alle Fragen beantwortet."; - document.querySelectorAll("label")[1].innerText = "Danke!"; - document.querySelector("select").style.display = "none"; - document.querySelector("button").style.display = "none"; - throw "Oh nein, alle beantwortet!"; // :^) - } - }) +fetch(`/poll/api/question/${qid}?type=${type}`) + .then((response) => response.json()) .then((response) => { - question_label.innerText = response["question"]; - question_input.setAttribute("value", response["id"]); + if (!response.empty()) { + question_label.innerText = response["question"]; + question_input.setAttribute("value", response["id"]); + if (response.answer) { + for (const c of dropdown.children) if (+c.value === response.answer) c.selected = true; + method = "PUT"; + } + submit.addEventListener("click", async () => { + await request(); + getNext(parseInt(qid) + 1); + if (method === "POST") method = "PUT"; + }); + } else getNext(); // Resets }); -function getParameterByName(name, url) { - if (!url) url = window.location.href; - name = name.replace(/[\[\]]/g, "\\$&"); - var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), - results = regex.exec(url); - if (!results) return null; - if (!results[2]) return ""; - return decodeURIComponent(results[2].replace(/\+/g, " ")); +function getNext(q = 0) { + window.location.assign(`/poll/?qid=${q}&type=${type}`); +} + +async function request() { + const body = JSON.stringify({ + question: question_input.value, + answer: dropdown.value, + }); + const resp = await fetch(`api/answer/${type}`, { + method, + headers: { "Content-Type": "application/json" }, + body, + }); + return resp.json(); } + +// I did this myself lel 🤨 +Object.prototype.empty = function () { + return Object.keys(this).length === 0 +}
\ No newline at end of file |