1
00:00:01,400 --> 00:00:02,570
In this section.

2
00:00:02,570 --> 00:00:06,260
We are going to work on the comments API.

3
00:00:06,290 --> 00:00:12,530
So let's get into the controllers and command and here we go for comments.

4
00:00:12,530 --> 00:00:14,000
You're going to have a couple of steps.

5
00:00:14,030 --> 00:00:19,850
Remember, first is you want to find the post that we want to add the comment to.

6
00:00:19,850 --> 00:00:26,090
And after that we are going to push the comment credit into the user's comment field.

7
00:00:26,090 --> 00:00:32,420
And for this one, it's an optional If you want to have all the comments that a user has created, then

8
00:00:32,420 --> 00:00:35,720
you can push the comment into the user's field.

9
00:00:35,720 --> 00:00:40,640
So first step is that we need to protect the controller or the root.

10
00:00:40,640 --> 00:00:45,680
So let's get into the root and comment root, which is this.

11
00:00:45,680 --> 00:00:48,500
So let's require the protected.

12
00:00:53,300 --> 00:00:53,990
All right.

13
00:00:53,990 --> 00:00:59,120
So let's pass it as a second argument to this, as protected.

14
00:00:59,480 --> 00:01:03,560
So let's get into the comments and let's have the roadmap.

15
00:01:03,830 --> 00:01:11,510
First step is less the structure, what the user is sending to the server as red dot body.

16
00:01:11,720 --> 00:01:19,700
For comment, we need let's have a look at the comment model, which is this We need the user and then

17
00:01:19,700 --> 00:01:22,550
the message the user is trying to create.

18
00:01:22,550 --> 00:01:28,010
So here is supposed to be message inside here.

19
00:01:28,160 --> 00:01:37,520
So inside the try first is let's find the post, So let's bring in the post model and then the user

20
00:01:37,520 --> 00:01:38,230
model.

21
00:01:38,240 --> 00:01:42,550
So let's save to have the go to import.

22
00:01:42,560 --> 00:01:54,770
So here I'm going to be the post model and then the user let me also receive it and going to be user.

23
00:01:54,770 --> 00:01:58,040
And I have the two models.

24
00:01:58,190 --> 00:02:10,490
So find the post is going to be caused post is equal to await post dart fine by ID and we can have the

25
00:02:10,490 --> 00:02:14,960
id on the rec dot params dot ID.

26
00:02:15,080 --> 00:02:17,150
Now this one is done.

27
00:02:17,180 --> 00:02:24,020
The next step is create the comment and it goes like this cost.

28
00:02:25,040 --> 00:02:34,160
The comment is equal to await the comment model less in the comment model also.

29
00:02:34,190 --> 00:02:35,450
Sorry guys.

30
00:02:35,450 --> 00:02:36,830
So here we go.

31
00:02:36,860 --> 00:02:39,020
The comment model.

32
00:02:40,500 --> 00:02:41,400
Perfect.

33
00:02:42,660 --> 00:02:43,140
All right.

34
00:02:43,140 --> 00:02:46,860
So comment and then dot create.

35
00:02:47,310 --> 00:02:50,820
Then you pass in the things that we want to create.

36
00:02:50,940 --> 00:02:58,350
First one going to be the user who is creating this comment and it can be found under rec the session.

37
00:03:00,890 --> 00:03:05,630
Don't user off then the message.

38
00:03:05,990 --> 00:03:07,490
And that is it.

39
00:03:07,520 --> 00:03:12,320
So after creating, then you push to the post.

40
00:03:12,320 --> 00:03:20,810
So I'm going to be as this remember, we have the post here, so it's going to be as post dot comments

41
00:03:20,810 --> 00:03:28,910
and dot push and we are pushing the ID of the comment going to be as comments dot underscore ID.

42
00:03:29,630 --> 00:03:35,900
The next step is that I want to push the common credit into the array of the users comments.

43
00:03:35,900 --> 00:03:40,280
So if you look at the users model, we have a field called comments.

44
00:03:40,280 --> 00:03:43,550
So in here we need to find the user.

45
00:03:46,350 --> 00:03:59,610
And going to be as cost user is equal to a weight user model defined by ID and we have the ID inside

