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 @@ + + + + + + + + + Steckbrief + + + +
+ Home + Logout +
+
+ +
+
+ Steckbrief + + + + + +
+
+
+ + + + \ No newline at end of file diff --git a/profile/public/script.js b/profile/public/script.js new file mode 100644 index 0000000..e69de29 diff --git a/profile/public/style.css b/profile/public/style.css new file mode 100644 index 0000000..e69de29 diff --git a/tables.sql b/tables.sql index cd22626..72d8664 100644 --- a/tables.sql +++ b/tables.sql @@ -14,6 +14,9 @@ CREATE TABLE IF NOT EXISTS theme( -- DROP TABLE IF EXISTS quotes; -- DROP TABLE IF EXISTS ranking_questions; -- DROP TABLE IF EXISTS ranking_answers; +-- DROP TABLE IF EXISTS profile_comments; +-- DROP TABLE IF EXISTS profile_answers; +-- DROP TABLE IF EXISTS profile_questions; -- DROP TABLE IF EXISTS users; -- DROP TABLE IF EXISTS types; -- DROP TABLE IF EXISTS class; @@ -94,3 +97,29 @@ CREATE TABLE IF NOT EXISTS motto_votes( CONSTRAINT `fk_voted_user` FOREIGN KEY (user_id) REFERENCES users (id), CONSTRAINT `fk_voted_vote` FOREIGN KEY (motto_id) REFERENCES mottos (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE IF NOT EXISTS profile_questions( + id INTEGER PRIMARY KEY AUTO_INCREMENT, + question VARCHAR(255) NOT NULL UNIQUE +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE IF NOT EXISTS profile_answers( + id INTEGER PRIMARY KEY AUTO_INCREMENT, + question_id INTEGER NOT NULL, + user_id INTEGER NOT NULL, + answer TEXT NULL, -- Consider VARCHAR + + UNIQUE KEY uk_answer (question_id, user_id), + CONSTRAINT `fk_profile_user` FOREIGN KEY (user_id) REFERENCES users (id), + CONSTRAINT `fk_profile_question` FOREIGN KEY (question_id) REFERENCES profile_questions (id) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE IF NOT EXISTS profile_comments( + id INTEGER PRIMARY KEY AUTO_INCREMENT, + profile_id INTEGER NOT NULL, -- User's profile + user_id INTEGER NOT NULL, -- User who commented + comment TEXT NOT NULL, + + CONSTRAINT `fk_user_profile` FOREIGN KEY (profile_id) REFERENCES users (id), + CONSTRAINT `fk_user_commenter` FOREIGN KEY (user_id) REFERENCES users (id) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; \ No newline at end of file -- cgit v1.2.3 From dd843f687bc90c39a36497c5c28c3f9e38562b8b Mon Sep 17 00:00:00 2001 From: LarsVomMars Date: Sat, 10 Oct 2020 12:05:53 +0200 Subject: Dynamic question loading --- profile/index.js | 12 ++++++++---- profile/public/index.html | 6 ++---- profile/public/script.js | 14 ++++++++++++++ profile/public/style.css | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 8 deletions(-) diff --git a/profile/index.js b/profile/index.js index 4bfc1f7..2f1cb50 100644 --- a/profile/index.js +++ b/profile/index.js @@ -2,14 +2,16 @@ const express = require("express"); const db = require("../db"); const app = express.Router(); -app.use("/", express.static(__dirname + "/public/")); +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]); + 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); @@ -18,7 +20,9 @@ app.get("/api/questions", async (req, res) => { res.json(questions); }); -app.post("/api/add", async (req, res) => {}); +app.post("/api/add", async (req, res) => { + await db.query("INSERT INTO profile_answers (question_id, user_id, answer) VALUES (?, ?, ?)"); +}); app.put("/api/update", async (req, res) => {}); @@ -31,4 +35,4 @@ app.put("/api/comment", async (req, res) => {}); app.delete("/api/comment", async (req, res) => {}); -module.exports = app; \ No newline at end of file +module.exports = app; diff --git a/profile/public/index.html b/profile/public/index.html index 30f9de8..5fcee74 100644 --- a/profile/public/index.html +++ b/profile/public/index.html @@ -21,14 +21,12 @@
Steckbrief - -
- + - \ No newline at end of file + diff --git a/profile/public/script.js b/profile/public/script.js index e69de29..7c72b69 100644 --- a/profile/public/script.js +++ b/profile/public/script.js @@ -0,0 +1,14 @@ +const fs = document.querySelector("fieldset"); + +function appendQuestions(question) { + const field = document.createElement("input"); + field.name = question.id; + field.value = question.answer ?? ""; + field.placeholder = question.question; + fs.insertBefore(field, fs.querySelector("button")); +} + +fetch("api/questions") + .then((response) => response.json()) + .then((response) => response.forEach(appendQuestions)) + .catch(console.error); diff --git a/profile/public/style.css b/profile/public/style.css index e69de29..2e7b945 100644 --- a/profile/public/style.css +++ b/profile/public/style.css @@ -0,0 +1,39 @@ +html, +body { + padding: 0; + margin: 0; + height: 100%; + width: 100%; + background-color: #eec0c6; + background-image: linear-gradient(315deg, #eec0c6 0%, #7ee8fa 74%); +} + +div { + background: white; +} + +main { + position: absolute; + max-height: 80%; + overflow-y: auto; + width: 30%; + left: 50%; + top: 50%; + -webkit-transform: translate(-50%, -50%); + transform: translate(-50%, -50%); + padding: 20px; + border-radius: 10px; + background: white; +} + +input, +button, +select { + width: 100%; +} + +@media only screen and (max-width: 600px) { + main { + width: calc(100% - 50px); + } +} -- cgit v1.2.3 From d99c8fd5281d32b6f1ef5174e403f0b0ee9ad522 Mon Sep 17 00:00:00 2001 From: LarsVomMars Date: Sat, 10 Oct 2020 13:17:45 +0200 Subject: It's working Kinda --- profile/index.js | 41 ++++++++++++++++++++++++++++++---- profile/public/index.html | 56 ++++++++++++++++++++++++----------------------- profile/public/script.js | 43 ++++++++++++++++++++++++++++++++++-- 3 files changed, 107 insertions(+), 33 deletions(-) diff --git a/profile/index.js b/profile/index.js index 2f1cb50..f5e8373 100644 --- a/profile/index.js +++ b/profile/index.js @@ -2,10 +2,15 @@ const express = require("express"); const db = require("../db"); const app = express.Router(); -app.use("/", express.static(__dirname + "/public")); +app.use("/", express.static(__dirname + "/public/")); + +app.get("/user/:uid", async (req, res) => {}); // Basic API -app.get("/api/user", async (req, res) => {}); +app.get("/api/user", async (req, res) => { + const user = (await db.query("SELECT name, surname FROM users WHERE id = ?", [req.session.uid]))[0]; + res.json(user); +}); app.get("/api/questions", async (req, res) => { const questions = await db.query("SELECT id, question FROM profile_questions"); @@ -21,10 +26,38 @@ app.get("/api/questions", async (req, res) => { }); app.post("/api/add", async (req, res) => { - await db.query("INSERT INTO profile_answers (question_id, user_id, answer) VALUES (?, ?, ?)"); + try { + for (let qid in req.body) { + if (!req.body.hasOwnProperty(qid)) continue; + await db.query("INSERT INTO profile_answers (question_id, user_id, answer) VALUES (?, ?, ?)", [ + qid, + req.session.uid, + req.body[qid].replace(//g, ">"), + ]); + } + res.send("ok"); + } catch (e) { + console.error(e); + res.send("error"); + } }); -app.put("/api/update", async (req, res) => {}); +app.put("/api/update", async (req, res) => { + try { + for (let qid in req.body) { + if (!req.body.hasOwnProperty(qid)) continue; + await db.query("UPDATE profile_answers SET answer = ? WHERE question_id = ? AND user_id = ?", [ + req.body[qid].replace(//g, ">"), + qid, + req.session.uid, + ]); + } + res.send("ok"); + } catch (e) { + console.error(e); + res.send("error"); + } +}); // Comments API app.get("/api/comments/:uid", async (req, res) => {}); diff --git a/profile/public/index.html b/profile/public/index.html index 5fcee74..fd7b507 100644 --- a/profile/public/index.html +++ b/profile/public/index.html @@ -1,32 +1,34 @@ + + + + + + Steckbrief + - - - - - - Steckbrief - - - -
- Home - Logout -
-
- -
-
- Steckbrief - - - -
-
-
- - + +
+ Home + Logout +
+
+

+
+
+ Steckbrief + + +
+
+
+ + diff --git a/profile/public/script.js b/profile/public/script.js index 7c72b69..b0434fa 100644 --- a/profile/public/script.js +++ b/profile/public/script.js @@ -1,13 +1,52 @@ const fs = document.querySelector("fieldset"); +const form = document.querySelector("form"); +let init = true; + +function updateHeading(user) { + document.getElementById("username").textContent = `${user.name} ${user.surname}`; +} function appendQuestions(question) { + const div = document.createElement("div"); + + const label = document.createElement("label"); + label.for = "id_" + question.id; + label.textContent = question.question; + const field = document.createElement("input"); + field.id = "id_" + question.id; field.name = question.id; - field.value = question.answer ?? ""; + if (question.answer !== undefined) init = false; + field.value = question.answer; field.placeholder = question.question; - fs.insertBefore(field, fs.querySelector("button")); + + div.append(label, field); + fs.insertBefore(div, fs.querySelector("button")); } +form.addEventListener("submit", async (evt) => { + evt.preventDefault(); + const url = init ? "api/add" : "api/update"; + const method = init ? "POST" : "PUT"; + + const inputs = form.querySelectorAll("input"); + const body = {}; + for (const input of inputs) body[input.name] = input.value; + + const resp = await fetch(url, { + headers: { "Content-Type": "application/json" }, + method, + body: JSON.stringify(body), + }); + const res = await resp.text(); + if (res !== "ok") alert("AHHHH"); +}); + +fetch("api/user") + .then((response) => response.json()) + .then(updateHeading) + .catch(console.error); + fetch("api/questions") .then((response) => response.json()) .then((response) => response.forEach(appendQuestions)) -- cgit v1.2.3