From e6626356d90fcc58db1dbfad35211c0b3a103aa7 Mon Sep 17 00:00:00 2001 From: LarsVomMars Date: Wed, 7 Oct 2020 23:42:34 +0200 Subject: User profile boilerplate --- app.js | 2 ++ db.js | 11 +++++++++++ profile.txt | 1 + profile/index.js | 34 ++++++++++++++++++++++++++++++++++ profile/public/index.html | 34 ++++++++++++++++++++++++++++++++++ profile/public/script.js | 0 profile/public/style.css | 0 tables.sql | 29 +++++++++++++++++++++++++++++ 8 files changed, 111 insertions(+) create mode 100644 profile.txt create mode 100644 profile/index.js create mode 100644 profile/public/index.html create mode 100644 profile/public/script.js create mode 100644 profile/public/style.css diff --git a/app.js b/app.js index fcb7a8a..79002dd 100644 --- a/app.js +++ b/app.js @@ -7,6 +7,7 @@ const motto = require("./motto"); const mottovote = require("./mottovote"); const quotes = require("./quotes"); const poll = require("./poll"); +const profile = require("./profile"); const app = express(); @@ -32,6 +33,7 @@ app.use("/motto", checkUser, motto); app.use("/mottovote", checkUser, mottovote); app.use("/quotes", checkUser, quotes); app.use("/poll", checkUser, poll); +app.use("/profile", checkUser, profile); app.use("/auth", auth); app.listen(5005, () => console.log("Server started on http://localhost:5005")); diff --git a/db.js b/db.js index 16f2970..49caabd 100644 --- a/db.js +++ b/db.js @@ -40,6 +40,7 @@ class DB { "INSERT INTO class (name) VALUES ('TGM13.1'), ('TGM13.2'), ('TGTM13.1'), ('TGI13.1'), ('TGI13.2'), ('teacher')" ); + // User polls fs.readFile(__dirname + "/poll.txt", "utf8", (err, data) => { if (err) throw err; @@ -53,6 +54,7 @@ class DB { }); }); + // Motto votes fs.readFile(__dirname + "/mottos.txt", "utf8", (err, data) => { if (err) throw err; @@ -64,6 +66,15 @@ class DB { }); }); + fs.readFile(__dirname + "/profile.txt", "utf8", (err, data) => { + if (err) throw err; + + const questions = data.split("\n"); + questions.forEach((question) => { + if (question) this.query("INSERT INTO profile_questions (question) VALUE (?)", [question]); + }); + }); + const classes = data.split("--"); const userPasswords = {}; console.log("Generating users"); diff --git a/profile.txt b/profile.txt new file mode 100644 index 0000000..dd79092 --- /dev/null +++ b/profile.txt @@ -0,0 +1 @@ +Alter? \ No newline at end of file diff --git a/profile/index.js b/profile/index.js new file mode 100644 index 0000000..4bfc1f7 --- /dev/null +++ b/profile/index.js @@ -0,0 +1,34 @@ +const express = require("express"); +const db = require("../db"); +const app = express.Router(); + +app.use("/", express.static(__dirname + "/public/")); + +// Basic API +app.get("/api/user", async (req, res) => {}); + +app.get("/api/questions", async (req, res) => { + const questions = await db.query("SELECT id, question FROM profile_questions"); + const answers = await db.query("SELECT answer, question_id FROM profile_answers WHERE user_id = ?", [req.session.uid]); + + for (const answer of answers) { + const qid = questions.findIndex((question) => question.id === answer.question_id); + if (qid !== undefined) questions[qid].answer = answer.answer; + } + res.json(questions); +}); + +app.post("/api/add", async (req, res) => {}); + +app.put("/api/update", async (req, res) => {}); + +// Comments API +app.get("/api/comments/:uid", async (req, res) => {}); + +app.post("/api/comment", async (req, res) => {}); + +app.put("/api/comment", async (req, res) => {}); + +app.delete("/api/comment", async (req, res) => {}); + +module.exports = app; \ No newline at end of file diff --git a/profile/public/index.html b/profile/public/index.html new file mode 100644 index 0000000..30f9de8 --- /dev/null +++ b/profile/public/index.html @@ -0,0 +1,34 @@ + + + +
+ + + + +