{"size": 1433, "ext": "js", "max_stars_repo_path": "controllers/api/dodger-routes.js", "max_stars_repo_name": "Calterat/artfulDodger", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "controllers/api/dodger-routes.js", "max_issues_repo_name": "Calterat/artfulDodger", "max_issues_repo_issues_event_min_datetime": "2021-01-29T02:55:19.000Z", "max_issues_repo_issues_event_max_datetime": "2021-02-04T23:06:53.000Z", "max_forks_repo_path": "controllers/api/dodger-routes.js", "max_forks_repo_name": "Calterat/artfulDodger", "max_forks_repo_forks_event_min_datetime": "2021-03-08T23:08:32.000Z", "max_forks_repo_forks_event_max_datetime": "2021-03-08T23:08:32.000Z"}
{"max_stars_count": null, "max_issues_count": 12, "max_forks_count": 1, "avg_line_length": 33.3255813953, "max_line_length": 131, "alphanum_fraction": 0.6336357292}
const router = require('express').Router();
const { Dodger } = require('../../models');

// POST /api/dodgers  THIS IS THE REGISTER FORM SUBMIT
router.post('/', (req, res) => {
  console.log('BEFORE CREATION');
  Dodger.create({
    dodgername: req.body.dodgername,
    email: req.body.email,
    password: req.body.password,
    games_won: req.body.games_won,
    games_loss: req.body.games_loss,
  })
    .then(dbDodgerData => {
      const { dodgername, games_won, games_loss } = dbDodgerData.dataValues;
      res.json({ dodgername, games_won, games_loss, loggedIn: true });
    })
    .catch(err => res.status(500).json(err));
})

router.post('/login', (req, res) => {
  Dodger.findOne({
    where: { email: req.body.email }
  })
    .then(dbDodgerData => {
      if (!dbDodgerData) {
        res.status(400).json({ message: "A ball whiffs. No dodger with that email address!" });
        return;
      }

      const validPW = dbDodgerData.checkPassword(req.body.password);

      if (!validPW) {
        res.status(400).json({ message: "A ball comes from nowhere and blasts you in the face. Wrong Password!" });
        return;
      }
      const { dodgername, games_won, games_loss } = dbDodgerData.dataValues;
      res.json({ dodgername, games_won, games_loss, loggedIn: true, message: 'A ball magically appears in your hands! GAME ON!' });
    })
    .catch(err => res.status(500).json(err));
})

module.exports = router;