WEBVTT

1
00:00.590 --> 00:02.330
Hi everyone.

2
00:02.450 --> 00:10.460
Let's talk about the DTO, or we can say "data transfer object".

3
00:10.730 --> 00:14.180
So what is the data transfer object?

4
00:15.170 --> 00:18.590
It's just a dumb object.

5
00:18.620 --> 00:22.700
Okay, we can say it's just a "dumb object".

6
00:23.300 --> 00:30.620
The only purpose of the data transfer object a reduced amount of data.

7
00:31.850 --> 00:36.320
Okay, that's the first purpose will be reduce the data.

8
00:37.220 --> 00:45.590
And in the MVC architecture, we usually write the validation logic inside the data transfer object.

9
00:45.770 --> 00:49.190
Now first forget that.

10
00:49.250 --> 00:52.550
Let me show you what is the reduced data right now.

11
00:52.550 --> 01:03.660
Right now you can see, for example, I have a schema like this id username, email and password.

12
01:04.410 --> 01:11.160
Now lets think about how we can create the data like this.

13
01:11.820 --> 01:16.680
In order to create we don't need the the "id" right?

14
01:17.190 --> 01:19.920
The "id" is useless in this case.

15
01:20.130 --> 01:23.460
We don't need the ID when we.

16
01:25.590 --> 01:27.270
Create the data, right?

17
01:27.300 --> 01:32.250
For that reason, if we use the DTO, it will reduce the data.

18
01:32.730 --> 01:38.400
It means we will remove the "id" when we create the "dump object".

19
01:41.100 --> 01:43.620
Based on the original object.

20
01:43.710 --> 01:44.370
Okay.

21
01:44.790 --> 01:45.990
That the first one.

22
01:46.470 --> 01:48.630
And the second one.

23
01:51.120 --> 01:54.990
Now let's think about how the data can be response.

24
01:55.020 --> 02:02.640
For example, when we create the data right here and the server will response for you the data, we don't

25
02:02.640 --> 02:04.130
need the password, right?

26
02:05.330 --> 02:05.840
We don't need

27
02:05.840 --> 02:06.560
The password.

28
02:06.800 --> 02:11.450
And it's also "reduce" the data, right? Here and here.

29
02:11.480 --> 02:16.220
We will call that the "DTO" -> data transfer object.

30
02:17.390 --> 02:24.560
And we need two data transfer object for create and for response.

31
02:24.590 --> 02:25.340
Right.

32
02:26.150 --> 02:27.650
And that's the idea.

33
02:30.770 --> 02:32.540
Of DTO.

34
02:32.990 --> 02:45.470
And when clients send this amount of data into the server we usually send the "DTO" over server okay.

35
02:45.620 --> 02:48.710
And server it will return the "DTO" also.

36
02:49.970 --> 02:52.610
And we will validation inside the DTO.

37
02:52.610 --> 02:53.060
Also.

38
02:53.480 --> 02:56.810
Let me see how we can do that right now.

39
02:56.810 --> 03:04.890
If you scroll down inside the Mongo option right here, Let me see.

40
03:07.350 --> 03:09.000
That you know it's not right here.

41
03:10.140 --> 03:12.420
I will scroll up a little bit.

42
03:18.390 --> 03:19.140
Here.

43
03:26.970 --> 03:27.780
Right here.

44
03:27.780 --> 03:28.590
You can see it.

45
03:29.610 --> 03:30.420
DTO, right?

46
03:30.660 --> 03:33.660
This is the "Cat DTO"

47
03:33.780 --> 03:41.310
And let me show you how we can do that in here in the post.

48
03:41.340 --> 03:43.290
I will create the "dto".

49
03:45.300 --> 03:49.140
I think  "dtos" is the best way.

50
03:49.740 --> 03:50.160
Okay.

51
03:50.190 --> 03:51.750
And I will create.

52
03:53.670 --> 03:58.350
One "dto" will be "create-post"

53
03:59.940 --> 04:03.270
post.dto.ts

54
04:03.270 --> 04:03.690
Right.

55
04:03.990 --> 04:13.210
And this is just the "dumb object" or "dumb class" based on the original class.

56
04:13.240 --> 04:15.640
The original class will be the "post", right?

57
04:16.450 --> 04:22.600
And now let me export class "CreatePostDTO"

58
04:24.970 --> 04:33.970
And in here, what we have, we have a title will be string and the description is string.

59
04:34.720 --> 04:43.360
Well, the reason, the reason it is the same property because we don't have so much property in the

60
04:43.360 --> 04:43.930
post

61
04:44.050 --> 04:45.790
So this is not important.

62
04:45.820 --> 04:46.540
This case.

63
04:46.690 --> 04:51.070
But later when we work with user we will understand more about that.

64
04:53.890 --> 04:55.300
Okay that's the first one.

65
04:58.180 --> 05:07.770
And because we already use that, we will use inside the controller and service. In the controller right here

66
05:08.130 --> 05:16.740
The "requestBody", I will use the "CreatePostDTO" and the body right here, I will use

67
05:16.740 --> 05:19.770
CreatePostDTO

68
05:21.900 --> 05:26.010
CreatePostDTO okay, that the first look.

69
05:26.160 --> 05:26.940
About

70
05:27.210 --> 05:30.360
DTO it's just a data type right now

71
05:32.370 --> 05:36.000
And the application will work if you see.

72
05:45.660 --> 05:46.200
Okay.

73
05:46.470 --> 05:50.190
But for example if I remove the title let's see what happened.

74
05:53.430 --> 05:55.050
It will create this.

75
05:55.080 --> 05:55.620
Right?

76
05:55.620 --> 05:57.150
I don't want that I want it.

77
05:57.150 --> 05:58.050
Show the error.

78
05:58.080 --> 05:59.970
Show how I can do that.

79
06:00.180 --> 06:05.940
And in the next video I will show you how we can validation.

80
06:05.940 --> 06:07.500
I will see you in the next one.