HyeonJun Jeon

[Style] Separate dbRouter

1 const mysql = require("mysql"); 1 const mysql = require("mysql");
2 +const mysql2 = require("mysql2/promise");
2 const fs = require("fs"); 3 const fs = require("fs");
3 const parseICal = require("./ICal"); 4 const parseICal = require("./ICal");
4 5
...@@ -61,8 +62,19 @@ function jcalToSQL(jcal, userID) { ...@@ -61,8 +62,19 @@ function jcalToSQL(jcal, userID) {
61 62
62 // iCal의 소유주를 DB에 등록 후 userID get 63 // iCal의 소유주를 DB에 등록 후 userID get
63 // subjectID가 subjects, userID가 users에 있어야함 64 // subjectID가 subjects, userID가 users에 있어야함
64 -const fdata = fs.readFileSync("C:/Users/teddy/Downloads/data.ics", "utf8");
65 -const jcal = parseICal(fdata);
66 -jcalToSQL(jcal, 1);
67 65
68 -module.exports = jcalToSQL; 66 +// const fdata = fs.readFileSync("C:/Users/teddy/Downloads/data.ics", "utf8");
67 +// const jcal = parseICal(fdata);
68 +// jcalToSQL(jcal, 1);
69 +
70 +const [id, pw] = fs
71 + .readFileSync("server/libs/sql.pvdata", "utf8")
72 + .split("\r\n");
73 +const connectOption = {
74 + host: "localhost",
75 + user: id,
76 + password: pw,
77 + database: "db",
78 +};
79 +
80 +module.exports = { jcalToSQL, connectOption };
......
1 const express = require("express"); 1 const express = require("express");
2 -const mysql = require("mysql2/promise"); 2 +const dbRouter = express.Router();
3 -const fs = require("fs");
4 -const router = express.Router();
5 3
6 -async function route() { 4 +const schedules_dateRouter = require("./schedules_date");
7 - const [id, pw] = fs 5 +const subjectsRouter = require("./Subjects");
8 - .readFileSync("server/libs/sql.pvdata", "utf8") 6 +const userSubjectRouter = require("./user-subject");
9 - .split("\r\n"); 7 +const usersRouter = require("./Users");
10 - const connection = await mysql.createConnection({
11 - host: "localhost",
12 - user: id,
13 - password: pw,
14 - database: "db",
15 - });
16 -
17 - // (userID, date) => schedules
18 - router.get("/schedules_date", async (req, res) => {
19 - console.log("/db/schedules_date");
20 - try {
21 - const queryString = `
22 - SELECT sc.label, sc.type, sc.description, sc.url, sc.detail, sbj.name, us.color
23 - FROM schedules_date sc
24 - INNER JOIN \`user-subject\` us
25 - ON sc.userID = us.userID
26 - AND sc.subjectID = us.subjectID
27 - AND us.status = 1
28 - INNER JOIN subjects sbj
29 - ON sc.subjectID = sbj.ID
30 - WHERE sc.date = "${req.query.date}"
31 - AND sc.userID = ${req.query.userID}
32 - AND sc.status = 1`;
33 - const [results] = await connection.query(queryString);
34 - res.send(results);
35 - } catch (e) {
36 - console.log(e);
37 - res.end();
38 - }
39 - });
40 -
41 - // (ID) => null | name //unused
42 - router.get("/subjects", async (req, res) => {
43 - console.log("/db/subjects");
44 - try {
45 - const queryString = `
46 - SELECT name FROM subjects sbj
47 - WHERE sbj.ID = ${req.query.ID}`;
48 - const [results] = await connection.query(queryString);
49 - res.send(results.length && results[0].name);
50 - } catch (e) {
51 - console.log(e);
52 - res.end();
53 - }
54 - });
55 -
56 - // (loginID) => null | ID(str)
57 - router.get("/users", async (req, res) => {
58 - console.log("/db/users");
59 - try {
60 - const queryString = `
61 - SELECT ID FROM users us
62 - WHERE us.loginID = '${req.query.loginID}'`;
63 - const [results] = await connection.query(queryString);
64 - res.send(results.length ? results[0].ID.toString() : null);
65 - } catch (e) {
66 - console.log(e);
67 - res.end();
68 - }
69 - });
70 8
71 - // (loginID, loginPW) => null | "correct" 9 +async function route() {
72 - router.get("/users/check", async (req, res) => { 10 + dbRouter.use("/user-subject", userSubjectRouter);
73 - console.log("/db/users/check"); 11 + dbRouter.use("/users", usersRouter);
74 - try { 12 + dbRouter.use("/subjects", subjectsRouter);
75 - const queryString = ` 13 + dbRouter.use("/schedules_date", schedules_dateRouter);
76 - SELECT loginPW FROM users us
77 - WHERE us.loginID = '${req.query.loginID}'`;
78 - const [results] = await connection.query(queryString);
79 - res.send(results[0].loginPW === req.query.loginPW ? "correct" : null);
80 - } catch (e) {
81 - console.log(e);
82 - res.end();
83 - }
84 - });
85 -
86 - // (userID) => subjects
87 - router.get("/user-subject", async (req, res) => {
88 - console.log("/db/user-subject");
89 - try {
90 - const queryString = `
91 - SELECT subjectID, nickname, status, color FROM \`user-subject\` us
92 - WHERE us.userID = ${req.query.userID}`;
93 - const [results] = await connection.query(queryString);
94 - res.send(results);
95 - } catch (e) {
96 - console.log(e);
97 - res.end();
98 - }
99 - });
100 -
101 - router.get("*", (req, res) => {
102 - console.log("/db/*");
103 - res.end();
104 - });
105 } 14 }
106 15
107 route(); 16 route();
108 17
109 -module.exports = router; 18 +module.exports = dbRouter;
......
1 +const express = require("express");
2 +const mysql2 = require("mysql2/promise");
3 +const { connectOption } = require("../libs/MySQL");
4 +const schedules_dateRouter = express.Router();
5 +
6 +async function route() {
7 + const connection = await mysql2.createConnection(connectOption);
8 +
9 + // (userID, date) => schedules
10 + schedules_dateRouter.get("/", async (req, res) => {
11 + console.log("/db/schedules_date");
12 + try {
13 + const queryString = `
14 + SELECT sc.label, sc.type, sc.description, sc.url, sc.detail, sbj.name, us.color
15 + FROM schedules_date sc
16 + INNER JOIN \`user-subject\` us
17 + ON sc.userID = us.userID
18 + AND sc.subjectID = us.subjectID
19 + AND us.status = 1
20 + INNER JOIN subjects sbj
21 + ON sc.subjectID = sbj.ID
22 + WHERE sc.date = "${req.query.date}"
23 + AND sc.userID = ${req.query.userID}
24 + AND sc.status = 1`;
25 + const [results] = await connection.query(queryString);
26 + res.send(results);
27 + } catch (e) {
28 + console.log(e);
29 + res.end();
30 + }
31 + });
32 +}
33 +route();
34 +
35 +module.exports = schedules_dateRouter;
1 +const express = require("express");
2 +const mysql2 = require("mysql2/promise");
3 +const { connectOption } = require("../libs/MySQL");
4 +const subjectsRouter = express.Router();
5 +
6 +async function route() {
7 + const connection = await mysql2.createConnection(connectOption);
8 +
9 + // (ID) => null | name //unused
10 + subjectsRouter.get("/", async (req, res) => {
11 + console.log("/db/subjects");
12 + try {
13 + const queryString = `
14 + SELECT name FROM subjects sbj
15 + WHERE sbj.ID = ${req.query.ID}`;
16 + const [results] = await connection.query(queryString);
17 + res.send(results.length && results[0].name);
18 + } catch (e) {
19 + console.log(e);
20 + res.end();
21 + }
22 + });
23 +}
24 +route();
25 +
26 +module.exports = subjectsRouter;
1 +const express = require("express");
2 +const mysql2 = require("mysql2/promise");
3 +const { connectOption } = require("../libs/MySQL");
4 +const userSubjectRouter = express.Router();
5 +
6 +async function route() {
7 + const connection = await mysql2.createConnection(connectOption);
8 +
9 + // (userID) => subjects
10 + userSubjectRouter.get("/", async (req, res) => {
11 + console.log("/db/user-subject");
12 + try {
13 + const queryString = `
14 + SELECT subjectID, nickname, status, color FROM \`user-subject\` us
15 + WHERE us.userID = ${req.query.userID}`;
16 + const [results] = await connection.query(queryString);
17 + res.send(results);
18 + } catch (e) {
19 + console.log(e);
20 + res.end();
21 + }
22 + });
23 +}
24 +route();
25 +
26 +module.exports = userSubjectRouter;
1 +const express = require("express");
2 +const mysql2 = require("mysql2/promise");
3 +const { connectOption } = require("../libs/MySQL");
4 +const usersRouter = express.Router();
5 +
6 +async function route() {
7 + const connection = await mysql2.createConnection(connectOption);
8 +
9 + // (loginID) => null | ID(str)
10 + usersRouter.get("/", async (req, res) => {
11 + console.log("/db/users");
12 + try {
13 + const queryString = `
14 + SELECT ID FROM users us
15 + WHERE us.loginID = '${req.query.loginID}'`;
16 + const [results] = await connection.query(queryString);
17 + res.send(results.length ? results[0].ID.toString() : null);
18 + } catch (e) {
19 + console.log(e);
20 + res.end();
21 + }
22 + });
23 +
24 + // (loginID, loginPW) => null | "correct"
25 + usersRouter.get("/check", async (req, res) => {
26 + console.log("/db/users/check");
27 + try {
28 + const queryString = `
29 + SELECT loginPW FROM users us
30 + WHERE us.loginID = '${req.query.loginID}'`;
31 + const [results] = await connection.query(queryString);
32 + res.send(results[0].loginPW === req.query.loginPW ? "correct" : null);
33 + } catch (e) {
34 + console.log(e);
35 + res.end();
36 + }
37 + });
38 +}
39 +route();
40 +
41 +module.exports = usersRouter;
1 const express = require("express"); 1 const express = require("express");
2 -const bodyParser = require("body-parser");
3 const fs = require("fs"); 2 const fs = require("fs");
4 const cors = require("cors"); 3 const cors = require("cors");
5 -const { getPeed, getSubjects, login } = require("./libs/E_Campus.js");
6 4
7 const loginRouter = require("./routers/Login"); 5 const loginRouter = require("./routers/Login");
8 const dbRouter = require("./routers/DB"); 6 const dbRouter = require("./routers/DB");
...@@ -21,8 +19,7 @@ app.use(express.json()); ...@@ -21,8 +19,7 @@ app.use(express.json());
21 app.use(express.urlencoded({ extended: true })); 19 app.use(express.urlencoded({ extended: true }));
22 20
23 app.post("/", (req, res) => { 21 app.post("/", (req, res) => {
24 - res.send({ body: req.body }); 22 + console.log("/");
25 - console.log("listened /");
26 }); 23 });
27 24
28 app.use("/login", loginRouter); 25 app.use("/login", loginRouter);
......