DB.js
1.78 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
const { default: axios } = require("axios");
const express = require("express");
const mysql2 = require("mysql2/promise");
const parseICal = require("../libs/ICal");
const { connectOption, jcalToSQL } = require("../libs/MySQL");
const dbRouter = express.Router();
const schedules_dateRouter = require("./Schedules_date");
const schedules_repeatRouter = require("./Schedules_repeat");
const schedules_timeRouter = require("./Schedules_time");
const subjectsRouter = require("./Subjects");
const userSubjectRouter = require("./User-subject");
const usersRouter = require("./Users");
async function route() {
const connection = await mysql2.createConnection(connectOption);
// (userID, uid)
dbRouter.delete("/schedule", async (req, res) => {
console.log("delete /db/schedule");
try {
const queryString = `
DELETE FROM \`${req.body.table}\` sc
WHERE sc.userID = ${req.body.userID}
AND sc.uid = ${req.body.uid}`;
await connection.query(queryString);
res.end();
} catch (e) {
console.log(e);
res.end();
}
});
// (userID, peed)
dbRouter.put("/ical", async (req, res) => {
console.log("put /db/ical");
try {
const { data: ical } = await axios({
url: req.body.peed,
method: "GET",
responseType: "blob",
});
await jcalToSQL(parseICal(ical), req.body.userID);
res.end();
} catch (e) {
console.log(e);
res.end();
}
});
dbRouter.use("/user-subject", userSubjectRouter);
dbRouter.use("/users", usersRouter);
dbRouter.use("/subjects", subjectsRouter);
dbRouter.use("/schedules_date", schedules_dateRouter);
dbRouter.use("/schedules_time", schedules_timeRouter);
dbRouter.use("/schedules_repeat", schedules_repeatRouter);
}
route();
module.exports = dbRouter;