{"size": 1245, "ext": "js", "max_stars_repo_path": "server/api/users.js", "max_stars_repo_name": "ComitiesCandescent/grace-shopper", "max_stars_repo_stars_event_min_datetime": "2018-08-03T15:39:02.000Z", "max_stars_repo_stars_event_max_datetime": "2018-08-05T02:35:12.000Z", "max_issues_repo_path": "server/api/users.js", "max_issues_repo_name": "ComitiesCandescent/grace-shopper", "max_issues_repo_issues_event_min_datetime": "2018-07-25T19:14:37.000Z", "max_issues_repo_issues_event_max_datetime": "2018-08-01T15:35:06.000Z", "max_forks_repo_path": "server/api/users.js", "max_forks_repo_name": "ComitiesCandescent/grace-shopper", "max_forks_repo_forks_event_min_datetime": "2018-08-08T02:36:54.000Z", "max_forks_repo_forks_event_max_datetime": "2018-10-19T16:40:00.000Z"}
{"max_stars_count": 2, "max_issues_count": 21, "max_forks_count": 2, "avg_line_length": 22.6363636364, "max_line_length": 80, "alphanum_fraction": 0.5951807229}
const router = require(`express`).Router()
const { User } = require(`../db/models`)
module.exports = router


// POST /api/users
router.post(`/`, async (req, res, next) => {
  try {
    const newUser = await User.create(req.body)
    req.login(newUser, err => (err ? next(err) : res.status(201).json(newUser)))
  } catch (error) {
    next(error)
  }
})

// GET /api/users/:userId
router.get(`/:userId`, async (req, res, next) => {
  try {
    const singleUser = await User.findById(Number(req.params.userId))
    res.json(singleUser)
  } catch (err) {
    next(err)
  }
})

// GET /api/users/email/:email
router.get(`/email/:email`, async (req, res, next) => {
  try {
    const singleUser = await User.findByEmail(req.params.email)
    req.user = singleUser
    req.login(singleUser, err => (err ? next(err) : res.json(singleUser)))
  } catch (err) {
    next(err)
  }
})

// GET /api/users
router.get(`/`, async (req, res, next) => {
  try {
    const users = await User.findAll()
    res.json(users)
  } catch (err) {
    next(err)
  }
})

// DELETE /api/users/:userId
router.delete(`/:userId`, (req, res, next) => {
  User.destroy({ where: { id: req.params.userId } })
    .then(() => {
      res.sendStatus(204)
    })
    .catch(next)
})
