WEBVTT

1
00:00.530 --> 00:02.480
Hi everyone!

2
00:02.510 --> 00:10.910
In previous video we already know how to create a controller, right?

3
00:11.300 --> 00:24.950
And in this video, let me tell you the concept of solid principle and only the "s" of the solid principle

4
00:24.980 --> 00:28.280
will be the single responsibility.

5
00:28.700 --> 00:37.550
Now, before showing you what is the single responsibility, let me click on the app.controllers.

6
00:37.700 --> 00:38.000
dot ts

7
00:38.000 --> 00:46.520
Right here, you can see in the get hello right here, it use the something call "service" to get the hello.

8
00:47.240 --> 00:49.580
Why it's use something like this.

9
00:51.890 --> 00:52.460
Okay.

10
00:52.760 --> 00:57.350
Instead of write directly hello world right here.

11
01:00.000 --> 01:01.350
And it's also work.

12
01:01.740 --> 01:05.220
You can see, okay, but why?

13
01:05.910 --> 01:09.210
Why it use the service to get?

14
01:09.240 --> 01:09.750
Hello.

15
01:11.580 --> 01:12.150
Okay.

16
01:12.600 --> 01:16.530
Well, with the concept of single responsibility.

17
01:18.330 --> 01:28.590
The single responsibility means one class only responsible zero for one purpose.

18
01:30.480 --> 01:33.690
Now, what does it mean?

19
01:33.960 --> 01:36.540
Firstly, we have a three class.

20
01:37.080 --> 01:37.710
Okay.

21
01:40.770 --> 01:47.520
Actually, in here it's don't have a "repository", but I will discuss that.

22
01:48.240 --> 01:49.170
Don't worry about that.

23
01:49.200 --> 01:50.760
Firstly, we have a controller.

24
01:51.480 --> 01:55.170
What is the purpose of the controller class right here?

25
01:56.280 --> 02:01.290
This will return data or the view

26
02:03.030 --> 02:03.450
Okay.

27
02:03.450 --> 02:06.690
It handles incoming requests and respond data.

28
02:07.230 --> 02:09.720
That's the whole purpose of controller.

29
02:11.430 --> 02:12.810
And what is the service?

30
02:13.320 --> 02:14.190
The service.

31
02:14.190 --> 02:16.920
It will handle the business logic.

32
02:18.180 --> 02:23.940
Right now we do not understand what is the business logic.

33
02:26.310 --> 02:28.500
We will progress in this course.

34
02:28.530 --> 02:29.550
Don't worry about that.

35
02:30.870 --> 02:33.270
And repository

36
02:33.930 --> 02:39.000
The repository is usually handle the database logic.

37
02:40.860 --> 02:52.680
If you say okay, you handle the underlying resource logic like database or file system okay.

38
02:54.060 --> 02:59.620
And with the controller we will usually call the service, and service

39
02:59.680 --> 03:06.100
We will call the repository and repository.

40
03:06.130 --> 03:11.050
Later it will return the data for the service and the service.

41
03:11.650 --> 03:13.750
It will handle the business.

42
03:14.350 --> 03:14.860
Okay.

43
03:14.890 --> 03:18.370
And it will return the data back to the controller.

44
03:18.820 --> 03:19.330
Okay.

45
03:19.360 --> 03:22.930
And the controller, it will return to the end user.

46
03:24.280 --> 03:28.000
And that's the concept of single responsibility.

47
03:29.620 --> 03:30.250
Okay.

48
03:30.550 --> 03:38.500
And let me write a little bit what is responsibility, right here in the controller.

49
03:38.530 --> 03:40.450
I will create one more.

50
03:43.150 --> 03:47.530
One more class test dot.

51
03:47.530 --> 03:48.760
Service dot.

52
03:48.940 --> 03:49.300
ts

53
03:50.170 --> 03:50.560
Okay.

54
03:50.650 --> 03:52.300
Right here we have a service.

55
03:52.330 --> 03:52.780
Right.

56
03:52.810 --> 04:00.730
In order to give the test service, firstly, I will create a test service first

57
04:04.000 --> 04:10.210
And for example, I will have a get method.

58
04:10.600 --> 04:12.160
I have a method.

59
04:12.820 --> 04:13.300
Sorry.

