User-subject.js
2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const express = require("express");
const mysql2 = require("mysql2/promise");
const { connectOption } = require("../libs/MySQL");
const userSubjectRouter = express.Router();
async function route() {
const connection = await mysql2.createConnection(connectOption);
// (userID) => [{userID, subjectID, nickname, status, color, name}, ...]
userSubjectRouter.get("/", async (req, res) => {
console.log("/db/user-subject");
try {
const queryString = `
SELECT us.userID, us.subjectID, us.nickname, us.status, us.color, sub.name
FROM \`user-subject\` us
INNER JOIN subjects sub
ON us.subjectID = sub.ID
WHERE us.userID = ${req.query.userID}`;
const [results] = await connection.query(queryString);
res.send(results);
} catch (e) {
console.log(e);
res.end();
}
});
// (userID, subjectID, status)
userSubjectRouter.put("/check", async (req, res) => {
console.log("/db/user-subject/check");
try {
const queryString = `
UPDATE \`user-subject\`
SET status = ${req.body.status}
WHERE userID = ${req.body.userID}
AND subjectID = ${req.body.subjectID}`;
await connection.query(queryString);
res.end();
} catch (e) {
console.log(e);
res.end();
}
});
// (userID, subjectID, nickname, color)
userSubjectRouter.put("/modify", async (req, res) => {
console.log("/db/user-subject/modify");
try {
const queryString = `
UPDATE \`user-subject\`
SET color = '${req.body.color}',
nickname = '${req.body.nickname}'
WHERE userID = ${req.body.userID}
AND subjectID = ${req.body.subjectID}`;
await connection.query(queryString);
res.end();
} catch (e) {
console.log(e);
res.end();
}
});
// (userID, [subjectID, ...])
userSubjectRouter.post("/s", async (req, res) => {
console.log("post /db/user-subject/s");
const colors = [
"3ADF00",
"0040FF",
"FF0000",
"FFFF00",
"FF00FF",
"FF8000",
"6E6E6E",
"8000FF",
"B40431",
"2EFEF7",
];
try {
for (const i in req.body.subList) {
const queryString = `
INSERT INTO \`user-subject\`
(userID, subjectID, status, color)
VALUES (${req.body.userID}, ${req.body.subList[i].subjectID}, 1, '${colors[i]}')`;
await connection.query(queryString);
}
res.end();
} catch (e) {
console.log(e);
res.end();
}
});
}
route();
module.exports = userSubjectRouter;