In this video.
Let's finish up with the MVC design pattern.
You may be thinking that this one is okay because now on a server file, we have few lines of code because
now on a server file, we don't have the business logic.
And again, if you are developing, let's say, small application, this one is okay.
But let's say you are building enterprise application.
You need to use what is called the MVC design pattern completely.
So let's see how we are going to implement that.
Great.
So now inside the folder of the controller, it's time to work with it.
So now let's locate the roots here.
The responsibility of the root is to render a root, but not with the business logic.
So looking at this one, our business logic is our handler.
So what we are going to do is that we are going to extract this one from this function and then put
it inside the controller, and that is it, because it's here that we are going to implement our business
logic.
For example, if employee has been registered, then we are going to throw an error that, hey, you
have been registered and this is a business logic and this where the action is being taken.
So let's get going.
So inside the controllers, you are going to create a file for a specific model.
Because for this one, we have only one model and that is the employee.
So let's see how we are going to implement that.
So I would name the file as a name of my model, and that is the employee controller.
And then going to be dot g's.
So like I say, we are going to extract this one.
So again, we are moving this one from here again.
And now let me cut it and I'm back to the end controller.
I will pass it.
Now you can see how we are moving this function from the server file to route and now to the controller.
It means that this function is tired about moving around.
All right, guys, so let's continue to finish up this.
So here we are going to create a function and assign this function to it, and that is it.
And lastly, we are going to export it.
So here we go.
Let's have some space and let's make a function counts as create an employee.
And our ad controller tweet is equal to that.
Now, I've done this, my controller function.
So now if I save this one and I make a request to create, let's see the error.
You can see that there is no right for that because on the router we don't have the logic or the handler.
So we need to require the handler here.
So we need to export this one from this file.
And remember, this file is going to contain all the operation for the employees.
So we are going to export this one as an object and going to be key and value per.
And I would like to use the key as a value, so I will maintain one as the create employee controller.
Save it.
Now let's get back to the router and now let's require the controller.
And here you go, I have the auto import.
Lastly, I need to bring in here as a second element to this function call and that is it.
So let me save it and let me hit send.
And now everything works fine.
So if you want to give us a challenge, I advise you to refactor the rest of the route to use the MVC
design palette.
You can put this video and give it a shot.
Great.
Again, we are going to catch the handler from here.
And now back to the file.
Let me pass it here.
So the first one is let me add a comment.
Say, great.
And here is going to be as fetch.
All right.
Yeah, fetch or let me see all employees and cons going to be get and implore.
Gees meaning poorer.
I'm going to add as comptroller and I'll pace that and I'm done and our export this one as get employees
and here I need to extract or pull out that controller and that is gets employees so here I'll pass
in here as a second agreement as get employees.
Great.
Next step is going to be the update.
So our card is one from here.
And now here we go.
So I have a comment as update and cost update employee that is singular controller.
And then here I'll that and that is it.
So let me collapse this one and this and now let's work on this one.
So let me export the update.
And now here, let's require it here as.
I did and let me pass in here on the second argument as update next one going to be the delete.
So I'll cut this one from here.
And now let's get back to the employees.
And now let me have a comment.
You're going to be as I forgot is delete.
All right, guys.
So delete and I'm going to be as concerned.
Delete.
Employee.
Controller is equal to the handler and that is it.
Collapse it and let's export that one.
And here we go.
So here let's require the delete and that is it.
Next one is going to be fixed single.
So our Kurdistan from here and now back here is going to be details and going to be us get employee
for singular.
Controller is equal to the callback function and that is it.
And let me export this one from here as gate employee, which is this.
So here lets require the gate employee.
Great.
So now let's pass the delete here as delete and here as gets come and gets employee save it.
And now let's test all the endpoint guys.
First one is create a great or a it works update a it works delete a it works single a it works.
So guys here and the complete MVC design pattern and we're happy to finish out this section because
this one is really important as Node.js developer.
Great.
So now we don't have the actual logic, but the design pattern is complete.
What has left is to fill up the controller with a rare logic.
So the next video will go ahead and then register employee using our model.
And we are going to fetch all we are going to update, we are going to delete and you are going to find
single employee.
Let's get into that the next video.