var express = require("express");
const router = express.Router();
const db = require("../../utils/database");
const dbFuntion = require("../../utils/dbfunction");
const { response } = require("express");
//const adminMiddleware = require("../../utils/middleware/authMiddleware");

router.post("/updateUser", (req, res) => {
  const id = req.body.id;
  var email=req.body.email
  var username=req.body.username
  var isAdmin=req.body.isAdmin
  var password = req.body.password

  let uQuer = `update finehhdi_fineAD.users set 
    email='${email}',
    username='${username}', 
    isAdmin='${isAdmin}',
    password='${password}'
    where id=${id}`;

  db.query(uQuer, (err, data) => {
    if (err) {
      console.log("error ==>", 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{
      console.log(err);
      res.status(401).json({
        message: "creation failed",
        results: "Failed",
      });
      
    }
  } 
  else {
    
      res.status(200).json({
        message: "Updated Successfully",
        results: "Success",
      }); 
 
  }

   
  });
});

module.exports = router;
