All right, so now that all we have left is the delete method, but I'm splitting it into two parts
so that we can try out adding some basic authentication as well.
We're going to be implementing the API for people to be able to delete jokes from the Jokes API.
The first challenge is challenge number seven and we're going to hit up forward slash jokes, forward
slash the ID parameter, and then this is going to hopefully delete the joke with that particular ID,
So we're only deleting one joke in this case and we're passing the path variable of ID and giving it
one that hopefully matches one of the jokes in the API.
We hit send and this delete request will come back as okay with code 200.
If everything went okay again, I give you the choice of trying to figure out how you can do this yourself,
which can be a little bit tricky.
Or let's go through everything together.
We're using the delete method from Express and we add in our endpoint and in the callback we get hold
of the ID that was added to the request.
So again, we can either use pass int or we can replace this triple quote into double quote.
Either of these options will work.
I'm using find index again to find the index of the joke that matches the ID that was passed in this
one.
And then if the index is greater than minus one.
So if it actually exists, then I'm going to go into the jokes array and use the splice method in order
to remove the joke with that index.
That's essentially what this line does.
It takes the search index and then removes only one element from the array.
And once that's all done, I send status 200.
Everything is A-okay.
But if there were any errors, then I send status 404 and tell them what the error was.
That's all that's required to delete a single joke.
And because it's just a single joke, we don't have any authentication in here.
The next challenge, challenge number eight simulates a situation where you need an API key in order
to be able to delete something, because in this case we're actually saying let's delete everything
in our joke API, let's just wipe our database, which as you can imagine is a very dangerous thing
to allow people to do.
In this case, we're saying you need to have a master key, which is in the format of a multi letter
code and that is going to be added as a query parameter under the key of key.
And when you have access to that, then you can make a delete request to forward slash all and when
you hit send it will delete everything in the database nuclear option basically.
So the actual deleting part is very simple.
We're just wiping the whole array clean.
But how we do authentication is something that is a little bit new.
In this case.
Again, you can take a look at the starting code.
If you scroll right to the top, you'll see a variable called master key, and that variable is going
to be something that we're going to learn in later modules.
When we learn about authentication, when we learn about how to generate unique uuids.
But let's just assume that we somehow have access to a master password, which is called master key,
and the user needs to enter it.
When they make this API request, we check the value of master key against the value that was inputted
by the user, and if they happen to match, then we say okay, we'll just go ahead and delete everything
in that jokes array.
So again, either you could have a think about it, try it out yourself, see it as a challenge, or
we can walk through it together step by step.
All right.
So at the top of the file, we've got our master key, and this is the copy that we're keeping on our
server to check against what the user sends in.
Now, the user is going to use a query parameter in the URL, so it'll be forward slash all and then
it will be query parameter which is called key, and they make it equal to whatever it is they decide
to type in.
And we have to check if the user key is equal to the master key.
And in this case, all we're doing is just wiping the jokes array clean, setting it back to an empty
array.
And in this case we send status 200.
Everything was implemented as you requested.
Otherwise we say 404 and we say you're not authorized to perform this action because the Elf statement
is on this check right here.
If the user key is equal to the master key.
So this is a very simplified way that we can implement API key authentication or username password authentication.
And as I mentioned before, later on we properly look into authentication.
We'll look into generating these unique IDs, we'll look into hashing passwords, storing passwords,
we'll look into Bcrypt, we'll look into how to hack passwords and all of that.
But for now, we can be satisfied that we're just doing a very, very basic password check.
So the admin password against what's typed in in the request and that gets our API and makes sure that
anything that is risky is not performed by just any user on our service.
That's it.
Check to make sure that your code matches what's in the solution or if it doesn't, make sure that your
functionality behaves as you would expect to.
And once you're ready, head over to the next lesson.
We're going to put all of these skills that we've learned in this module to practice by building a blog
API.
So for all of that and more, I'll see you there.