Updating and deleting todos
The most satisfying part of having a to-do list is marking the items as completed. As it is right now, the application allows us to toggle the completed status of a to-do item, but the change is not persisted to the DB. Let’s fix this.
First, let’s add a new route and the associated controller function to allow us to toggle the completed status of our to-do items. Add the following code to the routes.jl
file:
route("/todos/:id::Int/toggle", TodosController.toggle, method = POST)
Notice the :id::Int
component of the route. This is a dynamic route that will contain the id
of the to-do item that we want to toggle. Also, the route only matches integer values, making sure that incorrect values cannot be passed to the controller function.
Now, for the controller function, edit the TodosController.jl
file and add the following code:
using Genie.Renderers.Json function toggle() todo = findone(Todo, id = params(...