diff options
Diffstat (limited to 'profile')
-rw-r--r-- | profile/index.js | 55 | ||||
-rw-r--r-- | profile/public/script.js | 27 | ||||
-rw-r--r-- | profile/public/style.css | 7 | ||||
-rw-r--r-- | profile/public/uploads/.gitkeep | 0 |
4 files changed, 69 insertions, 20 deletions
diff --git a/profile/index.js b/profile/index.js index f5e8373..1c5752f 100644 --- a/profile/index.js +++ b/profile/index.js @@ -1,10 +1,13 @@ const express = require("express"); const db = require("../db"); +const fileupload = require("express-fileupload"); const app = express.Router(); +app.use(fileupload({})); + app.use("/", express.static(__dirname + "/public/")); -app.get("/user/:uid", async (req, res) => {}); +app.get("/user/:uid", async (req, res) => { }); // Basic API app.get("/api/user", async (req, res) => { @@ -13,7 +16,7 @@ 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 questions = await db.query("SELECT q.id, q.question, t.type FROM profile_questions q INNER JOIN profile_input_types t ON t.id = q.question_type"); const answers = await db.query("SELECT answer, question_id FROM profile_answers WHERE user_id = ?", [ req.session.uid, ]); @@ -28,11 +31,27 @@ app.get("/api/questions", async (req, res) => { app.post("/api/add", async (req, res) => { try { for (let qid in req.body) { - if (!req.body.hasOwnProperty(qid)) continue; + if (!req.body.hasOwnProperty(qid) || req.body[qid] === "dbg-image") continue; + let answer = req.body[qid].replace(/</g, "<").replace(/>/g, ">"); + await db.query("INSERT INTO profile_answers (question_id, user_id, answer) VALUES (?, ?, ?)", [ + qid, + req.session.uid, + answer + ]); + } + for (let fid in req.files) { + if (!req.files.hasOwnProperty(fid)) return; + + let image, imageType, imageName; + + image = req.files[fid]; + imageType = image.name.split(".").reverse()[0]; + imageName = `${req.session.uid}_${(new Date()).getTime()}.${imageType}`; + image.mv(__dirname + "/public/uploads/" + imageName); await db.query("INSERT INTO profile_answers (question_id, user_id, answer) VALUES (?, ?, ?)", [ qid, req.session.uid, - req.body[qid].replace(/</g, "<").replace(/>/g, ">"), + imageName, ]); } res.send("ok"); @@ -45,13 +64,29 @@ app.post("/api/add", async (req, res) => { app.put("/api/update", async (req, res) => { try { for (let qid in req.body) { - if (!req.body.hasOwnProperty(qid)) continue; + if (!req.body.hasOwnProperty(qid) || req.body[qid] === "dbg-image") continue; + let answer = req.body[qid].replace(/</g, "<").replace(/>/g, ">"); await db.query("UPDATE profile_answers SET answer = ? WHERE question_id = ? AND user_id = ?", [ - req.body[qid].replace(/</g, "<").replace(/>/g, ">"), + answer, qid, req.session.uid, ]); } + for (let fid in req.files) { + if (!req.files.hasOwnProperty(fid)) return; + + let image, imageType, imageName; + + image = req.files[fid]; + imageType = image.name.split(".").reverse()[0]; + imageName = `${req.session.uid}_${(new Date()).getTime()}.${imageType}`; + image.mv(__dirname + "/public/uploads/" + imageName); + await db.query("UPDATE profile_answers SET answer = ? WHERE question_id = ? AND user_id = ?", [ + imageName, + fid, + req.session.uid, + ]); + } res.send("ok"); } catch (e) { console.error(e); @@ -60,12 +95,12 @@ app.put("/api/update", async (req, res) => { }); // Comments API -app.get("/api/comments/:uid", async (req, res) => {}); +app.get("/api/comments/:uid", async (req, res) => { }); -app.post("/api/comment", async (req, res) => {}); +app.post("/api/comment", async (req, res) => { }); -app.put("/api/comment", async (req, res) => {}); +app.put("/api/comment", async (req, res) => { }); -app.delete("/api/comment", async (req, res) => {}); +app.delete("/api/comment", async (req, res) => { }); module.exports = app; diff --git a/profile/public/script.js b/profile/public/script.js index b0434fa..a386f56 100644 --- a/profile/public/script.js +++ b/profile/public/script.js @@ -12,6 +12,14 @@ function appendQuestions(question) { const label = document.createElement("label"); label.for = "id_" + question.id; label.textContent = question.question; + div.appendChild(label); + + if (question.type === "file" && question.answer) { + const img = document.createElement("img"); + img.src = "uploads/" + question.answer; + img.alt = "Image"; + div.appendChild(img); + } const field = document.createElement("input"); field.id = "id_" + question.id; @@ -19,8 +27,10 @@ function appendQuestions(question) { if (question.answer !== undefined) init = false; field.value = question.answer; field.placeholder = question.question; + field.type = question.type; + if (question.type === "file") field.accept = "image/*"; - div.append(label, field); + div.appendChild(field); fs.insertBefore(div, fs.querySelector("button")); } @@ -30,14 +40,13 @@ form.addEventListener("submit", async (evt) => { 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 body = new FormData(); + for (const input of inputs) { + if (input.type !== "file") body.append(input.name, input.value); + else body.append(input.name, input.files[0] ?? "dbg-image"); + } + + const resp = await fetch(url, { method, body }); const res = await resp.text(); if (res !== "ok") alert("AHHHH"); }); diff --git a/profile/public/style.css b/profile/public/style.css index 2e7b945..e674e71 100644 --- a/profile/public/style.css +++ b/profile/public/style.css @@ -16,7 +16,7 @@ main { position: absolute; max-height: 80%; overflow-y: auto; - width: 30%; + width: 50%; left: 50%; top: 50%; -webkit-transform: translate(-50%, -50%); @@ -32,6 +32,11 @@ select { width: 100%; } +img { + max-width: 80%; + max-height: 80%; +} + @media only screen and (max-width: 600px) { main { width: calc(100% - 50px); diff --git a/profile/public/uploads/.gitkeep b/profile/public/uploads/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/profile/public/uploads/.gitkeep |