Loading
Lesson 10
Courses / Getting Started with Express (Learn Nodejs Backend JSON REST API Web Development)
Defining the PUT Route to Update a Book

Video Transcript

Welcome back. So we learned how to get all the books, how to get a specific book by ID, how to create a book. Now let's learn how to update an existing book. Looking back at our data structure here in book.js. Let's say I want to change the title of Donkey Holiday book from to something else. Now we need a way to first get this guy, then we change the title, then we update this guy. Now for the route itself, it's going to be a foot slash book slash the book ID. So we give the book ID as a route parameter so we can first find what book we want to update. And then for the foot request, we're gonna give it a request body with the actual title we want to modify. From Donkey Holiday, let's say, I don't know, Mr. Kihote. Let's change the title to that. So we're gonna have a body in the request. We're gonna have a request parameter. So to get these guys remember we use rack.body for the request body and rack.params for the request the route parameter. Okay, it's gonna be a foot verb. Let's get started. After post, we're gonna use app. You guessed it. Put slash book slash book ID with a colon prefix. The usual rack and rest parameters for the function right here. Then so we need a method in the book module to update the book, right? So let's first do that. Going back to the MOOC module. We have to do this because we don't have an ORM nor a database setup. So we're faking it with this module. Okay. Now let's create a new one here update. It's gonna be somewhat similar to what we didn't create and find one combined kind of. Anyway, so update needs first you need the ID of the book to update. So let's give an ID and then we need the actual new properties for the book. So let's just call it book. Now first you want to find the book. So we're going to use the same logic right here. So existing book is going to be books dot fine. Book is the parameter where the book ID is going to be the ID fast as an argument that enable soft wrap so you can see. Now we have that or we don't have it because what if the book doesn't exist? Well if it does not exist we should definitely return no or something. Anyway, but if it does exist we are going to go here after we find the guy we need to update the title. So if it's just one property you can just make a very easy for example you're gonna create the updated book. It's just the existing book of an object whose ID is the existing book ID but the title is going to be what? This argument book dot title because it's the new title. That's one way but if you have so many properties what are you gonna do? Type one by one? Yeah you could do that. There's another way if you want to include all the properties from the book into the existing one in a new object and you can do like that as so. Just do a three dots existing book then you say whatever from the book here. So this is what this is doing is it copies all the properties from the existing book then copies all the properties from book and if book does happen to have the same properties already defined here previously it's going to overwrite them. Okay anyway with that we can just return. Oops we cannot because we need to actually update our fake database. So what I'm going to do is I'm going to use a map method of the array type because if you look at it an update is actually just you're gonna go and find the one you want to update. Just change one thing. The length of the array is going to stay the same so we're gonna map through this guy. If it's not the book we want to update we don't do anything just return the object as is. Otherwise if this is the book we want to update we return a new object to this list with this title replaced. Okay so let's do it. So let's get to books.map and let's do something here. So if the book.id is the same as the id we want to change right we are going to return that updated book. Now if it is not we just return the book itself unchanged. So this map is going through each book in the list. If the book.id matches the id of the book we want to update we return updated book. That means the map is going to include in the new array the updated book. Otherwise it's going to include the book as is without any changes. Mind you that the map function it creates a whole new list so we have to reassign it to books like so because this is not going to mutate the original books. Okay now after we do that we finally update the list and we can return the updated book to the client who called it because maybe we want to return the updated book back to the client. Okay so let's say this go back to our route. Now how we're gonna do this we're going to do book.update what's the id of the book we want to update. The id comes from the request parameter so we can say rack.what.params.book.id but remember this is a string because it's in params so we need to parse it into an integer otherwise this strict equality right here is not going to work because we have an actual number versus a string if we don't parse it. So let's remove this guy. Extract it and make a variable book id parse int parse it as an integer. I'll just put the 10 there just to make sure it's a base then and then we have the idea of just use it in update and finally we need to pass it the updated properties. Okay so you could just pick take all of them if you want or you can filter out the unwanted ones I'll leave it to you to filter out as an exercise. I'm just going to include request.body like so and then this is going to update our fake database our coded data and then we just return this guy back to actually we send the response right send this guy back to the user so the client gets a copy of the updated book. Okay let's test it out. So we have three books again notice there's no more four and five because we restarted the server our hard-coded data is in memory and will be wiped out as and reset to its original state every time we restart the server. So we got these guys going let's change the name of Donkey Hoodie to something else. So let me change the verb here to actually let me go to the another tab so we can reuse the other one we got to use put HTTP localhost slash books slash to to is the Donkey Hoodie books id we need a body remember let's use raw not text but JSON application slash JSON we need want to change the title of the book to Mr. Kihote for example now let's try it. Did we get any response yes we did it's just down here I would like to drag it up so you can see better. So this is our request body this is our response body so we got a title not changed to Mr. Kihote to confirm that our database or fake database actually actually changed we have to do a get slash books to see that the index the second element is Mr. has now title Mr. Kihote and all the other records are unchanged okay so that's a 200 okay everything went well. So let's just review what we did in this lesson we learned how to use the HTTP app.put to make a put request to update a book the endpoint is slash books slash colon book id book id comes from the request parameters which is always a string so you need to parse it into an actual number to make sure of things match correctly inside our fake database search. Then we after we pass it to our fake database to update the book using the id and the rack body with the updated properties we send it back to the user of the 200 okay all right. Now what if it doesn't exist oh that's something we didn't talk about right let's go back here to our postman and try to update a book that does not exist like book number four right now does not exist we are getting 200 okay that's not good we should get at least a 404 right so we got no here so let's get back to her to our route so this guy actually could be no so we should extract this guy into updated book it's going to be equal that then if there is no updated book if that is no right we are just going to return right away with a response send what remember the status code let's put a status of 404 beforehand so we're going to send a response with the status 404 and we're going to send a message saying that the book you want to update is not does not exist how about that okay so that's our error kind of handling here so let's try again oh actually I forgot to place the updated book right here let's go to postman so do what we started the service so it don't change that but we don't care anymore anyway so we're going to do a put on book slash port which does not exist and now we get 404 and add pound the book you want to update does not exist okay so I just wanted to finish with that doing the 404 if the actual book you're trying to update does not exist so I'll see you in the next lesson
No comments yet (loading...)
No comments yet (loading...)
Did you like the lesson? 😆👍
Consider a donation to support our work: