- [Instructor] In this video
we'll complete our post endpoints
so we can add new items to our database
and then test it.
So let's get to it.
So first you want to have CRM controller opened
and this is in the controllers folder.
And what we're going to do first is import Mongoose
because we need to use it
to actually save things to our database.
So let's go ahead and from Mongoose.
And then we'll import
contact schema
from,
model, CR models,
like so.
And by the way,
if you want to take a look
at what's available inside of CR models,
what you can do again,
this is a tip that I'm giving over and over in my course,
you want to do the from first
and then do something like this.
And then it's going to suggest what's available for you.
as contact schema.
Then we're going to create
a new contact, constructor
with mongoose.
And then execute the model function
and pass the contact
as the main object
to create new contacts in the database.
And leveraging the contacts schema
as the model for our new contacts.
So we're leveraging
we created here
and passing that to the contact object
that we'll create new contacts with.
So let's go ahead and do the function
add new contact,
which takes a request
and a response,
typical function for those types of request.
And then we're going to create a new contact
inside of that function.
By passing the new constructors,
or contacts.
So basically,
we're passing the model
to that contact here,
with the body from the request.
So we're passing the data
that is coming with the request
inside of that new contact.
And then we're going to save this new contact
into the database.
And then basically use the save function
and pass error
and contacts,
so this is basically the function
that will allow us to save it
and then give us an answer of what's happened.
Did it give us an error?
If it did give us an error,
then do respond
and send the error.
Otherwise,
do send the JSON with the contact
so, we're going to get the contact information
in a JSON file
with all the information
that has been saved in the database,
so basically,
if it provided us with an ID,
it provided us with anything else,
then we'll get that inside of the JSON object.
Okay?
So that is basically our function.
Now we need to pass it to the routes.
So let's go into the routes.
And we have a POST method here,
which right now sends post request successful.
What we need to do now
is remove from the second parent tease
to the one before us,
and pass the function that we just created.
So add new contacts.
So what we need to do before we pass it,
we need to import it inside of that file.
So let's import it.
And again, what I'm going to do
is put the curly braces
and finished a from
controllers,
CRM controller,
and let's see what's available
inside of the CRM controller.
Let's see, add new contact,
yeah, it's available,
all right?
So now we can pass it to the post.
So add new contact,
and now we've got our route
using the function that we just created,
which will save a new contact to the database.
So let's save all this.
And let's test this imposement.
So let's take a look at what it does.
So one thing that you can keep in mind,
if you want to take a look at what kind of data
this particular model takes,
you can go back to the model,
so it takes first name, last name,
email, company phone,
and the created date
will be created by the database automatically.
So let's go and,
create a new item inside of our contact
on the database,
so lets use post
this is the right address.
Before we send it,
want to make sure that the body is in form URL encoded,
and the editors also replicate that.
So, this should be done automatically
when you select URL encoded
and now we can continue working on this,
so let's do first name,
last name.
And after that it was email.
Let's do a bogus email.
Then we have company.
Let's do LinkedIn.
And then we have a phone.
Five, five, five, five, five, five, five, five, five, five
Okay?
So we have all the data that we need.
We selected form URL encoded
editors are on the right editors,
so we're ready to send our post.
Let's send it.
And there you go.
You got an answer with the ID of that particular item
in your database,
first name, last name,
all the information that we've put in our forum,
and we even got a version
which was created by the database,
and the created date has been inputs directly
from the model.
And the schema.
And now we got this information as well
in our object.
So as you can see it is fairly easy to add an endpoint
and then test it with (mumbles)
We can also get some very good tips on code syntax
when you come around to do the front end piece
with postman as I demonstrated earlier.