- [Instructor] The final endpoint we need to create
is a Delete API so we can delete any of the data
we have in our database.
Let's get to it.
So we're back again and having fun
in the CRM Controller file.
And what we're going to do is, again,
copy and paste the last one that we did.
Like so.
And then we're going to call this DeleteContact.
And, again, it takes a request and a response parameter.
And then, instead of FindOneAndUpdate,
we're going to do Remove on this one.
So we're going to remove from the database
the contact that we're going to pass.
Again, it needs an _id to be passed as the key
and we need to get it from the Request and the Params
and again, make sure that we have it from ContactID.
We don't need to pass anything else,
so we can delete all of this and keep the error and contact.
And then, if we got an error,
we send a message of what the error is.
And then it has a post to do a JSON
and pass the object that we got back
because we don't have anything to pass.
This contact has been deleted,
so we can't pass a deleted contact.
What we're going to do, instead, is send a message,
so let's remove that contact
and, in between curly braces, pass an object with a key
that will be called Message.
And then the message will be "successfully deleted contact".
Like so.
And that's all we need to do for the function.
So we can save that
and then let's go into CRM Routes
and pass that function to the right method
which is the Delete method.
So, first, we need to make sure we import it first.
So let's go and import the DeleteContact function
and then scroll all the way down.
Again, grab the second parentheses
and the one before last.
And pass DeleteContact.
Now let's clean this up a little bit
so we can add some notes
and you can always go back to this course
or the code that you just written
and understand what has been done here.
So middleware is already written here,
so let's go ahead and add a note here, so a comment.
So forward slash, forward slash.
This is a post endpoint.
Then let's go down here and then add a comment
before the Get.
This is to get a specific contact.
And then let's also add a note at the Put,
so this is updating a specific contact.
And finally, do the same for the Delete.
And this is deleting a specific contact.
Perfect, so now we added notes here,
we have all of our request methods
covered with a function that will do something
with the database.
Let's save all this and let's go test it
inside of Postman.
So let's go ahead and delete that specific contact
that we've just been working with for the past three videos.
So let's grab the ID and we have it here,
but I'm going to make sure, so I'm going to copy that
and then paste it here like so.
And then, instead of a Put, I'm going to
select the Delete method.
We don't need to set anything,
so we can select None on the body.
And we can go ahead and hit Send.
And our contact has been successfully deleted.
But this is a good opportunity to take a look
at what Robo T can do for you.
So let's go and grab Robo T.
So I'm going to open Robo T and make sure I'm connected.
And, when you're connected, if you don't see
the CRM DB, if you've actually connected
before we created the connection to Mongoose and Mongo,
you can refresh it and you're going to see the CRM DB.
So now what we could do is look into the database,
so basically Robo T is connected to the database.
It's not connected to the API.
It's inside of Mongo.
So let's go into the collections
and take a look at the contacts.
So, right now, I have five objects
inside of my database,
and these are all the five objects
that I inserted at one point
or another throughout the course.
But what we could do is edit these objects if we want to,
so let's go ahead and copy this object.
So let's grab five D,
so one thing you could do, if you want to copy
a specific object ID, what you could do
is go into the Edit mode and grab the ID here,
so what we're going to do first is grab it inside of Postman,
so let's go and copy that and go inside of Postman.
And, instead of delete,
we're going to paste that new contact
and get that specific contact, so let's go and get it.
So now we have the data that's coming from
the database and the API.
So let's go back to Robo T
and what we're going to do is change something
in the object here,
and then it's going to change it on our API.
So, as opposed to 555, we're going to do 666.
666 and 1234.
123, 123, 123, okay.
So we have ten numbers in here,
so we're going to save this in Mongo.
Now the object has been changed.
So now we have 6666666
and we're going to go to Postman now
and make a request for that specific object or contact.
So, right now, we have 555.
Let's make a send and now we have six.
So what Robo T allows you to do
is make changes directly into the database
or take a look at what the database looks like
and the actual data inside of it.
So that is a really cool tool
that allows you to take a look at what you have
in your database, make changes,
and then actually play with the database
as opposed to the API.
So now we've got a full API
with the proper endpoints to create,
update, and delete items in the database.