Hi everyone!
In previous video we already know how to create a controller, right?
And in this video, let me tell you the concept of solid principle and only the "s" of the solid principle
will be the single responsibility.
Now, before showing you what is the single responsibility, let me click on the app.controllers.
dot ts
Right here, you can see in the get hello right here, it use the something call "service" to get the hello.
Why it's use something like this.
Okay.
Instead of write directly hello world right here.
And it's also work.
You can see, okay, but why?
Why it use the service to get?
Hello.
Okay.
Well, with the concept of single responsibility.
The single responsibility means one class only responsible zero for one purpose.
Now, what does it mean?
Firstly, we have a three class.
Okay.
Actually, in here it's don't have a "repository", but I will discuss that.
Don't worry about that.
Firstly, we have a controller.
What is the purpose of the controller class right here?
This will return data or the view
Okay.
It handles incoming requests and respond data.
That's the whole purpose of controller.
And what is the service?
The service.
It will handle the business logic.
Right now we do not understand what is the business logic.
We will progress in this course.
Don't worry about that.
And repository
The repository is usually handle the database logic.
If you say okay, you handle the underlying resource logic like database or file system okay.
And with the controller we will usually call the service, and service
We will call the repository and repository.
Later it will return the data for the service and the service.
It will handle the business.
Okay.
And it will return the data back to the controller.
Okay.
And the controller, it will return to the end user.
And that's the concept of single responsibility.
Okay.
And let me write a little bit what is responsibility, right here in the controller.
I will create one more.
One more class test dot.
Service dot.
ts
Okay.
Right here we have a service.
Right.
In order to give the test service, firstly, I will create a test service first
And for example, I will have a get method.
I have a method.
Sorry.
Let me see add number, for example.
In the add number right here or I will say add and I will return the one plus one plus, for example
like this.
And I will export class and we sorry we don't have a bracket right here.
And the error right here.
Let's quickly fix that
Let's click on CLRF right here and change to LF.
I will fix that later.
We have a service.
Now let's see how we can use the service inside the controller.
Okay.
In order to use the service right here, we will init the service.
Let me say, for example, I will create a private method with that service.
And this is the test service right.
And I will init it with new test service.
Okay.
And right here instead of return the file, actually I will create one more controller will be calculation.
And right here we will call and I will return.
this.testService.add()
Now, if I go to the /calc, let's see what happened.
/calc
Okay, this will be the two, right?
This is the idea of the service and what's in the repository.
For example, I will create a test.repository.ts
I will export class.
Test repository
And the repository right here.
It will work with the database.
Right.
For example.
I will get number in database.
Okay.
We have method like this, and in here I will return.
For example I will have a data in database.
Okay.
For now I will fix the code like this.
And I will return the data in DB okay.
In later we will get the data in the real database.
But for now let's ignore that and let's assume this is the operation from database.
Okay.
We have a data in database.
We will return this right.
And let me fix that.
And inside the service, instead of using here, I will call the private test
repository
test repository
Okay.
And instead of using here, I will call the test this.testRepo.getNumber and I will plus
it with one.
Now if I refresh it will work, right?
And that's the concept of single responsibility in the controller.
We will call the service.
And in the service we will call the repository.
And we will return back the data for the service.
Okay.
And from service we will return back data for the controller.
And for that reason we will see the data right here.
Okay.
That's it for the video.
I will see you in the next one.