Exercise 14: Creating a Record

In this exercise, we're going to write code for saving a todo and its items. To start off, let's create a dummy user since we will hardcode the user ID for our code. Later, in Exercise 19: Securing All the Routes, we will have the ID picked from the authentication details:

  1. Go back to MySQL Workbench.
  2. Clear the previous query and paste the following query, and click on the Execute icon:
USE todo;
INSERT INTO 'user' ('id', 'name', 'email', 'password')
VALUES (NULL, 'Test User', '[email protected]',
MD5('u53rtest'));
  1. When you click on the user table, you should see the following; our newly created user has an ID of 1:
  1. Now, let's go to our routes file, /routes/todo.js and modify the code, for the POST: /todo route; change the code to be as follows (it's only the handler that is changing, notice the change to async function):
    1. Let's start by requiring our Knex instance that is in ./db.js. Just after the line requiring Joi, add this:
const Knex = require('../db');

Notice the two dots, ../db.js, since db.js is found in the parent folder. Recall our topic on requiring local modules in Chapter 1, Introduction to Node.js.
    1. Now, let's modify our handler for the POST: /todo route. Here, were are using the Knex.insert method, and adding an optional .returning method so that we get back the ID of todo we have added:
{
method: 'POST',
path: '/todo',
handler: async (request, reply) =>
{
const todo = request.payload;
todo.user_id = 1; // hard-coded for now
// using array-destructuring here since the
// returned result is an array with 1 element
const [ todoId ] = await Knex('todo')
.returning('id')
.insert(todo);
...
}
},
You can find the complete code from the todo.js file at Code/Lesson-3/exercise-a/routes.
Unlike our previous exercises in Chapter 2, Building the API – Part 1, we will split our POST: /todo route into two, POST: /todo, for adding a todo list, and POST: /todo/<id>/item for adding items to the list.
  1. Now, let's test our newly created endpoint. If you had stopped your server, go back to the Terminal and start it again, with nodemon:
nodemon server.js
  1. Go to Insomnia and make the post request; you should get something like this (notice the todo_id returned, since we will use it in our next example):
  1. Now, let's add a route for adding todo items, POST: /todo/<id>/item; therefore, next to the previous route object, add this route object:
{
method: 'POST',
path: '/todo/{id}/item',
handler: async (request, reply) =>
{
const todoItem = request.payload;
todoItem.todo_id = request.params.id;
const [ id ] = await Knex('todo_item')
.insert(todoItem);
return reply({ message: 'created', id: id });
...
},

You can find the complete code from the todo.js file at Code/Lesson-3/exercise-a/routes.
  1. Now, let's test the route, /todo/1/item, 1 being the ID of todo we created in step 6:
..................Content has been hidden....................

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