HyeonJun Jeon

[Style] Separate dbRouter

const mysql = require("mysql");
const mysql2 = require("mysql2/promise");
const fs = require("fs");
const parseICal = require("./ICal");
......@@ -61,8 +62,19 @@ function jcalToSQL(jcal, userID) {
// iCal의 소유주를 DB에 등록 후 userID get
// subjectID가 subjects, userID가 users에 있어야함
const fdata = fs.readFileSync("C:/Users/teddy/Downloads/data.ics", "utf8");
const jcal = parseICal(fdata);
jcalToSQL(jcal, 1);
module.exports = jcalToSQL;
// const fdata = fs.readFileSync("C:/Users/teddy/Downloads/data.ics", "utf8");
// const jcal = parseICal(fdata);
// jcalToSQL(jcal, 1);
const [id, pw] = fs
.readFileSync("server/libs/sql.pvdata", "utf8")
.split("\r\n");
const connectOption = {
host: "localhost",
user: id,
password: pw,
database: "db",
};
module.exports = { jcalToSQL, connectOption };
......
const express = require("express");
const mysql = require("mysql2/promise");
const fs = require("fs");
const router = express.Router();
const dbRouter = express.Router();
async function route() {
const [id, pw] = fs
.readFileSync("server/libs/sql.pvdata", "utf8")
.split("\r\n");
const connection = await mysql.createConnection({
host: "localhost",
user: id,
password: pw,
database: "db",
});
// (userID, date) => schedules
router.get("/schedules_date", async (req, res) => {
console.log("/db/schedules_date");
try {
const queryString = `
SELECT sc.label, sc.type, sc.description, sc.url, sc.detail, sbj.name, us.color
FROM schedules_date 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.date = "${req.query.date}"
AND sc.userID = ${req.query.userID}
AND sc.status = 1`;
const [results] = await connection.query(queryString);
res.send(results);
} catch (e) {
console.log(e);
res.end();
}
});
// (ID) => null | name //unused
router.get("/subjects", async (req, res) => {
console.log("/db/subjects");
try {
const queryString = `
SELECT name FROM subjects sbj
WHERE sbj.ID = ${req.query.ID}`;
const [results] = await connection.query(queryString);
res.send(results.length && results[0].name);
} catch (e) {
console.log(e);
res.end();
}
});
// (loginID) => null | ID(str)
router.get("/users", async (req, res) => {
console.log("/db/users");
try {
const queryString = `
SELECT ID FROM users us
WHERE us.loginID = '${req.query.loginID}'`;
const [results] = await connection.query(queryString);
res.send(results.length ? results[0].ID.toString() : null);
} catch (e) {
console.log(e);
res.end();
}
});
const schedules_dateRouter = require("./schedules_date");
const subjectsRouter = require("./Subjects");
const userSubjectRouter = require("./user-subject");
const usersRouter = require("./Users");
// (loginID, loginPW) => null | "correct"
router.get("/users/check", async (req, res) => {
console.log("/db/users/check");
try {
const queryString = `
SELECT loginPW FROM users us
WHERE us.loginID = '${req.query.loginID}'`;
const [results] = await connection.query(queryString);
res.send(results[0].loginPW === req.query.loginPW ? "correct" : null);
} catch (e) {
console.log(e);
res.end();
}
});
// (userID) => subjects
router.get("/user-subject", async (req, res) => {
console.log("/db/user-subject");
try {
const queryString = `
SELECT subjectID, nickname, status, color FROM \`user-subject\` us
WHERE us.userID = ${req.query.userID}`;
const [results] = await connection.query(queryString);
res.send(results);
} catch (e) {
console.log(e);
res.end();
}
});
router.get("*", (req, res) => {
console.log("/db/*");
res.end();
});
async function route() {
dbRouter.use("/user-subject", userSubjectRouter);
dbRouter.use("/users", usersRouter);
dbRouter.use("/subjects", subjectsRouter);
dbRouter.use("/schedules_date", schedules_dateRouter);
}
route();
module.exports = router;
module.exports = dbRouter;
......
const express = require("express");
const mysql2 = require("mysql2/promise");
const { connectOption } = require("../libs/MySQL");
const schedules_dateRouter = express.Router();
async function route() {
const connection = await mysql2.createConnection(connectOption);
// (userID, date) => schedules
schedules_dateRouter.get("/", async (req, res) => {
console.log("/db/schedules_date");
try {
const queryString = `
SELECT sc.label, sc.type, sc.description, sc.url, sc.detail, sbj.name, us.color
FROM schedules_date 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.date = "${req.query.date}"
AND sc.userID = ${req.query.userID}
AND sc.status = 1`;
const [results] = await connection.query(queryString);
res.send(results);
} catch (e) {
console.log(e);
res.end();
}
});
}
route();
module.exports = schedules_dateRouter;
const express = require("express");
const mysql2 = require("mysql2/promise");
const { connectOption } = require("../libs/MySQL");
const subjectsRouter = express.Router();
async function route() {
const connection = await mysql2.createConnection(connectOption);
// (ID) => null | name //unused
subjectsRouter.get("/", async (req, res) => {
console.log("/db/subjects");
try {
const queryString = `
SELECT name FROM subjects sbj
WHERE sbj.ID = ${req.query.ID}`;
const [results] = await connection.query(queryString);
res.send(results.length && results[0].name);
} catch (e) {
console.log(e);
res.end();
}
});
}
route();
module.exports = subjectsRouter;
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) => subjects
userSubjectRouter.get("/", async (req, res) => {
console.log("/db/user-subject");
try {
const queryString = `
SELECT subjectID, nickname, status, color FROM \`user-subject\` us
WHERE us.userID = ${req.query.userID}`;
const [results] = await connection.query(queryString);
res.send(results);
} catch (e) {
console.log(e);
res.end();
}
});
}
route();
module.exports = userSubjectRouter;
const express = require("express");
const mysql2 = require("mysql2/promise");
const { connectOption } = require("../libs/MySQL");
const usersRouter = express.Router();
async function route() {
const connection = await mysql2.createConnection(connectOption);
// (loginID) => null | ID(str)
usersRouter.get("/", async (req, res) => {
console.log("/db/users");
try {
const queryString = `
SELECT ID FROM users us
WHERE us.loginID = '${req.query.loginID}'`;
const [results] = await connection.query(queryString);
res.send(results.length ? results[0].ID.toString() : null);
} catch (e) {
console.log(e);
res.end();
}
});
// (loginID, loginPW) => null | "correct"
usersRouter.get("/check", async (req, res) => {
console.log("/db/users/check");
try {
const queryString = `
SELECT loginPW FROM users us
WHERE us.loginID = '${req.query.loginID}'`;
const [results] = await connection.query(queryString);
res.send(results[0].loginPW === req.query.loginPW ? "correct" : null);
} catch (e) {
console.log(e);
res.end();
}
});
}
route();
module.exports = usersRouter;
const express = require("express");
const bodyParser = require("body-parser");
const fs = require("fs");
const cors = require("cors");
const { getPeed, getSubjects, login } = require("./libs/E_Campus.js");
const loginRouter = require("./routers/Login");
const dbRouter = require("./routers/DB");
......@@ -21,8 +19,7 @@ app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.post("/", (req, res) => {
res.send({ body: req.body });
console.log("listened /");
console.log("/");
});
app.use("/login", loginRouter);
......