diff --git a/task-manager/src/index.js b/task-manager/src/index.js index 638a0fc..9e6c881 100644 --- a/task-manager/src/index.js +++ b/task-manager/src/index.js @@ -102,6 +102,28 @@ app.get('/tasks/:id', async (req, res) => { } }) +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.listen(port, () => { console.log('Server is up on port ' + port) })