1 00:00:00,180 --> 00:00:05,010 We've gone through resource creation once for our REST API with users. 2 00:00:05,010 --> 00:00:11,640 You're going to do it again in this video with tasks so down below I have the challenge comments outlining 3 00:00:11,640 --> 00:00:12,950 what I'd like you to do. 4 00:00:13,080 --> 00:00:17,370 The big picture goal is to set up the task creation end point. 5 00:00:17,370 --> 00:00:22,830 Now there's no need to worry about linking a task with a specific user who created it. 6 00:00:22,830 --> 00:00:27,830 We'll start to handle all of that when we talk about authentication a bit later in the class. 7 00:00:27,870 --> 00:00:31,430 Now down below there are three main steps for getting this done. 8 00:00:31,440 --> 00:00:36,970 Step number one is to create a separate file for the task model. 9 00:00:37,050 --> 00:00:44,220 So we want to break the task model out of Mongoose dot J S and into a new file in the model's directory 10 00:00:44,400 --> 00:00:47,840 similar to what we did with user dot J S. 11 00:00:47,940 --> 00:00:54,060 That file is going to get loaded into index dot J S the current file and it'll get used to actually 12 00:00:54,090 --> 00:00:55,900 create the new task. 13 00:00:55,920 --> 00:00:58,580 Step 2 You want to setup the end point. 14 00:00:58,590 --> 00:01:04,710 So we want to set up the new post end point and right here we want to make sure that when we create 15 00:01:04,710 --> 00:01:06,840 the task we send back things. 16 00:01:06,840 --> 00:01:12,860 For the success end for the error cases much like we did with users up above. 17 00:01:12,900 --> 00:01:15,700 We hope things go well but they don't always go well. 18 00:01:15,720 --> 00:01:19,790 It's important to be responsible and handle errors correctly. 19 00:01:19,800 --> 00:01:23,300 Step number three you want to test your work from postmen. 20 00:01:23,400 --> 00:01:30,690 Try to create a task using valid data then try to create a task using invalid data and make sure you 21 00:01:30,690 --> 00:01:34,610 get the correct response depending on the type of data you provide it. 22 00:01:34,980 --> 00:01:36,660 So that is it for these setup. 23 00:01:36,660 --> 00:01:41,340 The process is going to be pretty similar to what we did in the last video. 24 00:01:41,340 --> 00:01:46,710 Take some time to knock that out test your work and when you're done come back and click play 25 00:01:50,680 --> 00:01:51,430 how'd that go. 26 00:01:51,460 --> 00:01:53,460 Let's work through the process together. 27 00:01:53,460 --> 00:02:00,520 Step number one we want to create that new file in the model's directory for the tasks model right here. 28 00:02:00,520 --> 00:02:06,820 I'll call that task dot J S and we're going to bring the model file over from where it currently lives 29 00:02:06,820 --> 00:02:14,230 in Mongoose dot J S I'm going to take the task I'll cut it out of this file and I'll bring it over to 30 00:02:14,230 --> 00:02:15,500 the new file. 31 00:02:15,520 --> 00:02:20,540 Now we do have to make sure we have Mongoose required since we are using it right here. 32 00:02:20,680 --> 00:02:22,570 So up top we can get that done. 33 00:02:22,780 --> 00:02:24,860 Const Mongoose equals. 34 00:02:25,030 --> 00:02:29,220 We will call require requiring the Mongoose library. 35 00:02:29,260 --> 00:02:34,550 The last thing we need to do is export task so other files can actually use it. 36 00:02:34,730 --> 00:02:36,770 Module dot exports. 37 00:02:36,850 --> 00:02:41,260 Setting an equal to task which is defined on line three up above. 38 00:02:41,830 --> 00:02:50,260 So now task dot J.S. is complete and model dot J S excuse me Mongoose dot J S does almost nothing and 39 00:02:50,260 --> 00:02:51,260 this is fine. 40 00:02:51,310 --> 00:02:58,980 This is exactly what we want in the file a single responsibility Mongoose connects to the database user 41 00:02:59,020 --> 00:03:05,310 dot J S defines the user model and task dot J S defines the task model. 42 00:03:05,410 --> 00:03:11,800 Next up inside of index dot J Yes we have step number two we want to make sure it is setup that endpoint 43 00:03:12,040 --> 00:03:19,870 before we do let's load in the task model so up above just below where I grab user or above the order 44 00:03:19,870 --> 00:03:29,530 isn't important const task equals I will require the new file dot forward slash models forward slash 45 00:03:29,590 --> 00:03:31,430 task excellent. 46 00:03:31,570 --> 00:03:39,070 Now we can set up a another call to app dot post to set up our other resource creation endpoint app 47 00:03:39,850 --> 00:03:47,200 dot post setting up the path which would be forward slash tasks and setting up that callback function 48 00:03:47,620 --> 00:03:53,740 we get their request we get their response and inside of the function we're gonna go through the process 49 00:03:53,950 --> 00:04:02,230 of actually creating the new task to start that off const task equals right here I'll use the new operator 50 00:04:02,410 --> 00:04:09,850 with the task constructor function and once again I will provide request dot body as the object which 51 00:04:09,850 --> 00:04:15,080 contains the properties we're trying to set up for the new task we're creating. 52 00:04:15,400 --> 00:04:21,830 Next up we'll be calling task dot save in an attempt to save that new task to the database. 53 00:04:21,970 --> 00:04:28,450 Now things might go well or they might go poorly we want to handle both cases so I'll set up a then 54 00:04:28,450 --> 00:04:35,500 call for when the promise is fulfilled right here we'll go ahead and set up that callback function inside 55 00:04:35,500 --> 00:04:43,990 of then and I'll use response dot send to send back the task if things go well down below we'll be setting 56 00:04:43,990 --> 00:04:45,940 up that catch call as well. 57 00:04:45,940 --> 00:04:53,080 This one gets access to the error that occurred and down below inside of that function I'll use a response 58 00:04:53,440 --> 00:05:01,390 dot status to set up the four hundred status code and then I'll use dot send with e to send back the 59 00:05:01,390 --> 00:05:02,230 error. 60 00:05:02,410 --> 00:05:08,690 Now let's go ahead and move on to the final step which is to test things out from postmen I'm going 61 00:05:08,700 --> 00:05:15,460 to remove the challenge comments down below I'm gonna go ahead and save index dot J S and we're going 62 00:05:15,460 --> 00:05:23,400 to set up a new request in the task app collection right here from the menu I'll add a new request and 63 00:05:23,440 --> 00:05:30,790 I can name this one something like create task and down below it's already selected the task app collection 64 00:05:31,270 --> 00:05:37,540 I'll save the new request and I can click it to go through the process of actually filling it out for 65 00:05:37,540 --> 00:05:44,500 this one we are indeed using the post method so let's make sure to switch that up inside of the SC we 66 00:05:44,500 --> 00:05:52,050 called app dot post when we set up the route then we'll set up you are out that's local host colon three 67 00:05:52,050 --> 00:05:59,590 thousand forward slash tasks now currently this is not enough to actually fire it off in a way that 68 00:05:59,590 --> 00:06:06,220 causes a task to be created I could go ahead and send it off but we're going to get validation errors 69 00:06:06,220 --> 00:06:12,300 back with a four hundred status code we want to make sure to provide the necessary data. 70 00:06:12,400 --> 00:06:13,140 So right here. 71 00:06:13,150 --> 00:06:17,320 Body we'll go down to raw. 72 00:06:17,360 --> 00:06:20,870 We're going to switch from text over to Jason. 73 00:06:20,870 --> 00:06:25,130 Then we'll go through the process of defining the data we want to set. 74 00:06:25,160 --> 00:06:29,270 Now the only thing we have to define is the task description. 75 00:06:29,270 --> 00:06:31,500 So over inside of task dot and J. 76 00:06:31,500 --> 00:06:33,970 S We have our model definition. 77 00:06:34,160 --> 00:06:38,820 We have description which is required and we have completed which is optional. 78 00:06:38,840 --> 00:06:46,210 So over in postmen I will set up description and I'll go ahead and give it a value equal to what the 79 00:06:46,210 --> 00:06:53,360 task I'm creating is going to be I'll go ahead and do something like finish node j s course. 80 00:06:53,500 --> 00:06:58,810 Now if I go ahead and actually send that off we'll be able to see our response down below. 81 00:06:58,810 --> 00:07:04,840 Right here I am getting the 200 status code and I am seeing my brand new task created. 82 00:07:04,840 --> 00:07:10,570 One thing worth pointing out is that completed has correctly been set to false which is the default 83 00:07:10,570 --> 00:07:12,100 value for that field. 84 00:07:12,670 --> 00:07:17,970 So right here we have a pretty great setup for our two resource creation endpoints. 85 00:07:17,980 --> 00:07:23,350 There's just one last thing I want to take a quick moment to talk about and it's related to the status 86 00:07:23,350 --> 00:07:28,810 code we're using when we do create a user or a task by default. 87 00:07:28,810 --> 00:07:34,990 If you send back data and you don't change the status express sets it to two hundred assuming that things 88 00:07:34,990 --> 00:07:38,430 went well as we saw we can always customize that. 89 00:07:38,500 --> 00:07:45,790 Now in our case if we head back over to that H TTP statuses Web site and go to the home page we can 90 00:07:45,790 --> 00:07:49,670 see that there are more than one way to say that things went well. 91 00:07:49,690 --> 00:07:52,030 We have a few different success codes. 92 00:07:52,030 --> 00:07:53,660 Two hundred is a generic. 93 00:07:53,680 --> 00:07:54,530 OK. 94 00:07:54,580 --> 00:07:58,080 2 A 1 signifies that something has been created. 95 00:07:58,330 --> 00:08:04,690 So that is a more appropriate status code for our purposes now changing it from a two hundred to a two 96 00:08:04,690 --> 00:08:09,600 a one isn't going to affect the behavior of the API in any major way. 97 00:08:09,730 --> 00:08:13,690 But it's all about providing the most clear information possible. 98 00:08:13,780 --> 00:08:21,670 So over inside of index dot J asked to wrap this up I can actually add on status calls right here response 99 00:08:21,730 --> 00:08:24,940 dot status setting and equal to a 2 a 1. 100 00:08:24,940 --> 00:08:31,000 Then we'll call send a to send back the user and down below we'll do the same thing for the task. 101 00:08:31,000 --> 00:08:38,610 So dot status then chaining on the send call once again setting an equal to a two a one. 102 00:08:38,620 --> 00:08:44,550 Now if we were the test things out from postmen we would see that we're still able to create new tasks. 103 00:08:44,560 --> 00:08:50,590 The only change is in the status code that's coming back now that we have this in place we have wrapped 104 00:08:50,590 --> 00:08:54,640 up our resource creation endpoints for the rest api. 105 00:08:54,700 --> 00:08:58,750 We're going to continue on working on the other end points in the next video. 106 00:08:58,750 --> 00:09:00,730 So let's go ahead and jump into that.