We've gone through resource creation once for our REST API with users.
You're going to do it again in this video with tasks so down below I have the challenge comments outlining
what I'd like you to do.
The big picture goal is to set up the task creation end point.
Now there's no need to worry about linking a task with a specific user who created it.
We'll start to handle all of that when we talk about authentication a bit later in the class.
Now down below there are three main steps for getting this done.
Step number one is to create a separate file for the task model.
So we want to break the task model out of Mongoose dot J S and into a new file in the model's directory
similar to what we did with user dot J S.
That file is going to get loaded into index dot J S the current file and it'll get used to actually
create the new task.
Step 2 You want to setup the end point.
So we want to set up the new post end point and right here we want to make sure that when we create
the task we send back things.
For the success end for the error cases much like we did with users up above.
We hope things go well but they don't always go well.
It's important to be responsible and handle errors correctly.
Step number three you want to test your work from postmen.
Try to create a task using valid data then try to create a task using invalid data and make sure you
get the correct response depending on the type of data you provide it.
So that is it for these setup.
The process is going to be pretty similar to what we did in the last video.
Take some time to knock that out test your work and when you're done come back and click play
how'd that go.
Let's work through the process together.
Step number one we want to create that new file in the model's directory for the tasks model right here.
I'll call that task dot J S and we're going to bring the model file over from where it currently lives
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
the new file.
Now we do have to make sure we have Mongoose required since we are using it right here.
So up top we can get that done.
Const Mongoose equals.
We will call require requiring the Mongoose library.
The last thing we need to do is export task so other files can actually use it.
Module dot exports.
Setting an equal to task which is defined on line three up above.
So now task dot J.S. is complete and model dot J S excuse me Mongoose dot J S does almost nothing and
this is fine.
This is exactly what we want in the file a single responsibility Mongoose connects to the database user
dot J S defines the user model and task dot J S defines the task model.
Next up inside of index dot J Yes we have step number two we want to make sure it is setup that endpoint
before we do let's load in the task model so up above just below where I grab user or above the order
isn't important const task equals I will require the new file dot forward slash models forward slash
task excellent.
Now we can set up a another call to app dot post to set up our other resource creation endpoint app
dot post setting up the path which would be forward slash tasks and setting up that callback function
we get their request we get their response and inside of the function we're gonna go through the process
of actually creating the new task to start that off const task equals right here I'll use the new operator
with the task constructor function and once again I will provide request dot body as the object which
contains the properties we're trying to set up for the new task we're creating.
Next up we'll be calling task dot save in an attempt to save that new task to the database.
Now things might go well or they might go poorly we want to handle both cases so I'll set up a then
call for when the promise is fulfilled right here we'll go ahead and set up that callback function inside
of then and I'll use response dot send to send back the task if things go well down below we'll be setting
up that catch call as well.
This one gets access to the error that occurred and down below inside of that function I'll use a response
dot status to set up the four hundred status code and then I'll use dot send with e to send back the
error.
Now let's go ahead and move on to the final step which is to test things out from postmen I'm going
to remove the challenge comments down below I'm gonna go ahead and save index dot J S and we're going
to set up a new request in the task app collection right here from the menu I'll add a new request and
I can name this one something like create task and down below it's already selected the task app collection
I'll save the new request and I can click it to go through the process of actually filling it out for
this one we are indeed using the post method so let's make sure to switch that up inside of the SC we
called app dot post when we set up the route then we'll set up you are out that's local host colon three
thousand forward slash tasks now currently this is not enough to actually fire it off in a way that
causes a task to be created I could go ahead and send it off but we're going to get validation errors
back with a four hundred status code we want to make sure to provide the necessary data.
So right here.
Body we'll go down to raw.
We're going to switch from text over to Jason.
Then we'll go through the process of defining the data we want to set.
Now the only thing we have to define is the task description.
So over inside of task dot and J.
S We have our model definition.
We have description which is required and we have completed which is optional.
So over in postmen I will set up description and I'll go ahead and give it a value equal to what the
task I'm creating is going to be I'll go ahead and do something like finish node j s course.
Now if I go ahead and actually send that off we'll be able to see our response down below.
Right here I am getting the 200 status code and I am seeing my brand new task created.
One thing worth pointing out is that completed has correctly been set to false which is the default
value for that field.
So right here we have a pretty great setup for our two resource creation endpoints.
There's just one last thing I want to take a quick moment to talk about and it's related to the status
code we're using when we do create a user or a task by default.
If you send back data and you don't change the status express sets it to two hundred assuming that things
went well as we saw we can always customize that.
Now in our case if we head back over to that H TTP statuses Web site and go to the home page we can
see that there are more than one way to say that things went well.
We have a few different success codes.
Two hundred is a generic.
OK.
2 A 1 signifies that something has been created.
So that is a more appropriate status code for our purposes now changing it from a two hundred to a two
a one isn't going to affect the behavior of the API in any major way.
But it's all about providing the most clear information possible.
So over inside of index dot J asked to wrap this up I can actually add on status calls right here response
dot status setting and equal to a 2 a 1.
Then we'll call send a to send back the user and down below we'll do the same thing for the task.
So dot status then chaining on the send call once again setting an equal to a two a one.
Now if we were the test things out from postmen we would see that we're still able to create new tasks.
The only change is in the status code that's coming back now that we have this in place we have wrapped
up our resource creation endpoints for the rest api.
We're going to continue on working on the other end points in the next video.
So let's go ahead and jump into that.