WEBVTT

1
00:01.460 --> 00:03.410
Hi everyone.

2
00:04.550 --> 00:10.640
In this video I will show you how to do 
PATCH operation.

3
00:11.000 --> 00:19.520
Okay, now I will create one more method with PATCH.

4
00:21.620 --> 00:23.840
And I will use updateOne()

5
00:24.410 --> 00:28.790
Okay will be the @Param() will be the same

6
00:29.750 --> 00:34.790
id: string, @Body() requestBody

7
00:34.820 --> 00:35.270
Body.

8
00:37.760 --> 00:41.150
Now what is the request body.

9
00:41.150 --> 00:43.310
Right now I cannot use the

10
00:43.970 --> 00:46.520
UpdatePostDTO because

11
00:49.250 --> 00:52.880
Because it is not empty like this, right?

12
00:52.910 --> 00:55.130
I cannot remove one of that.

13
00:57.890 --> 01:00.290
I want it have a validation.

14
01:00.290 --> 01:08.470
But I want this only work for the title or the description.

15
01:09.100 --> 01:09.430
Okay.

16
01:09.460 --> 01:10.330
How we can do that.

17
01:10.360 --> 01:14.500
So I will create another DTO

18
01:17.080 --> 01:17.530
Post

19
01:18.400 --> 01:19.240
Patch

20
01:20.020 --> 01:20.410
.dto

21
01:20.920 --> 01:21.130
.ts

22
01:21.310 --> 01:21.670
.ts

23
01:23.020 --> 01:24.070
And now?

24
01:26.380 --> 01:29.770
If I create a class called Update.

25
01:29.950 --> 01:30.700
Post

26
01:33.460 --> 01:34.240
Patch

27
01:34.510 --> 01:35.110
DTO

28
01:36.640 --> 01:37.630
Now how?

29
01:37.630 --> 01:39.460
I can write a validation.

30
01:40.120 --> 01:45.070
I cannot copy anything here, but it's inside here, right?

31
01:46.510 --> 01:51.460
So for that reason, I will use a special syntax in the typescript.

32
01:52.120 --> 01:53.140
I will extends.

33
01:54.310 --> 01:54.670
Okay.

34
01:54.700 --> 01:56.230
I will extend the class.

35
01:57.850 --> 02:00.400
I will inherit from the Partial.

36
02:01.420 --> 02:02.380
Partial

37
02:16.070 --> 02:18.680
Oh, I don't know why.

38
02:50.810 --> 02:50.840
I.

39
02:59.270 --> 02:59.810
Let

40
03:00.320 --> 03:02.000
Let me find a package.

41
03:04.760 --> 03:05.630
nestjs

42
03:08.420 --> 03:11.180
Partial type

43
03:14.030 --> 03:14.840
Right here.

44
03:16.360 --> 03:23.230
We have a something called mapped type, inside the open api library here.

45
03:26.320 --> 03:27.580
Now you can see.

46
03:37.090 --> 03:44.170
You can see, it use the special syntax call PartialType and it'll use something like this

47
03:45.130 --> 03:45.700
Right.

48
03:48.280 --> 03:48.790
But.

49
03:51.790 --> 03:52.390
But why.

50
03:52.390 --> 03:55.690
I cannot use the PartialType right here.

51
03:59.830 --> 04:03.100
PartialType import from @nestjs/swagger

52
04:03.790 --> 04:06.370
I think I must install this package.

53
04:06.700 --> 04:07.840
Okay, let me install.

54
04:25.460 --> 04:32.480
Let me use PartialType from nestjs/swagger , and I will inherit it from the CreatePostDTO

55
04:34.700 --> 04:36.110
And technical

56
04:36.440 --> 04:38.750
We can easily,

57
04:38.750 --> 04:42.860
use the UpdatePostDTO, but

58
04:42.860 --> 04:48.380
I will inherit from CreatePostDTO because it later we.

59
04:48.860 --> 04:52.130
We may or may not have UpdatePostDTO

60
04:52.130 --> 04:53.480
anymore

61
04:53.510 --> 04:54.050
Right.

62
04:54.050 --> 04:57.710
So for that reason, I will inherit it from CreatePost

63
04:57.740 --> 04:58.220
DTO

64
04:58.370 --> 05:05.570
And what happened right now with, with the update right here.

65
05:08.900 --> 05:13.310
It will extend the Partial of CreatePost

66
05:13.310 --> 05:13.760
DTO

67
05:13.790 --> 05:23.030
It means it will ignore one of that, or it will ignore the title.

68
05:23.360 --> 05:28.180
Okay, that's the case of the PartialType

69
05:28.810 --> 05:29.290
Okay.

70
05:29.320 --> 05:29.830
That's it.

71
05:29.830 --> 05:31.180
Just enough for you.

72
05:31.240 --> 05:33.760
And let me show you how we can practice.

73
05:34.480 --> 05:35.380
Let me return.

74
05:36.160 --> 05:41.050
Actually, I will create one more method call Update

75
05:41.620 --> 05:43.210
Let me copy all of that

76
05:45.310 --> 05:47.980
And I will change the name to updateOne

77
05:49.210 --> 05:50.980
And we have a id and requestBody

78
05:50.980 --> 05:56.140
will be UpdatePostPatchDTO

79
05:56.740 --> 06:00.040
And now you can see, it's work, right?

80
06:00.160 --> 06:04.360
Because it's accept one or more property.

81
06:07.960 --> 06:12.880
And that for the job, but in this way, it'll not work

82
06:12.910 --> 06:20.350
And I can use a special syntax like Object.assign(post, requestBody)

83
06:21.310 --> 06:22.900
Now let's see it work or not

84
06:25.030 --> 06:30.430
I will return this.postService.updateOne

85
06:30.820 --> 06:32.700
and id and requestBody

86
06:35.100 --> 06:39.030
Right here will be 

87
06:39.060 --> 06:40.110
UpdatePostPatchDTO

88
06:45.570 --> 06:48.450
And let me, let me update.

89
06:48.570 --> 06:56.520
Let me duplicate and change to PATCH right here.

90
06:57.000 --> 06:58.680
I will only update the title.

91
06:58.740 --> 07:04.110
Let's see, if I send, now you can see, it update, right?

92
07:04.770 --> 07:05.460
Very cool.

93
07:06.420 --> 07:12.750
If I specify the description it also work because it's aceept 1 or 2.

94
07:13.500 --> 07:15.480
If I send, okay it's work, right.

95
07:15.510 --> 07:26.760
If I remove the title and I will use UpdatePost, then I send, now you can see it's worked very well

96
07:27.210 --> 07:41.200
and that's for the job of the PartialType, the PartialType, it just ignore one or more property inside

97
07:41.200 --> 07:43.750
the original DTO.

98
07:44.530 --> 07:48.250
Okay, in this case we will CreatePostDTO

99
07:49.090 --> 07:57.250
So that is, that how we can use the PartialType

100
07:57.580 --> 08:01.210
And I will not explain what is inside the Object.assign

101
08:01.240 --> 08:03.130
You can read more about that.

102
08:06.400 --> 08:11.140
In here because it is just the JavaScript knowledge.

103
08:11.620 --> 08:13.480
It just merge the type

104
08:13.930 --> 08:17.680
Okay, we just merge from here into the target type.

105
08:18.250 --> 08:28.180
Okay, now you can see, for example I have a target with a and B okay I have another object with b

106
08:28.180 --> 08:28.690
and c.

107
08:28.930 --> 08:37.540
So if I use the object.assign, it will merge it inside the final object like this.

108
08:38.560 --> 08:43.030
So that's just the object.assign. I will see you the next one.