Schedules_repeat.js
1.8 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
const express = require("express");
const mysql2 = require("mysql2/promise");
const { connectOption } = require("../libs/MySQL");
const schedules_repeatRouter = express.Router();
async function route() {
const connection = await mysql2.createConnection(connectOption);
// (userID, day) => schedules
schedules_repeatRouter.get("/", async (req, res) => {
// console.log("/db/schedules_repeat");
try {
const queryString = `
SELECT sc.label, sc.type, sc.description, sc.uid, sc.url, sc.detail, sbj.name, us.nickname, us.color, sc.startTime, sc.endTime
FROM schedules_repeat sc
INNER JOIN \`user-subject\` us
ON sc.userID = us.userID
AND sc.subjectID = us.subjectID
AND us.status = 1
INNER JOIN subjects sbj
ON sc.subjectID = sbj.ID
WHERE sc.day = "${req.query.day}"
AND sc.userID = ${req.query.userID}
AND sc.status = 1
ORDER BY sc.startTime`;
const [results] = await connection.query(queryString);
res.send(results);
} catch (e) {
console.log(e);
res.end();
}
});
// (userID, label, subjectID, type, desciption, url, status, day, startTime, endTime)
schedules_repeatRouter.post("/", async (req, res) => {
console.log("/db/schedules_repeat");
try {
const queryString = `
INSERT INTO schedules_repeat (userID, label, subjectID, type, status, day, startTime, endTime)
VALUE (${req.body.userID}, ${req.body.label}, ${req.body.subjectID}, ${req.body.type},
${req.body.status}, ${req.body.day}, ${req.body.startTime}, ${req.body.endTime});
UPDATE schedules_repeat
SET uid = -id;`;
await connection.query(queryString);
res.end();
} catch (e) {
console.log(e);
res.end();
}
});
}
route();
module.exports = schedules_repeatRouter;