{"size": 1647, "ext": "js", "max_stars_repo_path": "backend/src/controllers/UserController.js", "max_stars_repo_name": "brunosousadev/avicena_selection", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "backend/src/controllers/UserController.js", "max_issues_repo_name": "brunosousadev/avicena_selection", "max_issues_repo_issues_event_min_datetime": "2021-05-08T20:09:45.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-02T07:57:40.000Z", "max_forks_repo_path": "backend/src/controllers/UserController.js", "max_forks_repo_name": "brunosousadev/avicena_selection", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 5, "max_forks_count": null, "avg_line_length": 21.1153846154, "max_line_length": 90, "alphanum_fraction": 0.5154826958}
const User = require('../models/User');

module.exports = {
  async index(req, res) {
    try {
      const users = await User.findAll();

      return res.json(users);
    } catch (error) {
      return res.status(500).send();
    }


  },
  async show(req, res) {
    try {
      const { id } = req.params;
      const user = await User.findByPk(id);

      if (user) {
        return res.json(user);
      } else {
        return res.json({ Infor: `User with id ${id} does not exist` });
      }
    } catch (error) {
      return res.status(500).send();
    }



  },
  async update(req, res) {

    try {
      const { first, last } = req.body;
      const { id } = req.params;

      let user = await User.findByPk(id);
      if (user) {
        user = await User.update({ first, last }, { returning: true, where: { id: id } });
      } else {
        return res.status(204).json({ Infor: `User with id ${id} does not exist` });
      }

      return res.json(user);
    } catch (error) {
      return res.status(500).send();
    }



  },
  async store(req, res) {
    try {
      const { first, last } = req.body;
      const user = await User.create({ first, last });

      return res.json(user);
    } catch (error) {
      return res.status(500).send();
    }
  },

  async destroy(req, res) {
    try {
      const { id } = req.params;
      const user = await User.findByPk(id);
      if (user) {
        await user.destroy();
        return res.status(204).json();
      } else {
        return res.json({ Infor: `User with id ${id} does not exist` });
      }
    } catch (error) {
      return res.status(500).send();
    }
  }
};