{"size": 2128, "ext": "js", "max_stars_repo_path": "api/routes/users/users-route.js", "max_stars_repo_name": "MSquared88/use-my-tools-be", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "api/routes/users/users-route.js", "max_issues_repo_name": "MSquared88/use-my-tools-be", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "api/routes/users/users-route.js", "max_forks_repo_name": "MSquared88/use-my-tools-be", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 24.1818181818, "max_line_length": 79, "alphanum_fraction": 0.5709586466}
const express = require("express");

//auth
const bcrypt = require("bcryptjs");

const createToken = require("../../Utils/auth/createToken");

//middleware
const restricted = require("../../Utils/auth/restricted-middleware");
const validateUser = require("../../Utils/auth/validateUser-middleware");

//dataBase
const usersModel = require("./users-model");

const router = express.Router();

router.post("/user/register", validateUser, (req, res) => {
  const user = req.body;

  const hash = bcrypt.hashSync(user.password, 10);
  user.password = hash;

  usersModel
    .add(user)
    .then((user) => {
      res.status(201).json(user);
    })
    .catch((err) => {
      res.status(500).json({ message: "could not add user to database", err });
    });
});

router.post("/user/login", (req, res) => {
  const { user_name, password } = req.body;
  usersModel
    .getBy({ user_name })
    .then((user) => {
      if (user && bcrypt.compareSync(password, user.password)) {
        const token = createToken(user);
        res.status(200).json({
          message: `Welcome ${user.username} to  Use my Tools`,
          token,
        });
      } else {
        res.status(401).json({ message: "invalid credentials" });
      }
    })
    .catch((err) => {
      res.status(500).json({ message: "database error", err });
    });
});

router.get("/user/restricted", restricted, (req, res) => {
  usersModel
    .get()
    .first()
    .then((users) => {
      res.status(200).json(users);
    })

    .catch((err) => {
      res.status(500).json({ message: "could not get users", err });
    });
});

router.put("/user/update", validateUser, restricted, (req, res) => {});

router.delete("/user/delete", restricted, (req, res) => {
  const id = req.userid;

  usersModel
    .remove(id)
    .then((user) => {
      if (user) {
        res.status(200).json(user);
      } else {
        res
          .status(400)
          .json({ message: "the user with that id does not exsist" });
      }
    })
    .catch((err) => {
      res.status(500).json({ message: "could not delete user", err });
    });
});

module.exports = router;
