From 8555f4735bcb75c77fda189469d343dff4712347 Mon Sep 17 00:00:00 2001 From: JayWll Date: Sun, 23 Feb 2020 18:16:01 -0700 Subject: [PATCH] Video 101: Separate Route Files --- task-manager/src/index.js | 150 +------------------------------ task-manager/src/routers/task.js | 77 ++++++++++++++++ task-manager/src/routers/user.js | 77 ++++++++++++++++ 3 files changed, 158 insertions(+), 146 deletions(-) create mode 100644 task-manager/src/routers/task.js create mode 100644 task-manager/src/routers/user.js diff --git a/task-manager/src/index.js b/task-manager/src/index.js index 092f237..cb544fa 100644 --- a/task-manager/src/index.js +++ b/task-manager/src/index.js @@ -1,156 +1,14 @@ const express = require('express') require('./db/mongoose') -const User = require('./models/user') -const Task = require('./models/task') +const userRouter = require('./routers/user') +const taskRouter = require('./routers/task') const app = express() const port = process.env.PORT || 4200 app.use(express.json()) - -app.post('/users', async (req, res) => { - const user = new User(req.body) - - try { - await user.save() - res.status(201).send(user) - } catch (e) { - res.status(400).send(e) - } -}) - -app.get('/users', async (req, res) => { - try { - const users = await User.find({}) - res.send(users) - } catch (e) { - res.status(500).send() - } -}) - -app.get('/users/:id', async (req, res) => { - const _id = req.params.id - - try { - const user = await User.findById(_id) - - if (!user) { - return res.status(404).send() - } - - res.send(user) - } catch (e) { - res.status(500).send() - } -}) - -app.patch('/users/:id', async (req, res) => { - const updates = Object.keys(req.body) - const allowedUpdates = ['name', 'email', 'password', 'age'] - const isValidOperation = updates.every((update) => allowedUpdates.includes(update)) - - if (!isValidOperation) { - return res.status(400).send({ error: 'Invalid updates!' }) - } - - try { - const user = await User.findByIdAndUpdate(req.params.id, req.body, { new: true, runValidators: true }) - - if (!user) { - return res.status(404).send() - } - - res.send(user) - } catch (e) { - res.status(400).send(e) - } -}) - -app.delete('/users/:id', async (req, res) => { - try { - const user = await User.findByIdAndDelete(req.params.id) - - if (!user) { - return res.status(404).send() - } - - res.send(user) - } catch (e) { - res.status(500).send() - } -}) - -app.post('/tasks', async (req, res) => { - const task = new Task(req.body) - - try { - await task.save() - res.status(201).send(task) - } catch (e) { - res.status(400).send(e) - } -}) - -app.get('/tasks', async (req, res) => { - try { - const tasks = await Task.find({}) - res.send(tasks) - } catch (e) { - res.status(500).send() - } -}) - -app.get('/tasks/:id', async (req, res) => { - const _id = req.params.id - - try { - const task = await Task.findById(_id) - - if (!task) { - return res.status(404).send() - } - - res.send(task) - } catch (e) { - res.status(500).send() - } -}) - -app.patch('/tasks/:id', async (req, res) => { - const updates = Object.keys(req.body) - const allowedUpdates = ['description', 'completed'] - const isValidOperation = updates.every((update) => allowedUpdates.includes(update)) - - if (!isValidOperation) { - return res.status(400).send({ error: 'Invalid updates!' }) - } - - try { - const task = await Task.findByIdAndUpdate(req.params.id, req.body, { new: true, runValidators: true }) - - if (!task) { - return res.status(404).send() - } - - res.send(task) - } catch (e) { - res.status(400).send(e) - } -}) - -app.delete('/tasks/:id', async (req, res) => { - try { - const task = await Task.findByIdAndDelete(req.params.id) - - if (!task) { - return res.status(404).send() - } - - res.send(task) - } catch (e) { - res.status(500).send() - } -}) +app.use(userRouter) +app.use(taskRouter) app.listen(port, () => { console.log('Server is up on port ' + port) diff --git a/task-manager/src/routers/task.js b/task-manager/src/routers/task.js new file mode 100644 index 0000000..f914286 --- /dev/null +++ b/task-manager/src/routers/task.js @@ -0,0 +1,77 @@ +const express = require('express') +const Task = require('../models/task') +const router = new express.Router() + +router.post('/tasks', async (req, res) => { + const task = new Task(req.body) + + try { + await task.save() + res.status(201).send(task) + } catch (e) { + res.status(400).send(e) + } +}) + +router.get('/tasks', async (req, res) => { + try { + const tasks = await Task.find({}) + res.send(tasks) + } catch (e) { + res.status(500).send() + } +}) + +router.get('/tasks/:id', async (req, res) => { + const _id = req.params.id + + try { + const task = await Task.findById(_id) + + if (!task) { + return res.status(404).send() + } + + res.send(task) + } catch (e) { + res.status(500).send() + } +}) + +router.patch('/tasks/:id', async (req, res) => { + const updates = Object.keys(req.body) + const allowedUpdates = ['description', 'completed'] + const isValidOperation = updates.every((update) => allowedUpdates.includes(update)) + + if (!isValidOperation) { + return res.status(400).send({ error: 'Invalid updates!' }) + } + + try { + const task = await Task.findByIdAndUpdate(req.params.id, req.body, { new: true, runValidators: true }) + + if (!task) { + return res.status(404).send() + } + + res.send(task) + } catch (e) { + res.status(400).send(e) + } +}) + +router.delete('/tasks/:id', async (req, res) => { + try { + const task = await Task.findByIdAndDelete(req.params.id) + + if (!task) { + return res.status(404).send() + } + + res.send(task) + } catch (e) { + res.status(500).send() + } +}) + +module.exports = router diff --git a/task-manager/src/routers/user.js b/task-manager/src/routers/user.js new file mode 100644 index 0000000..1d3473e --- /dev/null +++ b/task-manager/src/routers/user.js @@ -0,0 +1,77 @@ +const express = require('express') +const User = require('../models/user') +const router = new express.Router() + +router.post('/users', async (req, res) => { + const user = new User(req.body) + + try { + await user.save() + res.status(201).send(user) + } catch (e) { + res.status(400).send(e) + } +}) + +router.get('/users', async (req, res) => { + try { + const users = await User.find({}) + res.send(users) + } catch (e) { + res.status(500).send() + } +}) + +router.get('/users/:id', async (req, res) => { + const _id = req.params.id + + try { + const user = await User.findById(_id) + + if (!user) { + return res.status(404).send() + } + + res.send(user) + } catch (e) { + res.status(500).send() + } +}) + +router.patch('/users/:id', async (req, res) => { + const updates = Object.keys(req.body) + const allowedUpdates = ['name', 'email', 'password', 'age'] + const isValidOperation = updates.every((update) => allowedUpdates.includes(update)) + + if (!isValidOperation) { + return res.status(400).send({ error: 'Invalid updates!' }) + } + + try { + const user = await User.findByIdAndUpdate(req.params.id, req.body, { new: true, runValidators: true }) + + if (!user) { + return res.status(404).send() + } + + res.send(user) + } catch (e) { + res.status(400).send(e) + } +}) + +router.delete('/users/:id', async (req, res) => { + try { + const user = await User.findByIdAndDelete(req.params.id) + + if (!user) { + return res.status(404).send() + } + + res.send(user) + } catch (e) { + res.status(500).send() + } +}) + +module.exports = router