1
0
Fork 0

Video 98: Resource Updating Endpoints: Part I

This commit is contained in:
JayWll 2020-02-23 17:25:21 -07:00
parent c2e488633a
commit 4b1ad55daa
1 changed files with 22 additions and 0 deletions

View File

@ -44,6 +44,28 @@ app.get('/users/:id', async (req, res) => {
}
})
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.post('/tasks', async (req, res) => {
const task = new Task(req.body)