1
00:00:07,330 --> 00:00:08,470
In this video.

2
00:00:08,470 --> 00:00:12,460
Let's finish up with the MVC design pattern.

3
00:00:12,490 --> 00:00:19,930
In the previous video, we able to refactor the code on the server file to use express routing.

4
00:00:19,930 --> 00:00:23,680
And this is a right path in the right direction.

5
00:00:23,860 --> 00:00:30,820
So what has left is that we are going to create a controller function to hold our business logic.

6
00:00:31,000 --> 00:00:38,470
And for the previous video, I gave this one to you as a challenge for the account and then the transaction

7
00:00:38,470 --> 00:00:41,650
to refactor to use express routing.

8
00:00:46,770 --> 00:00:50,810
And this is how I did it for the account creation.

9
00:00:50,820 --> 00:00:56,100
You can see that we have this URL and then the account root.

10
00:00:56,100 --> 00:01:00,060
So let's go to the routing and then inside the account.

11
00:01:00,060 --> 00:01:05,700
This is a code for that as we did for the users, it is safe process.

12
00:01:05,910 --> 00:01:13,200
The difference here is going to be the end point and for the account, the all end with account and

13
00:01:13,200 --> 00:01:17,460
for dynamic you are l for example, get a particular account.

14
00:01:17,460 --> 00:01:26,100
I added the params or the ID as that bear are still in case you talk, i will provide the source code

15
00:01:26,100 --> 00:01:28,040
for this particular section.

16
00:01:28,050 --> 00:01:33,690
So let's finish up with the MVC design pattern for what we have done.

17
00:01:33,690 --> 00:01:40,020
We are on the right track because we are able to restructure the code on the server file and now the

18
00:01:40,020 --> 00:01:47,910
responsibility of the server file is to render this route and our business logic now lies inside the

19
00:01:47,910 --> 00:01:48,570
root.

20
00:01:48,570 --> 00:01:53,610
So let's focus on the users root for the registration.

21
00:01:53,610 --> 00:01:57,180
This is where we are going to code our business logic.

22
00:01:57,360 --> 00:02:04,320
But one thing about express routing is that we don't need to have our business logic inside the root,

23
00:02:04,320 --> 00:02:09,210
but instead our business logic must lies inside the controller.

24
00:02:09,330 --> 00:02:16,170
So the responsibility of a root is to take in the controller function and then render it.

25
00:02:16,170 --> 00:02:23,160
Because of that, we need to extract the callback function here that is the handler, and this one holds

26
00:02:23,160 --> 00:02:24,630
our business logic.

27
00:02:24,930 --> 00:02:29,220
So let's go ahead and create a control function for users.

28
00:02:29,400 --> 00:02:33,000
Let's create three folders, one for each resource.

29
00:02:33,000 --> 00:02:35,600
The first one going to be the users.

30
00:02:35,850 --> 00:02:40,560
And after that we also have the account.

31
00:02:41,250 --> 00:02:45,090
And lastly, we have the transactions.

32
00:02:48,300 --> 00:02:49,230
There you go.

33
00:02:49,290 --> 00:02:50,780
Let's start with the user.

34
00:02:50,790 --> 00:02:58,020
So let's create one file for the users and I would name it as user's controller dot.

35
00:02:58,020 --> 00:03:03,390
JS So now how are you going to create a controller function for this?

36
00:03:03,390 --> 00:03:05,610
So back to the root.

37
00:03:05,610 --> 00:03:13,380
So here we are going to catch the callback function for this routing and ask that.

38
00:03:13,380 --> 00:03:16,530
So back to the controller file, let's face it.

39
00:03:16,530 --> 00:03:20,970
So now this is our anonymous function as we did for the routing.

40
00:03:20,970 --> 00:03:28,800
You can see how we are moving the logic from the file to the root and now to controller.

41
00:03:29,520 --> 00:03:36,840
So let's see for this file we are going to have multiple controllers for all the HTTP endpoints.

42
00:03:36,840 --> 00:03:41,190
So the first one is going to be the user registration.

43
00:03:41,280 --> 00:03:42,780
So here we go.

44
00:03:42,810 --> 00:03:54,840
We are going to create a normal function as const and then register user controller is equal to now

45
00:03:54,840 --> 00:03:59,010
is equal to our callback function and that is it.

46
00:03:59,010 --> 00:04:01,560
This is a valid controller function.

47
00:04:01,590 --> 00:04:01,850
Great.

48
00:04:01,890 --> 00:04:09,900
So next thing is that we need to export this one as module to export.

49
00:04:09,900 --> 00:04:13,260
And for this we are going to export multiple functions.

50
00:04:13,260 --> 00:04:16,019
So our export it as an object.

51
00:04:16,019 --> 00:04:19,769
So here as register controller function.

52
00:04:19,980 --> 00:04:29,130
So to be able to use it now, go to the users route and now I need to require the register user controller

53
00:04:29,130 --> 00:04:37,830
and the auto import is working fine so our cart is one and now place it inside the route and that we

54
00:04:37,830 --> 00:04:44,520
are not going to call it as this, but instead we want the express routing to do that for us.

