DB.js 1.3 KB
const express = require("express");
const mysql2 = require("mysql2/promise");
const { connectOption } = 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();
    }
  });

  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;