Testing Book Resources and Routes8:14 with Jonathan Barrios
We created the AuthorResource and used it to remove database specific data such as ID and timestamps. Then, we tested our routes using both POSTMAN and REST Client in order to verify that the data we are exposing to our API consumers is correct. In this section, we’re going to repeat the steps we took when building out the AuthorResource by using the Book model as a guide to specify what data to hide and which to expose.
We're making great progress. 0:00 We created the AuthorResource and 0:02 used it to remove database-specific data such as ID and timestamps. 0:04 Then we tested our routes using both Postman and 0:10 REST client in order to verify that the data we are exposing is correct. 0:14 In this section, we're going to repeat the steps we took 0:20 when building the AuthorResource by using the book model as a guide. 0:24 See you there. 0:28 Before we get started building our BookResource, 0:31 let's create our routes first. 0:34 Navigate to the api.php file and copy and 0:37 paste all except the index route in the author's routes, like this. 0:40 Holding down the Option key and 0:47 clicking allows you to edit multiple fields at once, which is super handy. 0:49 Holding down the Shift and right arrow will select them all at the same time, 0:56 allowing us to do this. 1:01 Now that we have all of our routes defined, 1:29 let's verify our routes with the route:list command, like this. 1:32 Great, all of our BookController routes are defined. 1:41 But we still need to refactor our BookController, store, and 1:45 update functions, like we did with the AuthorController.php file before. 1:49 Great, now we're ready to start building our BookResource using this artisan 2:50 command. 2:55 Ctrl+K will clear the terminal. 3:01 Php artisan make:resource BookResource. 3:10 Next, let's open the BookResource file located in 3:19 the App/Http/Resources directory. 3:23 And let's see what's happening there. 3:28 If we run this code, you'll see that it returns all books. 3:31 We can test this behavior using REST client, like this. 3:36 Great job. 3:44 Now we're ready to import the BookResource in the BookController.php file, like this. 3:46 Did you notice the database-specific data such as IDs and timestamps? 4:04 Great, now we'll delete the return statement in the BookResource, 4:10 just like we did with the AuthorResource. 4:14 And we'll 4:17 use the book 4:24 model as 4:32 a guide to 4:37 create our 4:43 BookResource, 4:50 like this. 5:02 Next, we need to wrap our book model around the BookResource collection in 5:26 the BookController, like this. 5:31 Great, we're now ready to test our work with REST client using 5:37 the rest_test.http file we created earlier, like this. 5:42 Fantastic, our BookResource is removing the database-specific data 5:49 because we are only exposing the fields we want to share with our API consumers, 5:53 mainly authors and books with our IDs or timestamps. 5:59 Our show function works great, and we tested this behavior using REST client. 6:05 However, our single books are still returning the ID and 6:11 timestamps again, and we don't want to expose this data to our API consumers. 6:14 Fortunately, we can use the AuthorResource to, 6:20 again, expose only the relevant data, like this. 6:23 Let's take a look at the BookResource and why this works. 6:27 We're basically passing in, 6:35 $this->author, and this is why we are still getting the IDs and 6:40 timestamps, because we're not using the AuthorResource. 6:45 Using this block of code will use a new AuthorResource and 6:52 passes in $this->author. 6:58 Nice, to illustrate the power of resources, 7:01 let's add a field to our BookResource, like this. 7:04 Let's test our behavior using REST client once again. 7:09 No IDs or timestamp. 7:21 And notice how every book has the secret. 7:26 Notice how this gets populated to every single book. 7:31 This is where you can define what your API exposes using existing resources. 7:35 Pretty powerful stuff. 7:41 Congratulations, we're done building the core of our REST API. 7:45 We tested our routes, and all of our data looks great. 7:50 We even used our existing AuthorResource to remove the database-specific data 7:54 when displaying books. 7:59 Way to go. 8:01 In the next section, we're going to clean up both our authors and books routes. 8:02 Then we'll discuss validation messages and how we can make them even better. 8:08 See you there. 8:13
You need to sign up for Treehouse in order to download course files.Sign up