Video 98: Resource Updating Endpoints: Part I
This commit is contained in:
parent
c2e488633a
commit
4b1ad55daa
@ -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) => {
|
app.post('/tasks', async (req, res) => {
|
||||||
const task = new Task(req.body)
|
const task = new Task(req.body)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user