1 00:00:00,830 --> 00:00:02,160 Welcome back. 2 00:00:02,160 --> 00:00:06,070 Now that we finished the @store function, it's time to move away from 3 00:00:06,070 --> 00:00:10,730 the browser and start testing with software such as postman, or 4 00:00:10,730 --> 00:00:14,150 an IDE plugin, such as REST client. 5 00:00:14,150 --> 00:00:19,320 To send and test a post request, you'll need to download and install Postman 6 00:00:19,320 --> 00:00:25,950 yourself and REST client in the Visual Studio Code extensions marketplace. 7 00:00:25,950 --> 00:00:29,280 I'll walk you through the steps of installing REST client by Chow, 8 00:00:29,280 --> 00:00:31,330 so let's get started. 9 00:00:33,240 --> 00:00:39,140 Now that we have Postman installed, let's test the @store function like this. 10 00:00:39,140 --> 00:00:42,390 To test the at index end point, select GET. 11 00:00:45,886 --> 00:00:52,261 And add this URL, And click Send. 12 00:00:55,360 --> 00:00:57,370 It works as expected. 13 00:00:57,370 --> 00:01:00,735 Nice, now, let's install REST client so 14 00:01:00,735 --> 00:01:06,628 we can do our testing without having to leave Visual Studio code like this. 15 00:01:16,149 --> 00:01:19,750 I've already installed it, so go ahead and install it here. 16 00:01:22,110 --> 00:01:27,514 Finally, we're gonna create a new 17 00:01:27,514 --> 00:01:33,828 file named rest_test.http like this. 18 00:01:39,212 --> 00:01:44,824 If you have any trouble with REST client like connection is being rejected, 19 00:01:44,824 --> 00:01:49,920 try PHP artisan serve without that dash dash host equals local host. 20 00:01:51,900 --> 00:01:55,060 First, let's test all authors like this. 21 00:02:00,427 --> 00:02:02,869 Nice, here are all the authors. 22 00:02:04,920 --> 00:02:09,990 As you can see, REST client is a wonderful tool for testing endpoints. 23 00:02:09,990 --> 00:02:13,500 We received our HTTP response of 200 okay, 24 00:02:14,600 --> 00:02:17,650 we have some extra information in the header. 25 00:02:17,650 --> 00:02:24,570 And we have all of our authors returned right inside of Visual Studio code. 26 00:02:24,570 --> 00:02:27,543 Next, let's test a single author like this. 27 00:02:34,019 --> 00:02:35,618 Perfect, author number three. 28 00:02:40,780 --> 00:02:45,434 How about all books like this? 29 00:02:48,169 --> 00:02:50,657 Perfect, here are all of the books. 30 00:02:57,549 --> 00:02:59,330 Code update. 31 00:02:59,330 --> 00:03:00,590 In this code snippet, 32 00:03:00,590 --> 00:03:05,740 be sure to remove the pipe unique because we're not checking for unique emails. 33 00:03:05,740 --> 00:03:09,090 Check the teachers notes below to learn more. 34 00:03:09,090 --> 00:03:11,513 Before we can test the @store function, 35 00:03:11,513 --> 00:03:14,351 let's import the validator at the top like this. 36 00:03:18,031 --> 00:03:22,636 Next, we're going to refractor the current validation code like this. 37 00:03:30,122 --> 00:03:34,784 Next, we're gonna update the return response as well, like this. 38 00:03:38,610 --> 00:03:42,969 Finally, let's test the @store function like this. 39 00:03:47,124 --> 00:03:51,750 To learn more about validation, check the teacher's notes below. 40 00:03:51,750 --> 00:03:55,330 Nice job, we successfully sent a post request and 41 00:03:55,330 --> 00:03:59,275 received an HTTP status of 201 created. 42 00:03:59,275 --> 00:04:04,750 201 means that the request has been fulfilled and has resulted in one or 43 00:04:04,750 --> 00:04:09,950 more resources being created, so everything is working as expected. 44 00:04:09,950 --> 00:04:14,555 To verify that our new author was created, use the default welcome view, 45 00:04:14,555 --> 00:04:17,720 Postman or REST client. 46 00:04:17,720 --> 00:04:21,660 Try submitting a git request using REST client like this. 47 00:04:24,590 --> 00:04:29,630 As you can see, the last author is indeed our newly created author, so 48 00:04:29,630 --> 00:04:32,560 everything is working, nice job. 49 00:04:32,560 --> 00:04:35,000 I like using REST client for testing, but 50 00:04:35,000 --> 00:04:38,370 feel free to use post man or the default welcome view 51 00:04:38,370 --> 00:04:42,740 as demonstrated earlier as each one of these will test the same results. 52 00:04:44,700 --> 00:04:50,368 Great job so far, we successfully created, and tested the index, 53 00:04:50,368 --> 00:04:55,440 show, and store functions in order to display all authors, 54 00:04:55,440 --> 00:05:01,427 one specific author, and store newly created authors respectively. 55 00:05:01,427 --> 00:05:05,928 In the next section, we're going to finish building the remaining crud 56 00:05:05,928 --> 00:05:10,820 functionality for our REST API by creating the update and destroy functions. 57 00:05:12,380 --> 00:05:16,680 Before we get started, let's discuss why the update function 58 00:05:16,680 --> 00:05:19,670 needs to accept both put in a patch method. 59 00:05:21,220 --> 00:05:22,440 Put is a method for 60 00:05:22,440 --> 00:05:29,010 modifying resources where the client sends data that updates the entire resource. 61 00:05:29,010 --> 00:05:31,900 It's used to set a resources information and 62 00:05:31,900 --> 00:05:35,980 if a resource already exists, creates a new resource. 63 00:05:35,980 --> 00:05:40,630 For example, when you want to change the first name of a person in a database, 64 00:05:40,630 --> 00:05:44,330 you need to send the entire resource when making a put request. 65 00:05:45,430 --> 00:05:48,690 Patch applies to a partial update to the resource, 66 00:05:48,690 --> 00:05:53,360 which means you're only required to send the data that you want to update and 67 00:05:53,360 --> 00:05:56,670 it won't affect or change anything else. 68 00:05:56,670 --> 00:06:01,580 For example, if you want to update the first name of a resource in the database, 69 00:06:01,580 --> 00:06:04,720 you'll only need to send the first name. 70 00:06:04,720 --> 00:06:09,670 Let's get started by building the put and patch routes for the update function. 71 00:06:09,670 --> 00:06:12,820 And to conclude this section on CRUD operations, 72 00:06:12,820 --> 00:06:15,450 we'll finish with the destroy function. 73 00:06:15,450 --> 00:06:15,950 See you there. 74 00:06:17,610 --> 00:06:20,610 Now that the store function is working as expected, 75 00:06:20,610 --> 00:06:25,200 we have two more functions to go, update and destroy. 76 00:06:25,200 --> 00:06:29,865 Let's start by building the put and patch routes for the update function like this. 77 00:06:57,966 --> 00:07:03,253 Now let's verify our routes using php artisan route lists like this. 78 00:07:16,630 --> 00:07:18,720 Great, our routes are ready to go. 79 00:07:18,720 --> 00:07:23,960 So let's navigate to the AuthorController.php file so we can update 80 00:07:23,960 --> 00:07:29,130 the validation and add the author resource to the update function like this. 81 00:07:30,310 --> 00:07:35,290 First, let's refactor the validation once again to make this as explicit 82 00:07:35,290 --> 00:07:38,621 as possible by copying the add store validation. 83 00:07:47,632 --> 00:07:52,664 Finally, we'll wrap our author model with the new 84 00:07:52,664 --> 00:07:57,708 author resource in the update function like this. 85 00:08:23,595 --> 00:08:27,255 Before we move on, let's change the 200OK, 86 00:08:27,255 --> 00:08:32,980 HTTP status to 201 created which is a better status code for this action. 87 00:08:34,500 --> 00:08:38,570 Great, now let's head over to the REST client and test our routes like this. 88 00:08:41,720 --> 00:08:44,380 This is the comment for our first PUT request. 89 00:08:47,570 --> 00:08:51,431 And this is a PUT method for the first author and 90 00:08:51,431 --> 00:08:55,898 here is the information that we're going to update. 91 00:08:59,866 --> 00:09:03,166 Nice job, you should see a 201 created status, 92 00:09:03,166 --> 00:09:07,790 which means we're almost done testing the update function. 93 00:09:07,790 --> 00:09:12,350 So let's go ahead and verify that our last record was updated successfully. 94 00:09:13,700 --> 00:09:15,070 If everything is correct, 95 00:09:15,070 --> 00:09:18,560 you should see an updated author with the changes we made earlier. 96 00:09:19,570 --> 00:09:23,028 We're almost done with our CRUD operations for our authors so 97 00:09:23,028 --> 00:09:26,967 let's build the destroy function starting with the route like this. 98 00:09:38,352 --> 00:09:44,998 Before we move on, let's verify our route using PHP artisan route list like this. 99 00:09:56,829 --> 00:10:01,910 Great, our route is verified and we're ready to test the destroy function. 100 00:10:01,910 --> 00:10:04,562 But let's make sure the destroy function is in order. 101 00:10:13,071 --> 00:10:18,151 Our first destroy function looks to be in order, so let's test it out like this. 102 00:10:26,149 --> 00:10:28,845 And we will delete the second author. 103 00:10:33,310 --> 00:10:36,859 Great, we deleted our author with an ID of 2, 104 00:10:36,859 --> 00:10:41,780 and our request was returned with a status of 204 No Content. 105 00:10:43,740 --> 00:10:48,830 Fantastic work, we've successfully tested all of the author resources and 106 00:10:48,830 --> 00:10:51,830 routes using the REST client plugin 107 00:10:51,830 --> 00:10:54,830 without having to leave the Visual Studio Code IDE. 108 00:10:56,510 --> 00:11:02,430 We also used resources to hide database specific information like ID and 109 00:11:02,430 --> 00:11:07,420 timestamps, while exposing fields, such as name, title, 110 00:11:07,420 --> 00:11:10,790 company, and email for each one of our authors. 111 00:11:12,030 --> 00:11:14,590 In the next section, we're gonna do the same for 112 00:11:14,590 --> 00:11:18,050 our authors books by creating the book resource. 113 00:11:18,050 --> 00:11:18,620 See you there.