diff options
Diffstat (limited to 'poll/index.js')
-rw-r--r-- | poll/index.js | 39 |
1 files changed, 26 insertions, 13 deletions
diff --git a/poll/index.js b/poll/index.js index ee4f375..4fb1eab 100644 --- a/poll/index.js +++ b/poll/index.js @@ -7,17 +7,23 @@ app.use("/", checkUser, express.static(__dirname + "/public")); app.get("/api/question/:id", checkUser, async (req, res) => { try { - const questions = await db.query(`SELECT rq.id, rq.question + 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]); + 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 * + const answers = await db.query( + `SELECT * FROM ranking_answers WHERE question_id = ? - AND user_id = ?`, [question.id, req.session.uid]); + AND user_id = ?`, + [question.id, req.session.uid], + ); question.answer = answers.length > 0 ? answers[0].answer_id : undefined; res.json(question); // 😜 } else { @@ -35,17 +41,23 @@ app.get("/api/questions/:type", checkUser, async (req, res) => { const fail = { success: false }; if (types.includes(type)) { try { - const questions = await db.query(`SELECT id + 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 + WHERE type_id = ?`, + [types.indexOf(type) + 1], + ); + const answers = await db.query( + `SELECT question_id FROM ranking_answers - WHERE user_id = ?`, [req.session.uid]); + WHERE user_id = ?`, + [req.session.uid], + ); const resp = []; + let i = 0; 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 }); + resp.push({ id: i++, answered: qid >= 0 }); } res.json(resp); } catch (e) { @@ -53,7 +65,7 @@ app.get("/api/questions/:type", checkUser, async (req, res) => { 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 (?,?,?)"); @@ -77,10 +89,11 @@ async function answer(req, res, qu) { 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 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) + } else if (type !== types[1]) return res.json(fail); await db.query(qu, [answer, question, req.session.uid]); res.json({ success: true }); } catch (e) { |