From 04bce4b90ff9f0ba025acd6d964388bb02132fb2 Mon Sep 17 00:00:00 2001 From: Primakov Alexandr Alexandrovich Date: Sun, 19 Jan 2025 22:47:47 +0300 Subject: [PATCH] todo-app: patch & delete tidi item --- server/routers/todo/routes.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/server/routers/todo/routes.js b/server/routers/todo/routes.js index 6c6a3cf..d4590cd 100644 --- a/server/routers/todo/routes.js +++ b/server/routers/todo/routes.js @@ -63,5 +63,38 @@ router.post('/item', requiredValidate('todoId', 'title'), async (req, res) => { res.send(getAnswer(null, item)) }) +// closed = new Date().toISOString() +router.patch('/item/:itemId', async (req, res) => { + const { itemId } = req.params + const { title, done } = req.body + + const item = await ItemModel.findById(itemId) + + if (!item) { + throw new Error('item not found') + } + + if (title) { + item.title = title + } + + if (done) { + item.done = done + item.closed = done ? new Date().toISOString() : null + } + + await item.save() + + res.send(getAnswer(null, item)) +}) + +router.delete('/item/:itemId', async (req, res) => { + const { itemId } = req.params + + await ItemModel.findByIdAndDelete(itemId) + + res.send(getAnswer(null, { ok: true })) +}) + module.exports = router