{"size": 999, "ext": "js", "max_stars_repo_path": "server/api/users.js", "max_stars_repo_name": "shelbygluck/matador", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "server/api/users.js", "max_issues_repo_name": "shelbygluck/matador", "max_issues_repo_issues_event_min_datetime": "2020-05-21T23:30:13.000Z", "max_issues_repo_issues_event_max_datetime": "2020-05-21T23:30:14.000Z", "max_forks_repo_path": "server/api/users.js", "max_forks_repo_name": "shelbygluck/matador", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 3, "max_forks_count": null, "avg_line_length": 20.387755102, "max_line_length": 62, "alphanum_fraction": 0.5665665666}
/* eslint-disable no-unused-vars */
const router = require('express').Router()
const {User} = require('../db/models')
module.exports = router

router.get('/', async (req, res, next) => {
  try {
    const users = await User.findAll({
      attributes: ['id', 'email']
    })
    res.json(users)
  } catch (err) {
    next(err)
  }
})

router.get('/:email', async (req, res, next) => {
  try {
    let user = await User.findAll({
      where: {
        email: req.params.email
      }
    })
    res.send(user)
  } catch (err) {
    next(err)
  }
})

router.post('/:email', async (req, res, next) => {
  let numberOfAffectedRows, affectedRows
  let newBalance = Number(req.body.newBalance)
  try {
    ;[numberOfAffectedRows, affectedRows] = await User.update(
      {
        balance: newBalance
      },
      {
        where: {email: req.params.email},
        returning: true,
        plain: true
      }
    )
    res.send(affectedRows.dataValues.balance)
  } catch (err) {
    next(err)
  }
})
