User-subject.js
1.32 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
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();
}
});
}
route();
module.exports = userSubjectRouter;