Edit A Quote
6:04 with Treasure PorthIn this video, we'll add functionality to our app that will allow the client to edit quotes.
This video doesn't have any notes.
Let's write an update route to
edit an already existing quote.
A put request indicates to
our application that a change
is going to be made to
an existing resource.
We're going to handle put requests
to the /quotes/:id route.
We're going to use await
inside this callback, so
we'll indicate that this
function is asynchronous.
Now we'll need a trycatch block.
Into the catch block, we can just copy and
paste the same error from
above in our post route.
Inside the try block,
we'll use the updateQuote function.
Go ahead and
hover over the updateQuote function.
You can see it accepts one parameter,
an object containing the content
that we wanna update the quote with.
So we'll expect the client to send us
an object containing the author and
quote text to replace the current values.
The client will send a request
containing the new object,
the endpoint of the quote
that they wish to change.
Much like we did in
the get /quotes/:id route,
we'll first retrieve the quote
And passing it the id of
the quote we're looking for,
which is available via req.params.id.
So records.getQuote(req.params.id), and
we can save that to
a variable called quote.
Remember, we're going to pull
the id from the request and
feed it to the records.getQuote method.
The getQuote method will then return
to us the quote we're looking for,
saved in a variable called quote.
Also, we'll need to wait for
this code information to come back
before we move onto the next line of code,
so we can add await here.
We're nearly ready to use
the updateQuote function, but
first let's make sure
the requested resource exists.
In other words,
that the id points to a valid quote.
If the quote doesn't exist,
we'll send a 404 error.
And we'll send a message
that the quote wasn't found.
If getQuote has returned a valid quote,
we'll need to reassign the quote's quote
and author properties to reflect the new
information sent to us by the client.
Recall that we have access to this
new information via req.body.
So we have the quote we wanna change,
let's change it.
We'll set the quote object's
quote property equal to
the new quote property
sent to us by the client.
And we'll do the same with the author.
So here we're getting the new
information the client has sent us.
And we are reassigning the value of the
quote and author properties to match that.
Now that we've updated the quote,
we can pass the new quote
to the updateQuote method.
So let's go ahead and
copy and paste this up here.
And we can pass quote to
the updateQuote method.
The updateQuote method will save
the new quote to our data store.
This is an asynchronous function, so
we'll need to await this as well.
Now we'll need to send
some kind of response.
For a put request, it's convention to send
status code 204, which means no content.
This generally means that everything went
okay, but there's nothing to send back.
Up until now,
we've responded to requests with JSON,
but for put requests, it's convention
not to respond with anything.
The data store simply gets updated, and
we send back a status code indicating
that the update went as expected.
We need another way to end the request, or
the server will just hang indefinitely,
and our application will
appear to be broken.
We can end the request with
the Express end method,
which simply tells
Express that we're done.
Let's test this out in Postman by
editing the quote with an id of 8721.
We'll edit it by adding a Jr.
to the end of Martin Luther King.
So up here, change the method to PUT, and
we'll send the request to
Click on Body, and then raw.
And here we'll send an object
with our request containing
an updated quote and author.
So I'm actually just going to copy and
paste this from below.
And add the Junior.
Go ahead and press Send.
And it looks like nothing happened,
but we did get a status of 204.
And if we go ahead and
take a look at our data.json file,
you can see that that has
been successfully changed.
So we're finding the quote by id,
taking the information the client has sent
us, and using it to update the quote.
Passing the new quote to
the updateQuote method to be saved.
In a more real-world situation,
you'd also wanna validate the information
the client has sent to you.
For example, has the client sent data for
all the required properties?
Is the data in the form your API is
expecting, like a string or a number,
and so on.
When you're using an ORM,
you'll most likely create data
models to do exactly that.
You define the shape of
your data in one place.
Which properties are required, which data
type each properties expect, and so on.
So our updateQuote route is working great,
let's move on to the delete route.
You need to sign up for Treehouse in order to download course files.