60
04:16.300 --> 04:19.810
Let me see add number, for example.

61
04:22.360 --> 04:31.870
In the add number right here or I will say add and I will return the one plus one plus, for example

62
04:31.900 --> 04:32.440
like this.

63
04:32.950 --> 04:42.340
And I will export class and we sorry we don't have a bracket right here.

64
04:44.140 --> 04:45.610
And the error right here.

65
04:45.640 --> 04:46.750
Let's quickly fix that

66
04:46.750 --> 04:53.050
Let's click on CLRF right here and change to LF.

67
04:54.910 --> 04:57.650
I will fix that later.

68
04:59.150 --> 05:00.740
We have a service.

69
05:00.890 --> 05:05.780
Now let's see how we can use the service inside the controller.

70
05:06.650 --> 05:07.040
Okay.

71
05:07.070 --> 05:14.900
In order to use the service right here, we will init the service.

72
05:15.470 --> 05:20.930
Let me say, for example, I will create a private method with that service.

73
05:21.230 --> 05:24.410
And this is the test service right.

74
05:24.770 --> 05:30.710
And I will init it with new test service.

75
05:34.250 --> 05:34.850
Okay.

76
05:35.390 --> 05:48.740
And right here instead of return the file, actually I will create one more controller will be calculation.

77
05:49.640 --> 05:55.310
And right here we will call and I will return.

78
05:55.970 --> 06:00.950
this.testService.add()

79
06:01.550 --> 06:05.420
Now, if I go to the /calc, let's see what happened.

80
06:07.520 --> 06:08.300
/calc

81
06:09.350 --> 06:13.010
Okay, this will be the two, right?

82
06:13.970 --> 06:21.380
This is the idea of the service and what's in the repository.

83
06:21.410 --> 06:28.550
For example, I will create a test.repository.ts

84
06:28.730 --> 06:31.280
I will export class.

85
06:32.000 --> 06:35.030
Test repository

86
06:37.310 --> 06:39.620
And the repository right here.

87
06:39.620 --> 06:42.620
It will work with the database.

88
06:42.650 --> 06:43.130
Right.

89
06:43.160 --> 06:44.090
For example.

90
06:46.610 --> 06:51.170
I will get number in database.

91
06:52.430 --> 06:52.760
Okay.

92
06:52.880 --> 06:56.730
We have method like this, and in here I will return.

93
06:56.730 --> 07:03.630
For example I will have a data in database.

94
07:04.380 --> 07:04.890
Okay.

95
07:04.920 --> 07:08.100
For now I will fix the code like this.

96
07:08.100 --> 07:11.880
And I will return the data in DB okay.

97
07:11.910 --> 07:16.980
In later we will get the data in the real database.

98
07:17.640 --> 07:24.930
But for now let's ignore that and let's assume this is the operation from database.

99
07:25.980 --> 07:26.340
Okay.

100
07:26.400 --> 07:28.620
We have a data in database.

101
07:28.650 --> 07:31.650
We will return this right.

102
07:33.000 --> 07:35.400
And let me fix that.

103
07:38.250 --> 07:51.780
And inside the service, instead of using here, I will call the private test

104
07:51.780 --> 07:53.010
repository

105
07:53.040 --> 07:54.660
test repository

106
07:55.530 --> 07:56.070
Okay.

107
07:57.510 --> 08:05.550
And instead of using here, I will call the test this.testRepo.getNumber and I will plus

108
08:05.550 --> 08:06.720
it with one.

109
08:08.910 --> 08:14.220
Now if I refresh it will work, right?

110
08:14.250 --> 08:20.070
And that's the concept of single responsibility in the controller.

111
08:20.130 --> 08:23.460
We will call the service.

112
08:23.910 --> 08:28.500
And in the service we will call the repository.

113
08:29.460 --> 08:34.170
And we will return back the data for the service.

114
08:34.890 --> 08:35.310
Okay.

115
08:35.340 --> 08:39.360
And from service we will return back data for the controller.

116
08:39.660 --> 08:45.300
And for that reason we will see the data right here.

117
08:46.590 --> 08:47.010
Okay.

118
08:48.240 --> 08:49.290
That's it for the video.

119
08:49.320 --> 08:51.630
I will see you in the next one.