Unlimited WordPress themes, graphics, videos & courses! Unlimited asset downloads! From $16.50/m
by
Lessons:18Length:1.9 hours
Introduction to koa javascript framework 400x277
  • Overview
  • Transcript

3.7 Finishing the In-Memory Task Repository

Let's go ahead and finish up our in memory task repository implementation. So we already have our gets now we wanna handle the create, update, and delete. So let's start with the create. Now there's a number of different ways that you can do this. And when I say that, really what I am referring to is the ID that gets associated with the task. Because what we're assuming here is that the consumer of our API is sending in a fully populated task object. But there's really no way for that person to know anything about how the ID is generated, or what the next ID should be, or whatever. So we're not assuming that that consumer has any knowledge about that whatsoever and they're going to have us take care of that, which is really the way that it should be. Now there's a number of different ways you could do this. You could come up with some crazy algorithm to generate unique numbers. You could create unique GUIDs every single time. You could do all sorts of things. I'm gonna keep things really simple. Remember, I made the ID property of the task a number. And that's really what we're gonna do here. So what I'm gonna do is I'm simply going to add another private variable here. And I'm gonna call this _nextId and I'm gonna initialize that to 0. Actually I'm gonna initialize that to 1. So the _nextId that's gonna be used is going to be 1 for whatever's added in to my task list. So the first thing that I'm gonna do is I'm gonna say task.id is gonna be set equal to the _nextId. And then once I do that I wanna make sure that no other tasks get that ID. So I'm gonna make sure that I go ahead and increment that. And then after I've done that I can go ahead and add that to my tasks list by using the push function. And I'm gonna push on that task. And then once I've done that I'm going to go ahead and return this updated task or this newly created task with an ID. And then I have to make sure that I make this method asynchronous. And there you go, it's really just that simple. So now we have our create. Now we wanna handle our update. Now our update here, you can do a number of different ways. You will typically wanna spend some time making sure that the user is passing in a task ID and a task to be updated that actually exists. So you might wanna keep that and to take that into consideration. For this one, we're just going to assume that the user has already pulled down a list of tasks, and has grabbed the one that they wanna update, and is updating it appropriately. So the way that we're gonna handle this is we're gonna say, let updated task be equal to, and we wanna be able to filter on our list. So we wanna be able to go into our tasks and we want to filter and find the appropriate task that has an id equal to the taskId that was passed in. And this filter is actually gonna return a collection. So we wanna make sure that we grab the first ones. So now that we have this updated task, I can update it. So what are the properties that I would update? Well there's really only two. You can really only update name and the isComplete flag. You're really never going to allow a user of your API to update an ID. So what we're gonna do is simply say updatedTask.name is gonna be equal to task.name. And updatedTask.isComplete, is gonna be equal to task.isComplete. So we're updating it to be whatever it was that was passed in to our API. And then at the end of this we're just gonna go ahead and return this updated task so that the user can see exactly what the new version is, and we'll make sure that this is async once again. All right, so that takes care of the create and the update. Let's go ahead and handle the delete. Now remember in this case I kinda made the decision or made the choice to pass back the particular task that's being deleted, just so that the user can see what the current state was before it got deleted. So what we're gonna need to do is, we gonna need to find this particular task that's represented by this ID in our list. We need to find it, we need to remove it. But not only just remove it but pass it back to the end users. So there is couple steps here but it's not too bad. So the first that we're gonna do is we're going to find the index of where this exist within our tasks list. So to do that, we're going to find the index of a task that has a task ID that is equal to the task ID that is being passed in. So that's gonna give us an index of where it lives within that task array. Once we have that, we can go ahead and get it. So we can say this._Tasks, and I want to get whatever task lives at that index. So now I have that. And now I want to remove it from the list. And so in order to do that we're going to use the splice function here. And what splice does is it takes in an index of where you want to start and then how many you want to remove, and I only wanna remove one. So now I have the task that's being removed, and I have removed it from the list. And now I am simply going to return that task. And once again, this needs to be asynchronous. So there we have it. Now we have a basic implementation of an in-memory task repository that implements our ITaskRepository. Now you may be saying, why go through all this trouble? Why do all this? Well we're getting there, we're very close. In the next lesson we're gonna add in one additional layer and that's going to be the service layer. And I'm gonna show you some neat things that you can do with that, and where this ITaskRepository is gonna come in handy when you start to augment your application to be in different environments. Whether it be on your development machine, maybe in a test environment, or even maybe in production. So let's go ahead and see how it can handle that.

Back to the top