46
00:03:59,610 --> 00:04:02,670
rec dot session dot user of.

47
00:04:03,840 --> 00:04:09,560
And let's push the comment into user.

48
00:04:09,570 --> 00:04:18,209
So you're going to be as a user comment dot push and I'm going to push the comment as that going to

49
00:04:18,209 --> 00:04:21,420
be as comment dot underscore ID.

50
00:04:21,810 --> 00:04:30,870
So last step is we need to the last step is we need to receive the post and then the user.

51
00:04:31,170 --> 00:04:35,730
And for this, let's go ahead and disable the validation.

52
00:04:35,730 --> 00:04:44,100
Otherwise mongoose, we say that we are trying to create a post by for post title is required or description

53
00:04:44,100 --> 00:04:44,970
is required.

54
00:04:44,970 --> 00:04:50,490
So let's disable the one and going to be as disable validation.

55
00:04:52,270 --> 00:05:00,130
And let's save the two models and going to be as I wait and the post.

56
00:05:01,250 --> 00:05:10,740
Don't save and you pass in an object and provide validation before save and force.

57
00:05:11,300 --> 00:05:17,690
And let's copy this one for the user as well, when it be as user.

58
00:05:18,230 --> 00:05:19,790
And that is it.

59
00:05:19,820 --> 00:05:23,330
Lastly, let's send back the comment.

60
00:05:24,080 --> 00:05:28,180
Let's go ahead and make the request to create a command.

61
00:05:28,190 --> 00:05:34,280
So let's get into the tender client and let's look at the comments, which is this.

62
00:05:34,550 --> 00:05:38,540
So for comments, we need to provide the ID of the post.

63
00:05:38,540 --> 00:05:43,790
So we're going to be asked API version one for this last comment.

64
00:05:45,570 --> 00:05:48,300
And let's have the ID of the post.

65
00:05:48,300 --> 00:05:52,320
So let's get back to all posts and hit send.

66
00:05:52,770 --> 00:06:00,510
I want to add comment to this post called CSS, so I'll copy this ID and here I'll provide the ID for

67
00:06:00,510 --> 00:06:01,050
that.

68
00:06:01,410 --> 00:06:08,880
So let me hit send and let's have a look where we got four or four, meaning that this root is not found.

69
00:06:09,000 --> 00:06:17,580
So let's get back to the comment route and let's see which is this well, supposed to be column ID for

70
00:06:17,580 --> 00:06:19,710
the great and that is it.

71
00:06:19,710 --> 00:06:22,230
So let's go ahead and make the request.

72
00:06:22,260 --> 00:06:24,510
Well, we still have the same error.

73
00:06:24,570 --> 00:06:30,350
The problem is we need to remove this a slash to have only one for slash.

74
00:06:30,360 --> 00:06:31,920
So let's hit send.

75
00:06:32,960 --> 00:06:37,550
You can see we have this error, meaning that we are not passing in the data.

76
00:06:37,550 --> 00:06:42,230
So here to create a command, we need to pass in the message.

77
00:06:44,490 --> 00:06:46,720
And I'll call this one as good.

78
00:06:46,740 --> 00:06:48,150
So let's hit send.

79
00:06:49,620 --> 00:06:52,740
And we have it as that is supposed to be.

80
00:06:52,740 --> 00:06:53,180
Good.

81
00:06:53,190 --> 00:06:58,080
So let me change this one to good and all right.

82
00:06:58,080 --> 00:07:00,810
So now we have the comments being created.

83
00:07:00,810 --> 00:07:08,940
So let's get back to our post and let's fetch our post and let's look at the post for CSS, which is

84
00:07:08,940 --> 00:07:09,210
this.

85
00:07:09,210 --> 00:07:12,660
And now we have to comment being added.

86
00:07:12,750 --> 00:07:19,530
And if I go to my profile page, I'll be able to see the comments that I have created.

87
00:07:19,530 --> 00:07:28,530
So if I go to users and then the profile, let's hit send and let's have a look on the comment.

88
00:07:28,950 --> 00:07:31,950
You can see I have created to comment.

89
00:07:31,950 --> 00:07:37,860
So let's see how we can populate the comment upon fetching all posts in the next video.

