3.4 Creating the Task Model
The app you are currently building is meant to manage tasks. Up to this point, there really isn't much in the app that has anything to do with this concept. So, in this lesson, you'll create the
Task model class that will be used throughout the rest of the course to represent the subject of your app.
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.4 Creating the Task Model
So far to this point, we've done a pretty good job of getting the basics of Koa up and running. Which is great, but we've kinda have almost completely ignored the concept of what a task really is. Sure, we've defined the task's route and that's really great but we're just doing a bunch of kinda boiler plate out of the box, messaging and sending things back to the consumer which is not really what we want. We ultimately want to be speaking a very common language throughout our application, which is going to be that of a task because this is going to be kind of like a task based API. Like I said before, you could extend it to become a task list API, or take it wherever you really want to go and take these fundamental concepts and move them into the future. But really right now, we need to start introducing the concept of a task and what that's ultimately going to look like. So what we're gonna do now is we're going to go into our source folder here and we're going to create another folder and this one is going to be models. Now typically, when we're dealing with the classes or interfaces within an API, or within most web applications, a really common terminology for that is gonna be the model. And that's what we're gonna use right now. So right now, we're going to create a new file within here and once again, this is going to be task.ts. And in here, we're gonna define what exactly a task looks like. Now, I'm gonna keep this fairly simplistic, it's not gonna be overly complicated. But you can obviously add to this, and experiment, and play with it as much as you want. So this is gonna be a class. So in order for me to use this class throughout my application, I'm going to have to export a class and I'm gonna call this class Task. Now Task is gonna have a couple of properties. And once again, we're going to start with basically a very simple shell of a class here and then you can add to it as much as you want. Now typically, when you're dealing with APIs and models for that matter, they're typically gonna be, the data's gonna be stored somewhere. That could be in a database, maybe in another web service somewhere. But the basic idea here is we wanna be able to get a single task if we want to, so we need to be able to uniquely identify this object, this model. And the way that we typically do that is via an ID. So this is going to have an ID and its type is going to be a number. Then after that, you can really start to add in as much as you want. I think typically, you're gonna find that a task is gonna have a name, and that's gonna be a string. And then also, a task is usually either complete or not complete, or finished, or whatever sort of terminology you wanna use. And in this case, we're gonna use is complete. And that is going to be a boolean. So now we have this task and we have a couple different properties on it. Once again, we have the ID, we have the name, and we have this concept of whether or not it's complete. So we'll go ahead and save that. So really, that's gonna be the starting point of our task class that we're gonna start to use an import all throughout the rest of our application. Now, as I mentioned before, a lot of times, this data is going to be stored somewhere. And I'm not gonna take you through the concepts or even a sample of storing anything in a database in this particular course, that's not really the focus here. So what I'm gonna show you in the next lesson is how we can start to create a couple different layers within this application by introducing concepts like services and repositories. And then I'm gonna show you some nifty little tricks later on in how we can do this for development purposes. And then once we're ready to change the way that we're storing these tasks, we can create a different implementation. And just continue right down our merry way, pointing to a database, pointing to a web service, and wherever you want. So let's go ahead and start to build out these concepts of repositories and services.