3.11 Updating and Deleting Tasks
It's great to be able to create and retrieve tasks, but a complete API needs more. It's now time to finish up your API by adding the final pieces to the puzzle: updating and deleting tasks.
1.Introduction2 lessons, 05:46
2.Getting Started4 lessons, 29:58
3.Building the API11 lessons, 1:16:10
4.Conclusion1 lesson, 01:37
3.11 Updating and Deleting Tasks
Now we're gonna turn our attention to the put and the delete operations. Now they're gonna be somewhat similar to the previous operations but with the slight change in the service methods that we're gonna be using, but for the most part, it's gonna be fairly simple. So let's start with the update. So what we want to do is we want to get two different things from the request. We wanna get the ID that's coming from the URL, as well as the body and then do the necessary updating that we need to do. So let's go ahead and say, const task = to, and we're gonna be getting here the body of the request. Once again, remember, we need to. Cast this into a task. So then we're gonna say, context-request.body. So now we have our task. Then we want to go ahead and do the updating. So we're gonna say the updated task is gonna be equal to. And we'll do an await service.updatetask. And we're gonna pass in the ID. So once again we need to get that, which is gonna come from the context.params.ID, and then we also need to pass in that task. So now that we have that, we have our newly updated task. And at this point, we're going to do another if, if we did get that updated task then we know, everything seem to work correctly. Then we'll go ahead and set the status of the result equal to 200, and the body equal to that updated task. And then we'll do the else side of the world, and in this case, we're once again, gonna do a throw, cuz we're going to assume at this point that the reason we weren't able to get that updated task is cuz because it wasn't found. So we'll go ahead and do that. So now we're gonna handle the delete, which is gonna be somewhat similar, only we are not gonna be getting a body in this one, it's only gonna be base on that ID. But remember, we are returning that deleted task for the end user, so say const deletedTask and we'll set that equal to await service. We'll go ahead and use the delete task method here and then we're gonna be passing in the number here. The number associated with the context.perams.id just like that. So hopefully we get something back and then we'll say, if. Deleted task, if it's trusty, then we'll go ahead and say that the status you know you, could do a couple different things here, for this one, I tend to do just a 200 to say that the operation succeeded, that everything is okay. There are some other ones you could send back a no content or things like that, depending on how you wanna do it, how you wanna structure your API, but I think a 200 should be just fine here. And then we'll do a deleted task as the body. And then else if that deleted task didn't come back, then once again, we'll go ahead and throw a forum for. Excellent. So let's go ahead and save that. And if we got everything correct here, then our server should restart. And now that we are restarting it, remember, because we're using an in memory data store, now that it's restarted, everything should be gone. So let's go ahead and just prove that. So we'll go ahead and do a request to get a list of tasks and we're gonna see that there's nothing there, which is kind of what we expected. So then let's go ahead and do this post again, so let's send that and the body of the responses is the same again. And actually what we'll do here is, let's do another one. Let's do Sample Task #2, and we'll pass that one in as well. So then our response back, you see here, Sample Task #2 is completely false an ID number 2. So our logic to implement those IDs is working correctly. If I were to come back here and say, give me all my tasks, you're gonna see I get a collection or a list of task objects. So that looks good. But let's say now I want to make an update. Let's say I want to update Task #2 to be completed now. So what I can do is I can come in here, and I can do a PUT. And that put is gonna be going to tasks, in this case, we wanna make sure we're updating Task Number 2. And then we're also gonna need to pass in a body and once again, this is going to be raw of type Jason, let's go ahead and pass in that object in this case, remember we want to. Change this to be, is complete true. So we're gonna do a put now. So let's go ahead and send that in. And our response back is, is complete true, which is good. So we can also come back to our list of tasks and send that again, and you're gonna see that it has been updated properly. So that's good. And then let's just say, maybe sample task number one, the first one, we didn't necessarily need that one. So let's go ahead and open up another tab here, we'll do a delete. And this time, we are going to do a delete on task number one, and there's nobody for this one. All we're just sending through is the URL here. So we'll go ahead and do send. And we got back this object, which is what we were expecting. And if I were to come back here and take a look at the task list, do ascend. I'm only getting back sample task number two. So there you have it. Now we've created a full API with a get to get all tasks, to get an individual task, we can now post to create a task, we can do a put to update it and do delete to delete it. So now you've seen the basics of how we can use COA, and it's very powerful context to be able to do a lot of different operations within an API using COA on top of node, which actually turns out to be quite simple.