From cffbe562444dae502f3825bbc2dae773b40bd1f9 Mon Sep 17 00:00:00 2001 From: Divyanshu Rathore Date: Mon, 24 Sep 2018 20:25:26 +0530 Subject: [PATCH] Adds delete api for todos --- server/server.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/server/server.js b/server/server.js index e4ad3ad..b1e32df 100644 --- a/server/server.js +++ b/server/server.js @@ -45,6 +45,22 @@ app.get('/todos/:id', (req, res) => { }).catch((e) => res.status(400).send()); }); +app.delete('/todos/:id', (req, res) => { + let id = req.params.id; + + if (!ObjectID.isValid(id)) { + return res.status(404).send(); + } + + Todo.findByIdAndRemove(id).then((todo) => { + if (!todo) { + return res.status(404).send(); + } + + res.send(todo); + }).catch((e) => res.status(400).send()); +}); + app.listen(port, () => { console.log(`Started on port: ${port}`); });