diff options
author | Marvin Borner | 2020-10-10 17:05:27 +0200 |
---|---|---|
committer | Marvin Borner | 2020-10-10 17:05:27 +0200 |
commit | 72f5731adeebf8d76c5c2dcc266f600ba57812d8 (patch) | |
tree | 4d774eb3d0464411a1bb09472c597298aa8965e8 /admin/index.js | |
parent | 167600b52eb03801bb7051a09dcb0e4f159cfb2a (diff) |
Added basic admin interface
Diffstat (limited to 'admin/index.js')
-rw-r--r-- | admin/index.js | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/admin/index.js b/admin/index.js new file mode 100644 index 0000000..4cea14d --- /dev/null +++ b/admin/index.js @@ -0,0 +1,53 @@ +const express = require("express"); +const db = require("../db"); +const app = express.Router(); +const { checkUser, checkAdmin } = require("../auth"); + +app.use("/", checkAdmin, express.static(__dirname + "/public")); + +// For debugging ig +app.get("/api/all", checkAdmin, async (req, res) => { + const all = []; + + const types = await db.query("SELECT * FROM types ORDER BY id"); + const clazz = await db.query("SELECT * FROM class ORDER BY id"); + const users = await db.query("SELECT * FROM users ORDER BY id"); + const quotes = await db.query("SELECT * FROM quotes ORDER BY id"); + const ranking_questions = await db.query("SELECT * FROM ranking_questions ORDER BY id"); + const ranking_answers = await db.query("SELECT * FROM ranking_answers ORDER BY id"); + const mottos = await db.query("SELECT * FROM mottos ORDER BY id"); + const motto_votes = await db.query("SELECT * FROM motto_votes ORDER BY id"); + + all.push( + { quotes }, + { clazz }, + { users }, + { quotes }, + { ranking_questions }, + { ranking_answers }, + { mottos }, + { motto_votes }, + ); + res.json(all); +}); + +app.get("/api/questions", checkAdmin, async (req, res) => { + const questions = await db.query("SELECT q.id, question, t.name type FROM ranking_questions q INNER JOIN types t on type_id = t.id ORDER BY q.id"); + res.json(questions); +}); + +app.get("/api/answers", checkAdmin, async (req, res) => { + const answers = await db.query( + "SELECT question_id, name, middlename, surname, count(*) count FROM ranking_questions q INNER JOIN ranking_answers a ON q.id = a.question_id INNER JOIN users u ON answer_id = u.id GROUP BY question_id, answer_id ORDER BY count DESC" + ); + res.json(answers); +}); + +app.get("/api/votes", checkAdmin, async (req, res) => { + const votes = await db.query( + "SELECT m.id, m.name, m.description, SUM(votes) votes FROM motto_votes mv RIGHT JOIN mottos m on mv.motto_id = m.id GROUP BY m.id, m.name, m.description ORDER BY SUM(votes) DESC", + ); + res.json(votes); +}); + +module.exports = app; |