In this video.
Let's finish up with the MVC design pattern.
In the previous video, we able to refactor the code on the server file to use express routing.
And this is a right path in the right direction.
So what has left is that we are going to create a controller function to hold our business logic.
And for the previous video, I gave this one to you as a challenge for the account and then the transaction
to refactor to use express routing.
And this is how I did it for the account creation.
You can see that we have this URL and then the account root.
So let's go to the routing and then inside the account.
This is a code for that as we did for the users, it is safe process.
The difference here is going to be the end point and for the account, the all end with account and
for dynamic you are l for example, get a particular account.
I added the params or the ID as that bear are still in case you talk, i will provide the source code
for this particular section.
So let's finish up with the MVC design pattern for what we have done.
We are on the right track because we are able to restructure the code on the server file and now the
responsibility of the server file is to render this route and our business logic now lies inside the
root.
So let's focus on the users root for the registration.
This is where we are going to code our business logic.
But one thing about express routing is that we don't need to have our business logic inside the root,
but instead our business logic must lies inside the controller.
So the responsibility of a root is to take in the controller function and then render it.
Because of that, we need to extract the callback function here that is the handler, and this one holds
our business logic.
So let's go ahead and create a control function for users.
Let's create three folders, one for each resource.
The first one going to be the users.
And after that we also have the account.
And lastly, we have the transactions.
There you go.
Let's start with the user.
So let's create one file for the users and I would name it as user's controller dot.
JS So now how are you going to create a controller function for this?
So back to the root.
So here we are going to catch the callback function for this routing and ask that.
So back to the controller file, let's face it.
So now this is our anonymous function as we did for the routing.
You can see how we are moving the logic from the file to the root and now to controller.
So let's see for this file we are going to have multiple controllers for all the HTTP endpoints.
So the first one is going to be the user registration.
So here we go.
We are going to create a normal function as const and then register user controller is equal to now
is equal to our callback function and that is it.
This is a valid controller function.
Great.
So next thing is that we need to export this one as module to export.
And for this we are going to export multiple functions.
So our export it as an object.
So here as register controller function.
So to be able to use it now, go to the users route and now I need to require the register user controller
and the auto import is working fine so our cart is one and now place it inside the route and that we
are not going to call it as this, but instead we want the express routing to do that for us.
So if I save it and now let's go ahead and make a request to the user.
If I hit send, you can see that everything remains the same.
All the routing is working fine.
So right now we have a central place for our business logic and that is the user controller.
So let's go ahead and then bring in the other logic for the log in.
So let's have a beautiful comment here and call this one as register.
So the next one is going to be as the log in so we can even copy this one.
And now here let's paste eight and change this one to user log in controller and then you are going
to be as log in route.
So let's export this one also as user log in.
So let's require this one inside the easiest route.
So here we have one more controller and that is the user log in.
So now I'm going to remove this callback here and replace with the user log in.
So let's save it and let's check the log in also.
And this is a log in.
When I hit send, you can see that it is working fine.
So let's finish up with a refactoring to use the MVC design pattern.
So the next one is going to be fetch or users.
Let's check it out.
What?
We have the profile.
Yes.
So let's go ahead and then copy this one and then paste it here and change this one to and for this
one as proof file.
And here is going to be as user profile controller and here is going to be as profile controller.
The next thing is I need to export the user controller.
So let's get to the users root and now let's try that one and then let's remove everything from here
and let's replace with the user file controller and that is it.
The next step is going to be the delete user.
So here too, we are going to bring in well, I need to export this one or so as user profile.
The next one is going to be the update.
So let's see, next is the delete.
Sorry guys.
So here is going to be as delete and here as delete.
User controller and here as delete root.
So let's export this one or so delete user controller.
The next one going to be the update.
So let's remove this one and as delete user controller and the auto import is working fine.
And the next one going to be the update.
So let's come here and let's copy this one.
Sorry, e here.
All right.
So here going to be as the update and let's collapse.
This one is going to be as update user controller.
There you go.
So here we're going to be as update and let's export it as update user controller.
So let's require that one inside the users root and I'll start and let's remove this one from here.
And now we have update controller.
You can see that now we have less code for our routing and now our business logic lies inside this controller
function.
So let's go ahead and test all the endpoints, make sure that everything is working fine.
So back to the root here for no is for users here this one register fine log in is also fine for file
is supposed to find delete it specify and update is also fine.
Now it's a time to give a save a challenge to refactor the account and then the transaction to use MVC
design pattern.
Then this video, I'll provide you my source code.