All right.
So now that we've covered all of the get requests, it's time to cover some of the posting and updating
methods.
So our post put and patch methods to create new jokes to add to the API and update the jokes that are
in the library.
The first one we're going to tackle is the post requests.
We want to be able to hit up the forward slash jokes endpoint and to submit content in the body of the
request using the W-w-w form URL encoded format.
And the two parameters that we can hit up is text and type to provide the text of the joke and the type
of the joke.
And these are two strings that we will use to submit our new joke.
The ID should be automatically created by adding an ID that is one higher than the last ID in the jokes
array and the other two pieces of data that comes from the body of the request will then get added to
a JavaScript object and included in the jokes array that's in that variable called jokes.
So if you want to try this yourself, pause the video now.
But otherwise, let's walk through how to do this.
We're going to use a different verb now.
And this method comes from Express.
It's the post requests that you've seen before.
So using App.post, we specify the endpoint that we want to receive this request on forward slash jokes,
and then we provide our callback to handle that request.
The first thing I'm going to do is to create a new object which I'll call new joke.
And in the new object I've got my ID, I've got my joke text, and I've got my joke type.
These three parts that create the joke itself, the ID I'm simply going to create by looking at the
length of the jokes array.
You'll notice that they are numbered from 1 to 100 and the length at the moment without any modification
is 100.
So 101 will be the next one.
And then once we're on 101, the next one will be 102 and so on and so forth.
So this will be automatically created without needing to get hold of this data from the user.
Now the next two parts use request dot body dot text and request dot body dot type.
If you scroll through the index.js, you'll see that I've already included the body parser code that
is required for us to be able to tap into request dot body and I've added URL encoded as an option to
true and we're basically ready to go using that middleware in order to receive these two pieces of data
as I specified before.
You can also use Express has a native passing ability, but for me I prefer using body parser because
it just makes it clear to me perfectly clear what that part of the middleware does.
And you'll see developers using body parser on all day long, all the time as well.
So this is very much standard practice.
Once we've created this new object, we're going to push it to our array of jokes, add it right to
the end of that array, and then we can also console.log the last joke.
So jokes slice minus one.
This line of code basically is equivalent to saying take all the jokes and then access the one at index,
which is the last index.
And this is how we would do that in JavaScript using the slice method.
So slice from zero backwards, which somehow in JavaScript land goes back to the very last item and
picks it out.
You can try this for yourself and see it.
Don't just take my word for it.
And then finally we're going to send back the new joke that the user created.
Take a look through this code.
Make sure that you understand everything.
If you need to look anything up, such as the slice method, you can check that out and you'll notice
that the object that I'm sending back as the Json to the client is the one that they created.
This comes from all of their data rather than the one that comes from the last item in the array.
You can make the design choice of sending this back to them just in case you wanted to check that this
works.
And you can also add in error handling or add in some checks, making sure that the joke text contains
however many characters you want it, and the type of joke matches a certain list of types that you've
specified.
We can always extend this API, but the key point here is you know how to write the API to handle a
post request.
That's all that I'm looking for here.
Pause the video.
Implement this in your own index.js if you haven't done so already.
All right.
Challenge number five.
We're going to make a put request.
So just as a reminder, a put request is a complete update.
That means you have to provide all of the parts that are not automated into the object.
So in our joke that is providing the text, providing the type, and of course we'll need the ID in
order to get hold of that particular item.
Now the ID identifies that object in the array, so we don't want to update that unless we want to mess
everything up.
That's the one thing we're going to keep constant.
But the rest and all of the content of the joke object is going to be completely replaced in Postman.
You should be able to send a put request if you have the solution JS up that server running using Nodemon
you should be able to send this request and be able to update completely the joke with a specific ID.
So if you take a look inside the params tab you'll see that I've specified an ID of two.
And that's the joke I want to update which gets replaced in this endpoint URL becomes forward slash
two and then I'm providing some text and the type and you'll notice that if you leave one of these blank,
you will end up with nothing in that updated joke.
And this is how put requests are meant to behave.
And the reason why we're doing both put and patch is for you to be able to see how it's done.
But in reality, when you're writing your API, you would probably choose either a put or a patch to
handle update requests depending on your needs.
Again, either pause the video now and try to figure out how to do this or continue with me and we'll
talk through how to do this together.
The first thing we're going to do is we're going to get hold of the ID that comes in from request dot
parameters, dot ID.
And again, remember, you can either choose to pass this int to turn it from a string to an integer
so that you can use the triple equal sign operator, or you can choose to not pass it and simply use
the double equal sign operator to check for vaguely equals.
And this is exactly equals.
That's kind of how I think about it in my head.
Now the next thing is we're going to grab hold of the ID, the body text and the body type.
These two come from the form submission, and then we're going to find that particular joke because
we need to replace that item in the joke array completely.
I'm choosing to use the find index method.
This is going to allow me to add a callback into that method for a match criteria.
So it's going to loop through each of the jokes inside the jokes array.
It's going to check each of their IDs against the ID that we provide up here.
And when there is a match, it's going to output the index of that joke into this constant called search
index.
And then I can tap into the jokes array using that search index and I can set it by simply using the
equal sign to this replacement joke here.
So it's going to completely replace the data that was in there before.
And this is one of the ways that you can do a put request.
Finally, we're going to resend the replacement joke back to the user just so that we can show them
this is what happened and that joke will be replaced in the data store.
Pause the video now and check if there's anything that you don't understand here.
Check the documentation for Find Index.
If this is the first time you're coming across this method and then try and implement this in your own
index.js and verify it by running the index.js and try to hit it up using Postman.
The final challenge in post put and patch is the patch request.
The patch request is like our little patchwork quilt.
It is a little update to a complete resource and we're only choosing to update one small bit of it.
And as you might have already realized, this is most often what you're going to be choosing when you're
making an update to a resource.
You don't want to update the entire thing.
Say if you have a user, they have their address, their name, their password.
There's very, very few instances where you would want to provide all of their data to update.
In most cases, you just want to pick one parameter to update.
That's how our patch request is going to work.
It's going to hit up jokes forward slash ID.
So this is the path parameter that the request has to provide, and then we're going to search through
our jokes to find a match for that ID, and then we look through the body of the request and we can
see what it is they want to replace.
So in this case, we want to replace the type of that joke into something else.
So joke with ID number two.
Why did the scarecrow win a prize?
Because he was outstanding in his field.
Um.
Okay.
This is not the funniest of all the jokes.
I don't know why I'm laughing, but, um, what we want to do is change the joke type to agriculture,
because we've decided that we have too many agriculture based jokes and we're going to create this new
type.
That is the goal.
And you can again, think about it, try it out or pause the video and do some Googling, do some searching,
see if you can figure it out.
Or we can walk through how to do this together.
Pause the video or continue.
This is how I've decided to write the patch request.
Yours might be different.
And in fact, there are many, many ways you can do this.
I've, as always, opted for the most readable version, so we're going to use the apt patch method
from Express, provide the endpoint that we want to hit up, provide the callback to handle that request,
pass the integer from the request parameter ID so that that number two string becomes number two as
a number.
And then we check it against the joke ID by looping through all of the jokes in our jokes array, checking
each of one of them and finding the joke that matches.
So we store it inside existing joke, and then we construct our replacement joke.
And notice here what I'm doing.
The two things that we can patch in this joke is the text and the type.
What this code does is it checks to see if there is a request dot body dot text.
If the patch request actually included some text in the body of the request, in which case replace
that in the joke text.
But if this is nil or it doesn't exist, then this or operator is going to allow the second part, which
is the existing joke that we found and grab the text from there anyways to put it back into this new
object.
Essentially this or statement says go for this if it exists.
Otherwise, go and set it to this.
And this is a really neat pattern that you'll see very often.
You can, of course, make this more complicated or more readable in your case by using an if statement,
you can say if request dot body dot text exists.
So just simply by leaving it this if this has any value, it's going to evaluate to true, in which
case we can go inside this curly braces and we can set the joke text to this request dot body dot text
and we can write it in many, many, many lines.
But this is something that you might see out in the wild, and this is one very efficient way of doing
it.
If you've never seen this before, test it out in your console or in a blank piece of JavaScript file
just to make sure you understand what's happening here.
Now again, we do the same thing that we did with the put request.
We find the index of the joke that matches the ID that the user requested and then we replace it with
the replacement joke that we just created up here.
And then we send that replace joke back to the client and we assure them that everything went correctly.
There's quite a lot of code in this patch request.
It's a good idea to go through it and make sure you understand what all the different parts are doing,
such as the find method, the find index method, the Or operators.
Make sure that you're happy with all of it and then implement it yourself.
Once you've done all of that, make sure you use Nodemon to run your index.js and check that all of
the routes in the collection posts put and patch all work as you expect it to.
So take a look in the params section, take a look inside the body section, make sure that all of these
parts you should not have to change, but when you send it, it should come back with a correct response
instead of an error.
So obviously in my case index.js none of these things have been implemented, so that's why I'm getting
that error.
But in your case, if you've written the correct code, it should work as expected.
Now all we have left is figuring out how to delete jokes.
So that's what we're going to be doing in the next lesson.