Hi everyone!
I already talked about the module in previous video, and in this video let me show you how to create
the modules.
Okay, now the application right now is very messy.
I think I will remove the all of the test right here, tesst controller test repository and test service
okay.
Remove all the that and inside the module let me remove here okay.
Remove here.
Sorry.
Here here and here.
Now the application right now, in order to create the some module we usually create a new folder okay.
For example I will create the blog post application.
So I will create a post folder okay.
Now what's inside the post
I will create a structure like application controller, app module and app service.
Okay, so I will create the app.
Sorry.
The post.controller
.ts
post.
service
.ts
And the very important a post.module.ts
Okay, this is the most important.
Now let me see how I can create the controller.
Firstly, I will create a controller in the controller from Nestjs/common and
The class will be post controller.
And create a method called get all.
Okay, we will get all the posts and I will return.
Well, what's a return for now?
I will return empty string and I will return inside the service later.
But let me fix this error is a little bit annoying.
Now let let me show you how I can find the solution.
Actually, this is the error, right?
I think I will copy this and paste it into the stackoverflow.
I click something topic
And if I scroll down into here, we can set the end of line will be auto.
Okay.
Let me find the eslint right here.
Inside the rule
Actually, is it prettier, I think.
Let me find some solution.
Eslint
Inside a rule
I think inside here
I will copy.
Let me scroll up into the top one.
No, that's what we want.
Here.
And let me paste it, work
Or not.
And actually, it's work, right?
So we will be in here.
And this is a rule of estlint.js
And let's go to the application.
In the get all, I will create a service.
Okay I will create a service post service
Sorry.
Class.
Post Service
And in here I will create a get all also
Okay.
The same name is still acceptable.
And in here I will return.
I will return something like this.
And I think I will ignore the repository right now.
I will create the data.
I will mock the data.
Data with object ID:1, title plus one description.
Will be post 1 description
Something like this.
Let me copy & paste 2 times
And here we will be three and I will return directly right here.
Okay.
Later we will fetch by database
Okay.
And inside here we will use the dependency injection.
Right.
So for that reason firstly we will mark it in the injectable
Make sure we call that
And secondly will be inside the post module.
And actually I don't know I don't remember the code inside the module.
Okay.
Never in my life I remember the code.
So I will copy all of that inside the app module and paste it inside the post module right here.
And I we will remove everything and rename it into the post module.
Okay.
That is.
And now let.
Let's pause the video right here and try to make the post work.
Okay.
If you have a little bit tricky, I will show you.
Don't worry, let pause the video.
Okay.
How about your solution?
This work or not?
Anyway, let me show you.
Firstly, in the controller I will mark.
That will be the post controller.
And inside the provider here we will register for the injectable class.
We will post service
And inside here I will use a get decorator.
For example I use the /posts
And in here I will create a constructor.
private post service
And I will return this.postService
get all
Also, we must have a curly brackets.
Now let me run.
For example, I will run the post right here.
Unfortunately, it does not work.
Why it's not work?
Because the nestjs right now doesn't have any idea about the post
Okay.
It doesn't have any idea about post.
It's don't know what is the module of the post, because in here you can see it's not correct.
For example, this is the post module.
It must somehow connect in with the application module.
Right.
But right now this independently.
It's just a post
Okay?
It's just a post.
And it doesn't have the connection between the application and the post.
So for that reason, it's not work right now, how we can do that?
And that's the purpose of the export statement
Now let me show you.
I must export the entire model.
The entire module will be the post module.
Okay.
Export that
And inside inside the app module, inside the import of app module, we will import the post module.
Now let's see, it's work or not, refresh
Now you can see it's already returned for me the data.
Right.
And that's it, that's how to create a module.
And we already understand what is import and export. Let's me summarize.
Firstly
Let me show you.
Firstly
I will create a post module.
Right?
But the application right now is.
Don't have any idea about what is the post module.
We need some way to create a connection between the post and the application module.
Right.
And for that reason, in the post module, we must "export".
We must export "itself" okay.
We must export itself.
And inside the application module we will import.
Okay.
We must import the post module.
And it will create a connection between the post and the application module.
Okay.
That's the everything about module.
I hope you understand this video.
I will see you in the next one.