So let's implement that MVC pattern here.
Since we already got the views and the controllers
are dealing with connecting models and views,
we need to start with the models,
otherwise there is nothing to connect.
And I will start with the blog related database logic
so to say.
Now I will add a new folder, the models folder,
which is a common name for storing your models.
And in there, I'll add a post.js file.
Now, why did I name it post?
Because this model, which we're building here
should be dealing with storing posts.
Now I named it post instead of posts,
because a post model should be a blueprint for a single post
though we will of course also support operations
that involve multiple posts like fetching multiple posts,
but we will also support deleting a single post
or updating a single post,
and therefore I chose to singular forum here.
Now these are all just conventions,
not a technical requirements,
but this is why I did name this file post.js.
And in this file, I'm now going to create a class.
Now you might remember classes,
we haven't used them thus far,
but we learned about them a while ago.
Classes are a JavaScript feature,
which you can use in NodeJS and browser site JavaScript,
which you can use to define blueprints for objects.
And whilst you don't necessarily need to use classes
to implement models, they are very useful for that.
Because if we think about it,
we now wanna define a blueprint for a post
that describes the data
that will eventually be stored in a post.
And that describes the functions, the so-called methods then
if they are tied to an object
that should be usable on a post.
Methods like saving it to the database or updating it.
That's the idea here.
And therefore, I'll add a post-class here.
And the convention is to give your class a name
that starts with an upper case character.
Now you might wanna go through that introduction
to classes again, if this is brand new to you,
because there you learn that
you then can add a special constructor function or method
as it's, then called to this class like this
and that function contains code,
that will be executed whenever you create a new instance
based on that class.
So a concrete object based on this class.
And here in this constructor,
I wanna get the title of the post
that I wanna create as a parameter value
and that the content of the post that I might wanna create,
and then in case I'm not creating a new post,
but updating an existing one, I also want to get the ID.
I'll add it as a last parameter value here
so that we could actually also omit it
and just pass a title and content
because when we create a post, we don't have an ID yet.
So I wanna be able to also execute this function
with only two parameter values instead of three.
And this is how we could implement this with JavaScript,
id then would simply be undefined,
which means it has no value.
Now then early in the course,
when I introduced you to classes,
we also learned about that this keyword,
which allows us to refer to that
to be created concrete object
that will be based on this blueprint.
And we can then, for example, add properties to this object,
and we could add a title property,
which we set equal to the title
we received as a parameter value.
And we can do the same with the content
and set this equal, like this.
And also with the id, which we said equal to id,
though, this of course, may be undefined
if no id was provided, but that's okay.
So now we could create post objects with that blueprint.
Now, the idea with models
is that we can then use these objects
to also trigger certain operations.
For example, we could add a insert method,
and this is how we do add methods on classes as we learned,
or a save method, the name is up to you
to insert this post into the database.
And here I'll use a save method
and there in the save method,
if our goal is to store that post in a database,
I now wanna execute that MongoDB logic
for inserting a new post in that save method.
So we can go to block.js to this routes file
and there, search for the route
where we do insert a new post, and that that would be here
and here we can grab this logic here in the end,
cut that and move that into this save method.
Now here for the new post object which I create here,
I will set title: this.title.
So to the title I stored in this post object
based on this class.
And it can be confusing
that we have another new post object here.
We don't need that, I just need some object,
which I use as a document for inserting,
and I don't wanna use the entire post object here
because we could be storing more data in there
which I don't wanna insert into the database,
even though we're not doing that here,
but I'll still create a new separate helper object,
which is the actual object that will end up in the database,
but to make this less confusing,
I'll not store it in a separate new constant,
instead, I'll pass it here to create it
just in time so to say.
And the title for the to be created document
has the title value that's stored in this object
based on this post class.
And we get access to that with this keyword
and the content is accessed in the same way
with this content.
Now here, we need to make sure
that we get access to our database here,
and therefore of course here in this post.js file,
I now also want to import db
by requiring it from going up one level with this syntax
and then diving into the data folder
and then to the database.js file,
because we need access to this db object
which does give us access to the database
which we then need to access collections and so on.
And then last but not least, we wanna export this.
So here at the bottom, I'll export this post class like this
so that we can use it in our files
so that the blueprint will be defined in this file,
but we can use it in other files as well.
So then I'll go to blog.js
and here I now will import my Post class like this
going up one level diving into models post,
and I name it post with an uppercase P here as well
to make it clear that I am importing a class here.
And then I wanna use this when we insert a new post.
So here in this place, in this posts route,
yes, these are a lot of posts, I'm sorry.
And we can create a new post with the new keyword
as we learned before, when I introduced you to classes.
With new posts like this, this gives us the post object.
You can name this however you want, new post or just post,
whatever you want.
And now we need to pass two parameter values
to this post class here, which we are creating
and that would be the parameter values
that are expected by the constructor function,
because that's the function that will be called
whenever you instantiate a class as it's called
with the new keyword.
Now, actually we expect three parameter values here,
but as I explained before,
I deliberately put ID at the end here
so that we can omit it when we create a post
and it will then simply be undefined.
And if we create a post for the first time
to store it as a brand new post in the database,
we of course have no id yet.
So therefore, I'll just forward my entered title
and my entered content here,
and that should create that post object.
And now to save it into the database,
we can use post and then dot
and then call that save method on that post
which be instantiated.
And that will then trigger this save method,
which will do the actual database saving.
Now, since I'm using await in there, by the way,
we should add async in front of this method,
otherwise await is not supported here.
And of course we could also get the result here
and return that in that method
if we would be interested in the result
in the place where we call save.
So in this case in blog.js.
I am not interested, but we can still return it,
it won't hurt.
Now, last but not least,
we also wanna await this post.save method call here
to ensure that we only redirect after the post was saved
so that we don't accidentally load the admin page
before the post ended up in a database.
In that case, the admin page would work,
but we might not fetch that latest post there yet
because it wasn't saved yet.
So therefore awaiting post.save here
is something we should do.
And we can await here because the safe method is async
and all async functions and methods
return promises by default automatically.
So therefore we can await posts save
because it automatically returns a promise.
If we now save all our files
and I reload this admin page,
I can create "A new post-created with a model."
And if I click Save, that worked and here is my new post.
And with that, we created our first model
with its first method.
Of course we're not done
because there's more we can do with posts.
And if you're feeling fancy,
try implementing the update and delete methods on your own
and see how you could wire all these things up.
We're going to do it together in the next lecture.