WEBVTT

1
00:00.950 --> 00:02.660
Hi everyone!

2
00:03.170 --> 00:12.260
In previous video I already showed you how to create transformDTO for response, right?

3
00:13.040 --> 00:17.450
for DTO Response and make sure we use the exclude.

4
00:17.480 --> 00:19.970
And inside the response we use the @Expose

5
00:20.000 --> 00:22.640
Okay all of them

6
00:23.900 --> 00:24.680
This will work.

7
00:25.820 --> 00:29.480
And now let's go to the next application.

8
00:32.750 --> 00:37.010
Let me create next endpoint will be getOne

9
00:37.160 --> 00:39.110
I already have a create and getAll.

10
00:39.140 --> 00:40.070
Right?

11
00:40.070 --> 00:43.220
So in this video I will create a get one method.

12
00:43.730 --> 00:49.760
And I will do the get method with the :id, okay

13
00:50.900 --> 00:52.310
And with the get one.

14
00:54.830 --> 00:57.380
And I will use the decoration.

15
00:57.470 --> 01:00.800
Decorator will be the @Param and pass the id

16
01:01.190 --> 01:04.410
Make sure the "id" right here, and "id" right here will be the same

17
01:05.520 --> 01:12.960
And it will be string because this is the id of the MongoDB, right?

18
01:14.160 --> 01:18.720
But if this is the number, make sure we use

19
01:18.720 --> 01:19.320
The Parse

20
01:22.170 --> 01:23.310
ParseIntPipe

21
01:23.610 --> 01:28.470
Okay, I already explained in the first section, right?

22
01:31.020 --> 01:32.820
So this is a string.

23
01:33.840 --> 01:36.300
And now let me see.

24
01:38.970 --> 01:41.100
What is the id here?

25
01:43.500 --> 01:45.780
It will concatenation from the post.

26
01:48.360 --> 01:51.720
And with the :id from here okay.

27
01:51.750 --> 01:52.920
This is endpoint.

28
01:53.370 --> 01:54.810
Right? I hope you

29
01:54.810 --> 01:55.830
Very clear.

30
01:57.150 --> 02:02.430
And let me go to the service and I will create an async getOne

31
02:05.340 --> 02:08.190
And create post = await

32
02:09.420 --> 02:11.670
Now what is the method?

33
02:11.700 --> 02:13.950
Right now we don't.

34
02:14.040 --> 02:15.150
We don't know.

35
02:15.150 --> 02:20.250
If you don't know how to find the methods inside the post model.

36
02:20.400 --> 02:22.290
You can look at the documentation.

37
02:22.980 --> 02:24.120
This is Mongo right here.

38
02:25.020 --> 02:36.300
It doesn't have anything related to the method in this documentation, because this method is not related

39
02:36.330 --> 02:37.680
to the NestJS.

40
02:37.800 --> 02:42.180
It actually is the mongoose okay.

41
02:42.210 --> 02:44.790
It's a different library, right?

42
02:44.820 --> 02:52.350
For that reason, if you go and click on Read the Docs and inside the API right here, you can click

43
02:52.380 --> 02:56.580
on the query and you will have many query right here.

44
02:56.580 --> 03:01.110
For example like deleteMany, deleteOne, right?

45
03:01.740 --> 03:07.080
Or findOne, find, findOne, right?

46
03:09.280 --> 03:12.460
And that's how to look at.

47
03:13.480 --> 03:15.640
And the findOne() right here is what we look.

48
03:16.540 --> 03:20.860
We will pass it some filter okay.

49
03:20.920 --> 03:24.160
Now I will show you how to do that.

50
03:24.400 --> 03:29.200
findOne() and I can use the _id.

51
03:29.230 --> 03:29.710
Right.

52
03:29.740 --> 03:31.270
This is the method from.

53
03:34.210 --> 03:36.610
This is the property okay.

54
03:36.610 --> 03:40.720
This is the default _id from the MongoDB document.

55
03:41.410 --> 03:44.800
And I will pass the id from parameter will be string.

56
03:45.370 --> 03:47.890
And I will pass directly id right here.

57
03:48.820 --> 03:56.170
And I will return the post okay that is and let's see how it works.

58
04:00.910 --> 04:08.800
In order to make sure it work I will drop the post and I will recreate again over.

59
04:10.930 --> 04:16.220
Okay, I will drop it and I will create a post for example like post one.

60
04:20.660 --> 04:21.440
And post 2.

61
04:22.610 --> 04:24.560
Okay I will create 2 posts

62
04:26.780 --> 04:30.920
And in here inside here, I duplicate one more.

63
04:30.920 --> 04:39.140
Endpoint using the get one post and I will specify the id okay.

64
04:40.280 --> 04:42.050
The ID is what it is right now.

65
04:42.080 --> 04:48.710
I will refresh and I will look into the documentation.

66
04:50.870 --> 04:57.200
Go to the posts collection and look at the documentation right here I will copy right here okay.

67
04:58.400 --> 04:58.820
Like this.

68
04:59.600 --> 05:01.160
And I send a request.

69
05:05.180 --> 05:06.020
This is not work.

70
05:07.580 --> 05:08.570
Why is not work?

71
05:08.600 --> 05:14.790
I will console.log the id I will go here.

72
05:15.150 --> 05:16.980
id I will check.

73
05:18.510 --> 05:20.640
I will check the ID.

74
05:25.080 --> 05:25.320
Here.

75
05:26.850 --> 05:27.780
And open.

76
05:30.390 --> 05:31.050
So again.

77
05:34.530 --> 05:42.720
Oh, sorry for us, I forgot I'll return this.postService

78
05:43.260 --> 05:43.920
.get

79
05:45.060 --> 05:46.530
getOne pass the id

80
05:47.430 --> 05:49.560
Okay, make sure we call the service.

81
05:53.490 --> 05:55.260
And I send again, now

82
05:55.350 --> 05:57.210
You can see, it's already work, right?

83
05:57.810 --> 06:00.510
And it have a id like this

84
06:03.090 --> 06:03.390
Very cool

85
06:03.390 --> 06:03.690
Right.

86
06:05.910 --> 06:15.690
And for example, if I pass the different ID let's see this return something like this.

87
06:15.720 --> 06:16.890
That's not what we want?

88
06:16.920 --> 06:17.220
Okay.

89
06:17.250 --> 06:21.000
I want it has a not found error

90
06:21.030 --> 06:23.190
I can easily check inside the service

91
06:23.760 --> 06:25.200
I can easily check it.

92
06:25.560 --> 06:26.280
!post

93
06:27.210 --> 06:29.160
I will throw

94
06:31.140 --> 06:36.750
new NotFoundException, actually NotFoundException, okay

95
06:36.780 --> 06:37.260
NOT

96
06:37.260 --> 06:38.190
NotFoundError

97
06:38.460 --> 06:42.090
You can see, NotFoundException inside a nestjs/common

98
06:44.490 --> 06:45.420
And I will pass

99
06:45.450 --> 06:45.930
Post

100
06:46.050 --> 06:54.990
With id not found, for example like that, okay. This Exception right here.

101
06:54.990 --> 06:58.230
Is the buil-in exception

102
06:58.680 --> 07:01.470
NestJS already write it for you.

103
07:02.760 --> 07:10.080
Now if I send it have status and message not found.

104
07:10.890 --> 07:11.280
Right.

105
07:11.790 --> 07:12.390
Very cool.

106
07:13.710 --> 07:16.470
So that's for the video.

107
07:16.500 --> 07:18.360
I will see you in the next one.