{"size": 803, "ext": "js", "max_stars_repo_path": "Modulo 4 - MongoDB principles/Trabalho/routes/routes.js", "max_stars_repo_name": "Vicnasc/IGTI-FullStack-Bootcamp", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "Modulo 4 - MongoDB principles/Trabalho/routes/routes.js", "max_issues_repo_name": "Vicnasc/IGTI-FullStack-Bootcamp", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "Modulo 4 - MongoDB principles/Trabalho/routes/routes.js", "max_forks_repo_name": "Vicnasc/IGTI-FullStack-Bootcamp", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 20.5897435897, "max_line_length": 54, "alphanum_fraction": 0.6189290162}
const express = require('express');
const userModel = require('../models/User');
const routes = express();

// Ler usuários
routes.get('/', async (req, res, next) => {
  const user = await userModel.find({});
  res.send(user);
});

// Criar usuário
routes.post('/user', async (req, res, next) => {
  const user = new userModel(req.body);
  await user.save();
});

// Depósito
routes.patch('/user/:id', async (req, res, next) => {
  const user = await userModel.findOneAndModify(
    { _id: req.params.id },
    req.body,
    { new: true }
  );

  res.send(user);
});

routes.delete('/user/:id', async (req, res, next) => {
  const user = await userModel.findOneAndDelete();

  if (!user) {
    res.status(404).send('Usuário não encontrado');
  }

  res.status(200).send();
});

module.exports = routes;
