WEBVTT

1
00:00.920 --> 00:02.840
Hi everyone.

2
00:02.870 --> 00:08.810
Let's continue with CRUD operation for post controller.

3
00:08.960 --> 00:13.520
And we already have a create post, right?

4
00:13.910 --> 00:17.570
In this video I will do the get all methods.

5
00:21.110 --> 00:25.310
So, getAll right here.

6
00:26.630 --> 00:28.610
And what is the get all.

7
00:30.410 --> 00:36.830
Firstly, I will remove un-used import statement right here because it's warning me.

8
00:39.800 --> 00:40.250
Okay.

9
00:41.810 --> 00:44.060
And inside.

10
00:46.370 --> 00:47.360
Inside the post service

11
00:47.390 --> 00:50.750
Let me create async method from getAll.

12
00:54.680 --> 01:03.950
And what's it return? I want to return for me the promise with response DTO.

13
01:04.040 --> 01:04.370
Array

14
01:04.910 --> 01:05.390
Right.

15
01:05.420 --> 01:07.010
Because this is a get all method.

16
01:07.910 --> 01:11.370
So in order to do that. Let me show you.

17
01:15.900 --> 01:16.890
Right here.

18
01:17.940 --> 01:19.860
I will go to the.

19
01:20.100 --> 01:23.790
I will getAll post using this.

20
01:23.910 --> 01:25.140
postModel

21
01:25.650 --> 01:26.430
.find()

22
01:27.120 --> 01:27.720
Okay.

23
01:27.930 --> 01:32.550
This "find()" method right here will get all of documents.

24
01:34.170 --> 01:35.910
And if I console.log.

25
01:36.480 --> 01:36.780
Or.

26
01:36.780 --> 01:39.330
Actually, I think I'll return directly

27
01:43.170 --> 01:52.200
And now you can see this return will be error because I not convert it inside the DTO.

28
01:52.380 --> 01:53.070
Right?

29
01:53.400 --> 01:58.470
We must convert the real entity into the DTO.

30
01:58.650 --> 01:59.340
So.

31
02:01.440 --> 02:05.160
Right now I will use the any

32
02:07.350 --> 02:10.800
And I will show you how we can convert it later.

33
02:11.010 --> 02:18.750
But for now, let's see, why we use that and how it works. Return this.postService

34
02:18.750 --> 02:19.180
dot

35
02:19.780 --> 02:20.230
getAll

36
02:22.420 --> 02:28.270
Okay, go to the postman and I will name to "create post".

37
02:29.650 --> 02:36.070
And I will duplicate and change method to get and post right here.

38
02:37.060 --> 02:39.640
I will change to get all posts

39
02:44.860 --> 02:45.670
Value.

40
02:47.830 --> 02:56.650
Okay, let me send, now you can see, if I get all post, we have a __v right here, right?

41
02:56.680 --> 02:57.430
I don't want it

42
02:57.430 --> 02:57.970
show that

43
02:58.360 --> 03:02.980
And for that reason I will use the DTO

44
03:03.670 --> 03:06.910
Now let's see how we can map it inside the post.

45
03:07.330 --> 03:13.960
DTO, in post right here. I will change the promise.

46
03:14.590 --> 03:17.170
ResponseDTO

47
03:19.000 --> 03:19.570
Array

48
03:20.860 --> 03:21.370
Okay.

49
03:22.030 --> 03:28.000
Inside the post I will map it and I will take the "post".

50
03:28.110 --> 03:31.740
And right now I will return.

51
03:32.490 --> 03:34.530
I will return the post dto.

52
03:34.560 --> 03:35.220
Right?

53
03:37.710 --> 03:39.480
I will return.

54
03:42.750 --> 03:44.970
The _id.

55
03:46.350 --> 03:47.100
will be

56
03:47.130 --> 03:51.840
post._id, title

57
03:54.390 --> 03:54.870
post

58
03:54.900 --> 03:55.290
dot

59
04:02.700 --> 04:04.170
And description.

60
04:08.730 --> 04:09.120
post

61
04:09.120 --> 04:09.600
dot

62
04:09.810 --> 04:10.890
Description.

63
04:11.760 --> 04:13.920
Okay, that's what we want.

64
04:13.950 --> 04:15.330
And it's also.

65
04:17.640 --> 04:20.100
An error, right?

66
04:21.570 --> 04:27.690
The reason because say the "_id" right here is not assignable to type post

67
04:27.720 --> 04:31.410
DTO because the post._id right here is not a string.

68
04:32.100 --> 04:36.120
If I convert it to string here, it will be work.

69
04:36.120 --> 04:42.190
Because this is same shape with the responseDTO here

70
04:42.220 --> 04:45.340
You can see, _id, title and description.

71
04:45.340 --> 04:49.780
So I can return this in this way okay.

72
04:52.810 --> 05:03.430
And in order to add it more explicitly, I will use as ResponsePostDTO

73
05:03.460 --> 05:05.440
To make it better, okay.

74
05:05.860 --> 05:07.960
You can see, right here.

75
05:11.110 --> 05:19.420
And that's it. If I send again, you can see it only have _id, title and description.

76
05:19.450 --> 05:19.990
Right.

77
05:20.890 --> 05:31.510
But you can see, right now it's very tricky for me to create some other method because we always convert

78
05:31.510 --> 05:34.360
it inside the DTO when we respond.

79
05:34.360 --> 05:36.040
Right.

80
05:36.040 --> 05:41.710
And in the few next video I will show you how to refactor this.

81
05:43.030 --> 05:45.550
So I will see you in the next one.