{"size": 1006, "ext": "js", "max_stars_repo_path": "desafio-03/src/app/controllers/StudentController.js", "max_stars_repo_name": "willduarte/gostack-gympoint", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "desafio-03/src/app/controllers/StudentController.js", "max_issues_repo_name": "willduarte/gostack-gympoint", "max_issues_repo_issues_event_min_datetime": "2021-04-17T07:46:03.000Z", "max_issues_repo_issues_event_max_datetime": "2021-04-17T07:46:04.000Z", "max_forks_repo_path": "desafio-03/src/app/controllers/StudentController.js", "max_forks_repo_name": "willduarte/gostack-gympoint", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 22.8636363636, "max_line_length": 80, "alphanum_fraction": 0.5914512922}
import Student from '../models/Student'

class StudentController {
  async store(req, res, next) {
    const isValid = await Student.validate(req.body)

    if (!isValid) {
      return res.status(422).json({ error: 'Invalid input' })
    }

    const { name, email, age, weight, height } = req.body

    const student = await Student.create({ name, email, age, weight, height })

    return res.json(student)
  }

  async update(req, res, next) {
    const isValid = await Student.validate(req.body)

    if (!isValid) {
      return res.status(422).json({ error: 'Invalid input' })
    }

    const student = await Student.findByPk(req.params.id)

    if (!student) {
      return res.status(404).json({ error: 'Student not found' })
    }

    const { name, email, age, weight, height } = await student.update(req.body)

    return res.json({
      name,
      email,
      age,
      weight,
      height,
    })
  }
}

export default new StudentController()
