Skip to content

Commit

Permalink
add update todo function
Browse files Browse the repository at this point in the history
  • Loading branch information
ellenlee committed Jun 18, 2020
1 parent fd2e825 commit c270aa0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
25 changes: 25 additions & 0 deletions routes/modules/todos.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 13 additions & 0 deletions views/edit.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<form action="/todos/{{ todo.id }}?_method=PUT" method="POST" style="display: inline;">
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text">
<input type="checkbox" name="isDone" {{#if todo.isDone }} checked {{/if}}>
</div>
</div>
<input class="form-control" type="text" placeholder="name" name="name" value="{{ todo.name }}">
<div class="input-group-append">
<button class="btn btn-success" type="submit">Save</button>
</div>
</div>
</form>

0 comments on commit c270aa0

Please sign in to comment.