55
00:04:44,520 --> 00:04:49,440
So if I save it and now let's go ahead and make a request to the user.

56
00:04:49,440 --> 00:04:53,730
If I hit send, you can see that everything remains the same.

57
00:04:53,730 --> 00:04:56,820
All the routing is working fine.

58
00:04:56,820 --> 00:05:04,260
So right now we have a central place for our business logic and that is the user controller.

59
00:05:04,650 --> 00:05:09,870
So let's go ahead and then bring in the other logic for the log in.

60
00:05:09,870 --> 00:05:16,110
So let's have a beautiful comment here and call this one as register.

61
00:05:17,250 --> 00:05:24,690
So the next one is going to be as the log in so we can even copy this one.

62
00:05:24,840 --> 00:05:34,470
And now here let's paste eight and change this one to user log in controller and then you are going

63
00:05:34,470 --> 00:05:36,930
to be as log in route.

64
00:05:36,930 --> 00:05:41,580
So let's export this one also as user log in.

65
00:05:41,700 --> 00:05:46,080
So let's require this one inside the easiest route.

66
00:05:46,080 --> 00:05:51,810
So here we have one more controller and that is the user log in.

67
00:05:51,810 --> 00:05:58,440
So now I'm going to remove this callback here and replace with the user log in.

68
00:05:58,890 --> 00:06:02,310
So let's save it and let's check the log in also.

69
00:06:02,310 --> 00:06:03,660
And this is a log in.

70
00:06:03,660 --> 00:06:07,410
When I hit send, you can see that it is working fine.

71
00:06:07,920 --> 00:06:12,390
So let's finish up with a refactoring to use the MVC design pattern.

72
00:06:12,390 --> 00:06:15,540
So the next one is going to be fetch or users.

73
00:06:15,540 --> 00:06:16,260
Let's check it out.

74
00:06:16,260 --> 00:06:16,530
What?

75
00:06:16,530 --> 00:06:18,660
We have the profile.

76
00:06:18,660 --> 00:06:19,200
Yes.

77
00:06:19,200 --> 00:06:29,760
So let's go ahead and then copy this one and then paste it here and change this one to and for this

78
00:06:29,760 --> 00:06:32,100
one as proof file.

79
00:06:32,280 --> 00:06:43,140
And here is going to be as user profile controller and here is going to be as profile controller.

80
00:06:43,560 --> 00:06:47,970
The next thing is I need to export the user controller.

81
00:06:47,970 --> 00:06:56,430
So let's get to the users root and now let's try that one and then let's remove everything from here

82
00:06:56,430 --> 00:07:02,610
and let's replace with the user file controller and that is it.

83
00:07:02,880 --> 00:07:06,510
The next step is going to be the delete user.

84
00:07:06,540 --> 00:07:13,350
So here too, we are going to bring in well, I need to export this one or so as user profile.

85
00:07:14,070 --> 00:07:17,520
The next one is going to be the update.

86
00:07:17,520 --> 00:07:21,000
So let's see, next is the delete.

87
00:07:21,000 --> 00:07:22,080
Sorry guys.

88
00:07:22,080 --> 00:07:29,580
So here is going to be as delete and here as delete.

89
00:07:31,960 --> 00:07:37,240
User controller and here as delete root.

90
00:07:37,510 --> 00:07:42,670
So let's export this one or so delete user controller.

91
00:07:43,030 --> 00:07:46,210
The next one going to be the update.

92
00:07:46,210 --> 00:07:56,830
So let's remove this one and as delete user controller and the auto import is working fine.

93
00:07:57,070 --> 00:07:59,620
And the next one going to be the update.

94
00:07:59,620 --> 00:08:03,520
So let's come here and let's copy this one.

95
00:08:03,520 --> 00:08:05,500
Sorry, e here.

96
00:08:05,500 --> 00:08:05,830
All right.

97
00:08:05,830 --> 00:08:10,900
So here going to be as the update and let's collapse.

98
00:08:10,900 --> 00:08:17,860
This one is going to be as update user controller.

99
00:08:18,280 --> 00:08:18,850
There you go.

100
00:08:18,850 --> 00:08:27,130
So here we're going to be as update and let's export it as update user controller.

101
00:08:27,250 --> 00:08:35,620
So let's require that one inside the users root and I'll start and let's remove this one from here.

102
00:08:35,890 --> 00:08:38,740
And now we have update controller.

103
00:08:38,860 --> 00:08:46,300
You can see that now we have less code for our routing and now our business logic lies inside this controller

104
00:08:46,300 --> 00:08:46,990
function.

105
00:08:47,140 --> 00:08:52,630
So let's go ahead and test all the endpoints, make sure that everything is working fine.

106
00:08:52,630 --> 00:09:02,500
So back to the root here for no is for users here this one register fine log in is also fine for file

107
00:09:02,500 --> 00:09:08,950
is supposed to find delete it specify and update is also fine.

108
00:09:09,310 --> 00:09:18,700
Now it's a time to give a save a challenge to refactor the account and then the transaction to use MVC

109
00:09:18,700 --> 00:09:20,050
design pattern.

110
00:09:20,050 --> 00:09:22,990
Then this video, I'll provide you my source code.

