As promised.
This is a challenge video and in this one you'll be setting up two end points that allow someone to
fetch multiple tasks or a single task by I.D..
Down below I have the challenge comments big picture goal the set up the task reading and points.
The first step is to create the endpoint that allows you to fetch all tasks now later on in the class.
When we add authentication in will limit this to being able to fetch all tasks that you actually created.
If I as my account create a task you as another account shouldn't be able to read it.
We're not worrying about any of that for now but I wanted to mention it's something we'll get into later.
So you're going to set up one end point for fetching all of the tasks then another for fetching a single
task by its I.D..
Similar to what we did in the last video for users then you'll use postmen to test your work set up
new requests for both of those endpoints firing things off and make sure you're getting the correct
results with the second end point.
Make sure to test it with a valid I.D. and an invalid I.D. ensuring that you're getting the expected
results in both scenarios.
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 go ahead and jump right in by starting with the end point that's going to fetch all of the tasks
right here that'll be app dot get to set it up and the path is a forward slash and tasks.
Now from here we're going to set up our callback function with request and response and the first thing
we're gonna do is use the task model and access its find method.
In this case we're not actually trying to limit it by any criteria.
We want to fetch everything.
So we're not going to put any attributes on the object from here we can set up our call to then and
we'll get access to the tasks that were found.
Now it's gonna be an array but it might be an empty array if you don't have any tasks.
What are we going to do.
We're just going to send them back using response dot send send the tasks Next up down below.
We want to make sure to handle errors when things go wrong.
We'll set up that catch call right here.
We're going to provide the callback function and in this case we'll just send back a five hundred.
So response dot status.
Five hundred.
Then we'll go ahead and use send.
So that's gonna take care of the tasks route.
Let's go ahead and test that before moving on to the next one.
Over inside of post man I'm gonna add a new request to our collection.
I'll call this read tasks.
FORERO I'm going to create the new route I'll go ahead and save my old one first then I'll crack open
read tasks and set up the U.R.L. local host.
Port three thousand forward slash tasks.
And if I go ahead and fire that off what do I get down below.
I get all of the tasks I've created so far so that is it for the first route.
Let's go ahead and knock out the second one.
That'll be a another call to app.
Dot get this time providing a path that also contains one of those root parameters so up above we had
used one for this route and we'll do the same thing down below.
So the route is forward slash tasks forward slash colon I.D. And once again getting the request and
the response and the first thing we're gonna do is extract that I.D. const I.D. equals request dot parameter
dot I.D..
And from here we can go ahead and use defined by I.D. method on task that is task dot find by I.D. providing
the I.D. as the first and only argument then we call that right here we'll go ahead and set up that
callback function.
And if it did work we will get the task now.
It could also work and we don't get a task.
So we want to make sure to set up that conditional logic.
Once again right here if there is no task.
What are we going to go ahead and do.
We will use return to stop the function execution and we'll send a 4 0 4 back response.
Dot status for 0 4.
Then we'll use send.
And next up.
This is only going to run if there was a task.
In that case we want to send that back with the two hundred status code response dot and task.
Excellent.
The only thing we need to do is handle other errors that come up using catch.
Right here.
We're gonna go ahead and just send back the 500.
Once again response dot status five hundred dot send.
Perfect.
Now we're all done.
We can move on to the final step which is to test our work.
So I'm going to remove the challenge comments.
We have our two routes in place and over inside of postmen we'll test out the second one.
That's going to require us to create a brand new request in our collection.
I'll call this read task singular and we'll go ahead and set up the.
You are out.
I'll make sure to save my old one.
Crack open the new request and start to fill this out.
That will be local host on port three thousand forward slash tasks forward slash the I.D..
Now I can grab one of the task ideas I had over here.
I'll go ahead and grab the one for the task.
Eat lunch.
I'm gonna take the I.D..
Bring it over to the other request tab and paste it right in.
Now if I go ahead and fire this off what do I get.
I get my two hundred and I get my task showing up with description of eat lunch.
Now let's change this I'll change the leading five in the I.D. over to something else like a one.
This should result in a 4 0 4 and that is exactly what we get.
No task was found with this I.D. which is correct.
So now that we have this in place we have the resource end points for creating resources and for reading
them.
That leaves just two left.
The ability to update our resources and the ability to delete resources.
Let's go ahead and continue on in the next lecture.