Exercise 16: Updating a Record

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():

  1. 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(),
}
}
}
},
  1. Let's test the route:
  1. 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.
  1. 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:
    1. For instance, changing the item from Nairobi to Nigeria, or:
    1. Marking the item as done:
  1. When we list the items again through the GET: /todo/<id>/item route, you will see the updated item:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset