- [Instructor] Now that we have an item in our database,
let's create the end point
that will allow us to get all of our contacts.
Also, in between this video and the last,
I took the opportunity to insert a few items in the database
so we can have a lot more to work with.
So lets go ahead and create the next function that we need
which is get contacts.
So lets go inside our CRM controller file
and what we're going to do is
copy everything from line six to 15
and then paste it again.
And what we're going to do is create the get contacts function
from what we got here.
Because it's very similar from one to the other.
So basically, the response here
is going to be the same on every method that we're using.
So if errors, send error.
Response json contact, except to delete,
where we're going to insert a message here.
So the first thing we need to remove here is
the creation of a new contact.
We don't need that.
What we're going to do now is use the contact database
to find something.
So what we're going to do is change this here.
So, what we need to do is find something.
So use the find function.
And inside of that find function,
you can pass some arguments and options,
we don't need any for this particular case.
And then we're going to pass the error
and what we need as part of the answer.
And let's just close that out here.
Perfect.
So you need only one parentheses here
because we're closing it out right there.
And then everything else is the same.
If we get an error, send the message error.
If we get an object, send the object
or send the array of object.
And this is basically what we're going to get.
Okay, so let's save this.
Now we need to go to our routes
and add it to what we have in here.
So we need to add it inside of our import.
So lets just turn this into a multiple line import.
Like so.
And import here.
So I'm going to need to put a comma there.
And then lets see what's available.
So what we have here is get contacts.
Yeah, it is available.
So now we need to pass it inside of the get method here.
So what we need to do is remove all this gibberish here.
So what I'm going to do is,
literally get from the curly brace
and remove all the way up here.
Like so.
And then what you can do is pass the get contacts function.
And that's it.
That's all you have to do.
Lets save this.
Lets bring up our terminal.
I'm going to do control [].
You can also use view terminal to get it.
Everything seems to be working fine.
So lets go back to [] to test this.
Now this is the object
that I inserted in between the last video and this one,
and what I'm going to do now,
is go to get method, do non in the body,
so you don't need to pass anything for this one.
And then do a get call on this URL.
So remember, if we go to the routes,
we are calling from the get method,
with this URL here.
So lets go ahead and do that.
And I'm going to click on send
and then I have an array of objects
that I inserted in between the last two videos
and as you can see,
I have multiple objects here.
So, this is basically the array of contacts
that I have in my database.
So now we got our get end point ready
and we're about to pull our data from our database.