diff --git a/controllers/users.js b/controllers/users.js new file mode 100644 index 0000000..de7ea3f --- /dev/null +++ b/controllers/users.js @@ -0,0 +1,66 @@ +const users = require('../data/index'); +const sampleUser = require('../data/sampleUser'); + +// Should retrieve the entire array from _data/index_ +// GET /users Return all users +const listUsers = (req, res) => { + res.json(users) + }; + +// retrieve just the user that matches the passed-in id +// GET /users/:id Return just the user that matches the path param (id) +const showUser = (req, res) => { + let found = users.findIndex(user => user.id == req.params.id); + let user = users[found]; + if (user) { + res.json(user); + } else { + res.status(404).send("User not found.") + } +} + +// Should add a user to the array +// POST /users Create a new user (sampleUser). Find a way to increment the id so that we always insert the next available id in the list. Currently we have users 1-10 (_data/index_). The next user should be 11 + +const createUser = (req, res) => { + counter = users.length; + const newUser = sampleUser; + if (!newUser.name || !newUser.username){ + return res.status(400).json({msg: 'Include a name or username to continue.'}); + } + users.push(newUser) + res.json(users); + } + +// Should update one user in the array based on its id +// PUT /users/:id Update one user matching the path param (id). You may again use the sampleUser code as your "body" for this request +const updateUser = (req, res) => { + const update = req.body; + const found = users.some(user => user.id == req.params.id); + if (found) users.forEach(user => { + if(user.id === parseInt(req.params.id)) { + user.name = update.name; + res.json({ msg: 'User name updated', user}) + } else { + res.status(404).send("User not found.") + } + }) + res.json(users) +} + +// Should delete one user from the array based on its id +// DELETE /users/:id Delete one user by it's id +const deleteUser = (req, res) => { + const found = users.some(user => user.id == req.params.id); + if(found){ + res.json( + { + message: 'Deleted', + users: users.filter(user => user.id !== parseInt(req.params.id)) + }) + } else { + res.status(404).send("User not found.") + } +} + +module.exports = { listUsers, showUser, createUser, updateUser, deleteUser} \ No newline at end of file diff --git a/data/routes/users.js b/data/routes/users.js new file mode 100644 index 0000000..ea7d49f --- /dev/null +++ b/data/routes/users.js @@ -0,0 +1,63 @@ +const express = require('express') +const app = express() +const sampleUser = require('../data/sampleUser') + +// GET /users Return all users +app.get('/users', (req, res) => { + res.json(users) + }) + +// GET /users/:id Return just the user that matches the path param (id) +app.get('/users/:id', (req, res) => { + let found = users.findIndex(user => user.id == req.params.id); + let user = users[found]; + if (user) { + res.json(user); + } else { + res.status(404).send("User not found.") + } +}) + +app.get('/users/:id', (req, res) => { + res.json(users.id) +}) + +// POST /users Create a new user (sampleUser). Find a way to increment the id so that we always insert the next available id in the list. Currently we have users 1-10 (_data/index_). The next user should be 11 +app.post('/users', (req, res) => { + counter = users.length; + const newUser = sampleUser; + if (!newUser.name || !newUser.username){ + return res.status(400).json({msg: 'Include a name or username to continue.'}); + } + users.push(newUser) + res.json(users); + } +) + +// PUT /users/:id Update one user matching the path param (id). You may again use the sampleUser code as your "body" for this request +app.put('/users', (req, res) => { + const update = req.body; + users.forEach(user => { + if(user.id === parseInt(req.params.id)) { + if(user.name != update.name){ + user.name = update.name + } else { + return user.name + } + } + }) + res.json(users) +}) + +// DELETE /users/:id Delete one user by it's id +app.delete = ('/users', (req, res) => { + const found = users.some(user => user.id === req.params.id); + if(found){ + res.json( + { + message: 'Deleted', + users: users.filter(user => user.id !== parseInt(req.params.id)) + }) + } else{res.status(404).json({ msg: 'User not found'}): +} +}) \ No newline at end of file diff --git a/index.js b/index.js index e9537d2..f7d0cdf 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,6 @@ const express = require('express') +const bodyParser = require('body-parser') +const usersRouter = require('./routes/users') const app = express() const port = process.env.PORT || 4000 @@ -6,4 +8,7 @@ app.get('/', (req, res) => res.send('default route')) app.listen(port, () => { console.log('app is listening on:', port) -}) \ No newline at end of file +}) + +app.use(bodyParser.json()) +app.use(usersRouter) \ No newline at end of file diff --git a/routes/users.js b/routes/users.js new file mode 100644 index 0000000..8041638 --- /dev/null +++ b/routes/users.js @@ -0,0 +1,11 @@ +const express = require('express'); +const router = express.Router(); +const usersController = require('../controllers/users'); + +router.get('/users', usersController.listUsers); +router.get('/users/:id', usersController.showUser); +router.post('/users', usersController.createUser); +router.put('/users/:id', usersController.updateUser); +router.delete('/users/:id', usersController.deleteUser); + +module.exports = router; \ No newline at end of file