From c270aa0052ec911a90c3850a5aec5e32e479ab65 Mon Sep 17 00:00:00 2001 From: Ellen Lee Date: Fri, 19 Jun 2020 03:10:11 +0800 Subject: [PATCH] add update todo function --- routes/modules/todos.js | 25 +++++++++++++++++++++++++ views/edit.hbs | 13 +++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 views/edit.hbs diff --git a/routes/modules/todos.js b/routes/modules/todos.js index cc4651e..6aabfc5 100644 --- a/routes/modules/todos.js +++ b/routes/modules/todos.js @@ -28,4 +28,29 @@ router.get('/:id', (req, res) => { .catch(error => console.log(error)) }) +router.get('/:id/edit', (req, res) => { + const UserId = req.user.id + const id = req.params.id + + return Todo.findOne({ where: { id, UserId } }) + .then(todo => res.render('edit', { todo: todo.get() })) + .catch(error => console.log(error)) +}) + +router.put('/:id', (req, res) => { + const UserId = req.user.id + const id = req.params.id + const { name, isDone } = req.body + console.log(req.body) + + return Todo.findOne({ where: { id, UserId } }) + .then(todo => { + todo.name = name + todo.isDone = isDone === 'on' + return todo.save() + }) + .then(() => res.redirect(`/todos/${id}`)) + .catch(error => console.log(error)) +}) + module.exports = router diff --git a/views/edit.hbs b/views/edit.hbs new file mode 100644 index 0000000..95b80c8 --- /dev/null +++ b/views/edit.hbs @@ -0,0 +1,13 @@ +
+
+
+
+ +
+
+ +
+ +
+
+
\ No newline at end of file