var express = require("express");
const router = express.Router();
const db = require("../../utils/database");

router.post("/updateDepartment", (req, res) => {
  const id = req.body.id;
  const department = req.body.department;

  db.getConnection((err, connection) => {
    if (err) {
      console.log("database not connected error: >>>", err);
      throw err;
    }
    console.log("get Connection");
    let uQuer = `update finehhdi_eDefect.Department set 
      department='${department}'
      where id=${id}`;

    connection.query(uQuer, (err, data) => {
      connection.release();
      if (err) {
        if (err.code === "ER_DUP_ENTRY") {
          res.status(409).json({
            message: "Duplicate User",
            results: "Failed",
          });
        } else if (err.code === "ENOTFOUND") {
          res.status(404).json({
            message: "Internet Connection Failed",
            results: "failed",
          });
        } else {
          res.status(401).json({
            message: "dep creation failed",
            results: "Failed",
          });
        }
      } else {
        res.status(200).json({
          message: "Created Successfully",
          results: "Success",
        });
      }
    });
    console.log("release Connection");
  });
});

module.exports = router;
