diff --git a/user/UserController.js b/user/UserController.js index cb7b5e8..94e70b5 100644 --- a/user/UserController.js +++ b/user/UserController.js @@ -1,56 +1,59 @@ -var express = require('express'); -var router = express.Router(); -var bodyParser = require('body-parser'); +const express = require('express'); +const app = express(); +const router = express.Router(); -router.use(bodyParser.urlencoded({ extended: true })); -router.use(bodyParser.json()); -var User = require('./User'); +app.use(express.urlencoded({ extended: true })); +app.use(express.json()); +const User = require('./User'); // CREATES A NEW USER -router.post('/', function (req, res) { - User.create({ - name : req.body.name, - email : req.body.email, - password : req.body.password - }, - function (err, user) { - if (err) return res.status(500).send("There was a problem adding the information to the database."); - res.status(200).send(user); - }); +router.post('/', (req, res) => { + const { name, email, password } = req.body; + + User.create({ name, email, password }, (err, user) => { + if (err) return res.status(500).json({ message: "There was a problem adding the information to the database." }); + return res.status(200).json(user); + }); }); // RETURNS ALL THE USERS IN THE DATABASE -router.get('/', function (req, res) { - User.find({}, function (err, users) { - if (err) return res.status(500).send("There was a problem finding the users."); - res.status(200).send(users); +router.get('/', (req, res) => { + User.find({}, (err, users) => { + if (err) return res.status(500).json({ message: "There was a problem finding the users." }); + return res.status(200).json(users); }); }); // GETS A SINGLE USER FROM THE DATABASE -router.get('/:id', function (req, res) { - User.findById(req.params.id, function (err, user) { - if (err) return res.status(500).send("There was a problem finding the user."); - if (!user) return res.status(404).send("No user found."); - res.status(200).send(user); +router.get('/:id', (req, res) => { + const { id } = req.params; + + User.findById(id, (err, user) => { + if (err) return res.status(500).json({ message: "There was a problem finding the user." }); + if (!user) return res.status(404).json({ message: "No user found." }); + return res.status(200).json(user); }); }); // DELETES A USER FROM THE DATABASE -router.delete('/:id', function (req, res) { - User.findByIdAndRemove(req.params.id, function (err, user) { - if (err) return res.status(500).send("There was a problem deleting the user."); - res.status(200).send("User: "+ user.name +" was deleted."); +router.delete('/:id', (req, res) => { + const { id } = req.params; + + User.findByIdAndRemove(id, (err, user) => { + if (err) return res.status(500).json({ message: "There was a problem deleting the user." }); + return res.status(200).json({ message: `User: ${user.name} was deleted.` }); }); }); // UPDATES A SINGLE USER IN THE DATABASE -router.put('/:id', function (req, res) { - User.findByIdAndUpdate(req.params.id, req.body, {new: true}, function (err, user) { - if (err) return res.status(500).send("There was a problem updating the user."); - res.status(200).send(user); +router.put('/:id', (req, res) => { + const { id } = req.params; + + User.findByIdAndUpdate(id, req.body, {new: true}, (err, user) => { + if (err) return res.status(500).json({ message: "There was a problem updating the user." }); + return res.status(200).json(user); }); }); -module.exports = router; \ No newline at end of file +module.exports = router;