WEBVTT

1
00:00.890 --> 00:02.420
Hi everyone.

2
00:02.510 --> 00:06.440
Let's continue with CRUD operations.

3
00:06.980 --> 00:10.970
And in this video will be the update action.

4
00:11.150 --> 00:11.630
Okay.

5
00:11.960 --> 00:17.240
Now let me remove DTO here to fix the warning.

6
00:18.290 --> 00:23.690
And now go to the post controller and I will create a method called @Put.

7
00:24.260 --> 00:24.800
Okay.

8
00:25.850 --> 00:27.650
The @Put will be for update.

9
00:29.270 --> 00:37.400
And because we update we need to know what is the ID of the post we want to update.

10
00:37.400 --> 00:37.940
Right.

11
00:40.250 --> 00:45.620
And we will update with @Param('id) id: string

12
00:47.150 --> 00:53.570
And now because we update we need the payload or the request body.

13
00:54.380 --> 00:57.710
And for that reason I will use the annotation @Body right here.

14
00:58.130 --> 00:59.330
And request body.

15
00:59.690 --> 01:02.840
But what is inside the request body right here.

16
01:03.500 --> 01:07.530
Now if you see in the post of the create.

17
01:07.770 --> 01:09.990
We have a createPostDTO right here.

18
01:09.990 --> 01:11.670
So for that reason, we need.

19
01:11.700 --> 01:13.140
We need the post.

20
01:13.620 --> 01:15.360
The UpdatePostDTO

21
01:15.390 --> 01:15.960
Right.

22
01:16.290 --> 01:23.550
So I will create a new file inside the DTO, update-post.dto.ts

23
01:24.840 --> 01:29.760
And actually right here we will export the class.

24
01:30.360 --> 01:35.430
Actually we use the same with the CreatePostDTO

25
01:35.580 --> 01:39.600
I will copy all of that because I'm very lazy.

26
01:42.360 --> 01:44.940
So I'll use the UpdatePostDTO

27
01:48.960 --> 01:49.320
Okay?

28
01:51.870 --> 01:54.240
And that what we want

29
01:54.600 --> 01:56.400
It will be the same, but.

30
01:56.400 --> 01:59.490
We need to separate it.

31
01:59.790 --> 02:00.120
Okay?

32
02:00.150 --> 02:01.470
Make sure we separate it.

33
02:02.010 --> 02:05.400
And right here I will use the UpdatePostDTO

34
02:07.560 --> 02:17.260
The reason we separate is because later if you want to add some validation only for the updates,

35
02:17.470 --> 02:22.150
we can easily add it right here and now.

36
02:22.150 --> 02:27.880
Let me go to the service and scroll down and call async update

37
02:29.350 --> 02:31.270
Give the ID string.

38
02:33.550 --> 02:37.120
And the request body.

39
02:37.540 --> 02:42.460
We will UpdatePostDTO like that.

40
02:43.570 --> 02:44.320
Very cool.

41
02:45.880 --> 02:47.560
Now how I can update.

42
02:47.950 --> 02:50.890
Actually, in order to update we can use the find.

43
02:53.260 --> 02:58.270
findByIdAndUpdate or findOneAndUpdate 

44
03:00.730 --> 03:02.800
And update it will be the same.

45
03:02.800 --> 03:11.080
And the second way we will use, we will update it by hand.

46
03:11.740 --> 03:14.110
So look up the get one and update.

47
03:14.140 --> 03:15.880
Let me show you the second way.

48
03:17.360 --> 03:17.720
Okay.

49
03:18.290 --> 03:19.760
I will find the post.

50
03:19.790 --> 03:22.460
= await this.

51
03:23.000 --> 03:23.750
getOne

52
03:24.050 --> 03:24.440
Okay.

53
03:24.470 --> 03:25.760
I will get the post.

54
03:25.970 --> 03:27.170
Inside the method.

55
03:27.170 --> 03:27.860
Right here.

56
03:27.860 --> 03:30.440
We already write it before, right?

57
03:30.740 --> 03:43.310
And for that reason, I can give you the post.title = requestBody.title and post.description

58
03:43.310 --> 03:46.580
= requestBody.description

59
03:47.750 --> 03:50.180
And I will return.

60
03:51.710 --> 03:53.630
I will return post.save()

61
03:54.020 --> 03:54.410
Okay.

62
03:55.580 --> 03:57.260
And that's it, that for the update

63
03:57.410 --> 03:59.630
And I will go to the controller right here.

64
03:59.660 --> 04:03.020
And I will return this.

65
04:05.810 --> 04:06.470
postService

66
04:06.950 --> 04:12.650
.update(id, requestBody), okay, that's it

67
04:13.970 --> 04:25.060
And let me show you how it works, in here I will duplicate and I will change to the PUT method, right?

68
04:26.050 --> 04:29.680
and will be update post.

69
04:32.440 --> 04:36.640
We have a ID so we will pass the ID right here.

70
04:37.210 --> 04:37.990
The title

71
04:40.000 --> 04:44.890
Will be "post 1 update"and description

72
04:46.930 --> 04:51.220
Will be "description 1"

73
04:54.760 --> 04:55.120
Update

74
04:55.630 --> 04:56.920
Okay, let me send.

75
05:00.250 --> 05:02.920
Uh, actually it's not found

76
05:08.770 --> 05:10.510
Let me go to the database.

77
05:10.630 --> 05:15.130
I will be here and I will paste it right here.

78
05:15.460 --> 05:16.510
I will send it again.

79
05:23.260 --> 05:24.760
Uh, why it not found?

80
05:46.130 --> 05:47.180
This I.

81
05:56.600 --> 05:57.290
Go.

82
05:59.660 --> 06:00.650
We use the @Put

83
06:01.640 --> 06:04.490
And the :id, oh sorry.

84
06:04.820 --> 06:06.530
We make a mistake right here.

85
06:07.340 --> 06:11.510
Okay, you typo correctly, will be :id

86
06:14.450 --> 06:15.770
Okay, let me send.

87
06:17.390 --> 06:17.960
Let me send.

88
06:19.700 --> 06:27.290
And now you can say this already updated for me the title and description.

89
06:27.590 --> 06:28.220
Right.

90
06:29.750 --> 06:33.030
And that's What we want.

91
06:33.960 --> 06:39.060
But for example, if I remove the description and I send the update. Let's see what happens.

92
06:39.630 --> 06:44.880
If I send, now it say the description should not be empty.

93
06:45.480 --> 06:48.780
The reason because I used is not empty right here.

94
06:48.780 --> 06:53.460
It means we will pass the description and the title.

95
06:54.570 --> 07:04.260
So how I can only update the title or only update the description?

96
07:04.710 --> 07:05.670
I want to do that.

97
07:05.700 --> 07:07.440
How I can do that right.

98
07:07.530 --> 07:10.920
And actually this is not the job of the @Put method.

99
07:10.950 --> 07:15.660
With the PUT method we will update all fields

100
07:16.710 --> 07:23.340
And in the next video I will show you how we can use the PATCH method.

101
07:23.370 --> 07:28.530
The PATCH methods will be update one fields.

102
07:31.740 --> 07:32.130
Okay.

103
07:33.300 --> 07:36.810
The job of the PATCH and I will show you in the next one.