{"size": 2006, "ext": "js", "max_stars_repo_path": "vilanda-express/routes/login/index.js", "max_stars_repo_name": "Lamborshea/vilanda", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "vilanda-express/routes/login/index.js", "max_issues_repo_name": "Lamborshea/vilanda", "max_issues_repo_issues_event_min_datetime": "2020-09-06T03:24:27.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-25T19:03:30.000Z", "max_forks_repo_path": "vilanda-express/routes/login/index.js", "max_forks_repo_name": "Lamborshea/vilanda", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 13, "max_forks_count": null, "avg_line_length": 22.5393258427, "max_line_length": 60, "alphanum_fraction": 0.5164506481}
const express = require("express");
const objectid = require("mongoose").objectid;
const router = express.Router();
const Code = require("../../constants/error_code_constant");
const Msg = require("../../constants/message_constant");
const User = require("../../models/user");
router.post("/", (req, res, next) => {
  const requestBody = req.body;
  if (requestBody.username && requestBody.password) {
    User.findOne(
      {
        username: requestBody.username,
        password: requestBody.password
      },
      (err, result) => {
        if (err) console.log(err);
        res.json({
          code: Code.SUCCESS,
          data: {
            username: result.username,
            _id: result._id,
            avatar: result.avatar
          },
          msg: Msg.SUCCESS
        });
        next();
      }
    );
  } else {
    res.json({
      code: Code.SYSTEM_EROOR,
      data: null,
      msg: "无效的参数"
    });
    next();
  }
});

router.post("/register", (req, res, next) => {
  const requestBody = req.body;
  if (requestBody.username && requestBody.password) {
    let user = new User({
      username: requestBody.username,
      password: requestBody.password
    });
    user.save((err, result) => {
      if (err) {
        res.json({
          code: Code.SYSTEM_EROOR,
          data: err,
          msg: Msg.SYSTEM_EROOR
        });
        next();
      } else {
        res.json({
          code: Code.SUCCESS,
          data: {
            username: result.username,
            createdTime: result.created_time
          },
          msg: Msg.SUCCESS
        });
        next();
      }
    });
  } else {
    res.json({
      code: Code.SYSTEM_EROOR,
      data: null,
      msg: "无效的参数"
    });
    next();
  }
});

router.get("/users", (req, res, next) => {
  User.find({}, (error, result) => {
    if (error) console.log(error);
    res.json({
      code: Code.SUCCESS,
      data: result,
      msg: Msg.SUCCESS
    });
    next();
  });
});

module.exports = router;
