In this exercise, we're going to write routes for updating a todo title or a todo item, and here we will introduce a new Knex method, .update():
- Let's start by modifying our previous PATCH: /todo/<id> route. We have also added an extra validation to make sure that title is supplied as payload:
{
method: 'PATCH',
path: '/todo/{id}',
...
title: Joi.string().required(),
}
}
}
},
- Let's test the route:
- Now, let's add another PATCH route for /todo/<id>/item, this will help in editing a todo item's text and also marking a todo item as done or not done:
{
method: 'PATCH',
path: '/todo/{todo_id}/item/{id}',
handler: async (request, reply) =>
{
const itemId = request.params.id;
...
payload:
{
text: Joi.string(),
done: Joi.boolean(),
}
...
},
You can find the complete code from the todo.js file at Code/Lesson-3/exercise-a/routes.
- This route can take each of the payload items one at a time (which will be the most practical case, when using, for example, a web or mobile UI), or all at once:
- For instance, changing the item from Nairobi to Nigeria, or:
-
- Marking the item as done:
- When we list the items again through the GET: /todo/<id>/item route, you will see the updated item: