aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xcli.js2
-rw-r--r--mottovote/index.js2
-rw-r--r--poll/index.js28
-rw-r--r--poll/public/index.html4
-rw-r--r--poll/public/script.js6
-rw-r--r--profile/index.js4
6 files changed, 36 insertions, 10 deletions
diff --git a/cli.js b/cli.js
index c62dfb5..b802095 100755
--- a/cli.js
+++ b/cli.js
@@ -1,7 +1,5 @@
#!/usr/bin/env node
require("dotenv").config();
-const bcrypt = require("bcrypt");
-const nanoid = require("nanoid");
const db = require("./db");
const params = process.argv.slice(2);
diff --git a/mottovote/index.js b/mottovote/index.js
index 73c1641..9ef3d62 100644
--- a/mottovote/index.js
+++ b/mottovote/index.js
@@ -11,7 +11,7 @@ app.get("/api/list", checkUser, async (req, res) => {
for (const vote of votes) {
const mid = mottos.findIndex((motto) => motto.id === vote.motto_id);
- if (mid !== undefined) mottos[mid].votes = vote.votes;
+ if (mid >= 0) mottos[mid].votes = vote.votes;
}
res.json(mottos);
});
diff --git a/poll/index.js b/poll/index.js
index 54a6721..ee4f375 100644
--- a/poll/index.js
+++ b/poll/index.js
@@ -29,6 +29,32 @@ app.get("/api/question/:id", checkUser, async (req, res) => {
}
});
+app.get("/api/questions/:type", checkUser, async (req, res) => {
+ const type = req.params.type;
+ const types = ["pupil", "teacher"];
+ const fail = { success: false };
+ if (types.includes(type)) {
+ try {
+ const questions = await db.query(`SELECT id
+ FROM ranking_questions rq
+ WHERE type_id = ?`, [types.indexOf(type) + 1]);
+ const answers = await db.query(`SELECT question_id
+ FROM ranking_answers
+ WHERE user_id = ?`, [req.session.uid]);
+ const resp = [];
+ for (const question of questions) {
+ const qid = answers.findIndex((answer) => question.id === answer.question_id);
+ console.log(qid);
+ resp.push({ id: question.id, answered: qid >= 0 });
+ }
+ res.json(resp);
+ } catch (e) {
+ console.error(e);
+ res.json(fail);
+ }
+ } else res.json(fail);
+})
+
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 (?,?,?)");
});
@@ -47,7 +73,7 @@ async function answer(req, res, qu) {
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]);
+ const answerTypes = await db.query("SELECT type_id FROM ranking_questions 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]) {
diff --git a/poll/public/index.html b/poll/public/index.html
index 26c7684..45379aa 100644
--- a/poll/public/index.html
+++ b/poll/public/index.html
@@ -20,10 +20,8 @@
</div>
<main>
-<!-- <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>
@@ -34,8 +32,6 @@
<button id="skip-btn" class="pure-button pure-button-danger">Weiter</button>
</fieldset>
</div>
-
-<!-- </form>-->
</main>
<script src="script.js" charset="utf-8"></script>
diff --git a/poll/public/script.js b/poll/public/script.js
index dc46578..cdffa17 100644
--- a/poll/public/script.js
+++ b/poll/public/script.js
@@ -55,6 +55,12 @@ fetch(`/poll/api/question/${qid}?type=${type}`)
} else getNext(); // Resets
});
+fetch(`api/questions/${type}`)
+ .then(response => response.json())
+ .then(response => {
+ // TODO: PROGRESS POGGERS
+ });
+
function getNext(q = 0) {
window.location.assign(`/poll/?qid=${q}&type=${type}`);
}
diff --git a/profile/index.js b/profile/index.js
index fcdf877..ded1ec4 100644
--- a/profile/index.js
+++ b/profile/index.js
@@ -17,7 +17,7 @@ app.get("/api/user/:uid", async (req, res) => {
for (const answer of answers) {
const qid = questions.findIndex((question) => question.id === answer.question_id);
- if (qid !== undefined) questions[qid].answer = answer.answer;
+ if (qid >= 0) questions[qid].answer = answer.answer;
}
res.json({ user: user[0], questions });
});
@@ -33,7 +33,7 @@ app.get("/api/questions", async (req, res) => {
for (const answer of answers) {
const qid = questions.findIndex((question) => question.id === answer.question_id);
- if (qid !== undefined) questions[qid].answer = answer.answer;
+ if (qid >= 0) questions[qid].answer = answer.answer;
}
res.json(questions);
});