1
00:00:00,840 --> 00:00:05,850
In this video we are going to implement updating of a comment.

2
00:00:06,060 --> 00:00:09,900
Before we continue on, let me create one comment in my name.

3
00:00:12,060 --> 00:00:15,210
And you can see that I have the icons here.

4
00:00:15,900 --> 00:00:20,290
So when I click on this icon, I want to open a form.

5
00:00:20,310 --> 00:00:28,250
So first step is that let's create the templates and we are going to use the template for logging in.

6
00:00:28,260 --> 00:00:36,630
So duplicate and place it inside the host and rename this one to update comment.

7
00:00:39,090 --> 00:00:43,650
And let me change the title as comments updates.

8
00:00:45,340 --> 00:00:48,940
And here as an update comment.

9
00:00:51,870 --> 00:00:53,610
Perfect for this.

10
00:00:53,610 --> 00:00:57,860
I want only one imput failed and that is the message.

11
00:00:57,870 --> 00:01:06,120
So for this the type is going to be text and the name is going to be message and here is going to be

12
00:01:06,120 --> 00:01:07,260
the message.

13
00:01:08,490 --> 00:01:09,270
All right.

14
00:01:09,270 --> 00:01:10,980
There is no ID for this.

15
00:01:10,980 --> 00:01:16,740
And let me remove this one and here as update comment.

16
00:01:18,960 --> 00:01:19,920
All right.

17
00:01:19,920 --> 00:01:23,130
So for the meantime, let's ignore this one.

18
00:01:23,130 --> 00:01:25,080
We want to render the form.

19
00:01:25,110 --> 00:01:31,450
So next step is we need to create a route for a to be able to render the comment.

20
00:01:31,470 --> 00:01:34,470
So let's get to the controllers.

21
00:01:35,440 --> 00:01:36,610
And comment.

22
00:01:36,610 --> 00:01:37,930
And here you go.

23
00:01:37,960 --> 00:01:41,240
I'm going to create one to fetch a single comment.

24
00:01:41,260 --> 00:01:44,050
And lucky for us, we have it as here.

25
00:01:44,080 --> 00:01:49,810
So here we can go ahead and then render the template and find the particular comment.

26
00:01:49,810 --> 00:01:51,370
So here we go.

27
00:01:52,420 --> 00:01:53,410
Cost.

28
00:01:55,110 --> 00:01:58,080
Comment is equal to a weight.

29
00:01:59,320 --> 00:02:08,949
The comment model dot find by id req dot params dot id perfect.

30
00:02:08,949 --> 00:02:13,870
So let me remove the JSON data here and rest the render.

31
00:02:15,400 --> 00:02:16,270
Then.

32
00:02:17,100 --> 00:02:22,500
Go to comment and then first, last update, comment form.

33
00:02:26,580 --> 00:02:35,490
And I'm going to pass in the comments being fetched and then the error in case we have one for the meantime.

34
00:02:36,060 --> 00:02:38,590
And I would say comment.

35
00:02:38,610 --> 00:02:39,960
Fetching field.

36
00:02:44,170 --> 00:02:44,680
All right.

37
00:02:44,680 --> 00:02:48,520
So the same thing here inside a cast block.

38
00:02:48,520 --> 00:02:52,570
But I'm going to use there is no comment at this point.

39
00:02:52,570 --> 00:02:59,170
And for the error, I'm going to use an error from the server as error message.

40
00:02:59,500 --> 00:03:01,060
And that is it.

41
00:03:01,270 --> 00:03:04,240
Next point is that we need to have the path.

42
00:03:04,240 --> 00:03:12,580
So if you go to the root command and let's look at the root for finding single command, which is this

43
00:03:12,580 --> 00:03:13,180
one.

44
00:03:13,180 --> 00:03:16,150
So let's get back to the post details.

45
00:03:17,070 --> 00:03:19,140
That's where we want to trigger that.

46
00:03:19,140 --> 00:03:21,870
So post details.

47
00:03:23,040 --> 00:03:24,510
And this is the icon.

48
00:03:24,510 --> 00:03:32,820
So here the path is going to be four slash API forward slash version one first last command and then

49
00:03:32,820 --> 00:03:34,380
the ID of the command.

50
00:03:34,380 --> 00:03:40,290
And lucky for us we have it inside this particular template equal to command.

51
00:03:42,670 --> 00:03:44,650
Then, though it's ID.

52
00:03:45,990 --> 00:03:46,530
Great.

53
00:03:46,530 --> 00:03:48,850
So now let's check it out.

54
00:03:48,870 --> 00:03:49,950
Refresh it.

55
00:03:50,430 --> 00:03:55,240
Click on this icon and we have this error.

56
00:03:55,260 --> 00:03:56,400
Let's have a look.

57
00:03:56,640 --> 00:04:03,360
It says that fail to look up, meaning that you couldn't find the template that we are rendering, which

58
00:04:03,360 --> 00:04:04,140
is this.

59
00:04:04,140 --> 00:04:08,700
So let's look at the comment folder is comment.

60
00:04:10,230 --> 00:04:11,550
It's coming out.

61
00:04:11,550 --> 00:04:15,140
And so I need to put it inside the comment folder.

62
00:04:15,150 --> 00:04:20,070
So now I'm going to copy this one and put it inside the comment folder.

63
00:04:20,279 --> 00:04:22,350
Yeah, that is the problem.

64
00:04:22,950 --> 00:04:23,910
All right.

65
00:04:23,910 --> 00:04:26,820
So let's get back refreshed.

66
00:04:27,900 --> 00:04:29,100
And at its.

67
00:04:29,990 --> 00:04:36,980
Well, we have this error comment fetching field meaning is coming from this place.

68
00:04:37,130 --> 00:04:39,630
Sorry, it's supposed to be empty.

69
00:04:39,650 --> 00:04:40,760
Sorry, guys.

70
00:04:41,420 --> 00:04:41,930
Yeah.

71
00:04:41,930 --> 00:04:42,790
Yes, empty.

72
00:04:42,800 --> 00:04:43,910
Sorry, guys.

73
00:04:44,240 --> 00:04:46,730
All right, so let's refresh it.

74
00:04:47,210 --> 00:04:51,890
And I have that I want to populate into the form as we did.

75
00:04:51,890 --> 00:05:01,550
So we're going to be as value is equal to edges, equal to the comment.

76
00:05:02,400 --> 00:05:04,350
Dot message.

77
00:05:08,880 --> 00:05:11,370
And I have it as that.

78
00:05:11,670 --> 00:05:15,630
The next step is that I need to call the right end point.

79
00:05:15,630 --> 00:05:23,190
So let's look at the comments and points and let's have a look inside the route comment.

80
00:05:23,190 --> 00:05:24,540
And here we go.

81
00:05:24,750 --> 00:05:27,990
We need to pass in the ID of the comment.

82
00:05:27,990 --> 00:05:33,660
So let's add that one inside the views comments and here we go.

83
00:05:34,260 --> 00:05:40,200
So the path here is going to be comment and then the ID of the comment.

84
00:05:40,200 --> 00:05:42,120
So let me copy this one.

85
00:05:43,100 --> 00:05:51,110
And here changed this one to underscore ID because I'm going to use put method and the form does not

86
00:05:51,110 --> 00:05:52,030
support that.

87
00:05:52,040 --> 00:05:58,760
I need to use the override method as underscore method equal to put.

88
00:06:00,140 --> 00:06:01,490
Right now.

89
00:06:01,490 --> 00:06:02,900
We are all set.

90
00:06:03,230 --> 00:06:06,530
So let's get back home.

91
00:06:07,790 --> 00:06:10,700
And continue reading.

92
00:06:12,050 --> 00:06:15,320
Our dates change this 1 to 3.

93
00:06:17,020 --> 00:06:18,430
Now six.

94
00:06:18,820 --> 00:06:22,600
Let's see if indeed it has been updated.

95
00:06:25,610 --> 00:06:27,620
And I have it as that.

96
00:06:27,620 --> 00:06:32,620
So the next step is I want to redirect after updating the comment.

97
00:06:32,630 --> 00:06:34,940
So how are you going to get around done?

98
00:06:35,030 --> 00:06:37,610
You want to go back to the posts?

99
00:06:37,610 --> 00:06:44,480
So let's get into the controller for comment and I think you are going to implement the same way as

100
00:06:44,480 --> 00:06:45,620
we did for deleting.

101
00:06:45,620 --> 00:06:46,070
Right?

102
00:06:46,070 --> 00:06:53,390
So inside the controller of the comments, let me collapse this, this one and that and let's work on

103
00:06:53,390 --> 00:06:54,290
the update.

104
00:06:54,410 --> 00:06:58,850
So here we are going to redirect to the post page.

105
00:06:58,850 --> 00:07:05,840
That is a single post page and you are going to use the same format when we delete in the comment.

106
00:07:05,840 --> 00:07:14,060
So here I'm going to copy this one and inside here remove that and paste.

107
00:07:14,060 --> 00:07:22,160
And again, we don't have access to the post inside there, so we are going to pass it as a query string

108
00:07:22,160 --> 00:07:23,540
or query parameter.

109
00:07:23,540 --> 00:07:28,160
So I'm going to provide as the way we did for deleting the comment.

110
00:07:28,160 --> 00:07:32,180
So let's go into the post details.

111
00:07:33,860 --> 00:07:40,600
Post and post details, which is this and this is the path I'm referring to.

112
00:07:40,610 --> 00:07:46,730
So here I'm going to copy this one entirely up to here.

113
00:07:46,970 --> 00:07:50,480
And let me go to the update.

114
00:07:50,630 --> 00:07:53,960
This is the path so I'm going to use.

115
00:07:53,960 --> 00:07:56,300
Let me paste it here and look at the path.

116
00:07:56,300 --> 00:08:01,700
Well, I'm going to use an operator and then the ID of the post.

117
00:08:01,700 --> 00:08:05,450
So here I'm going to append it as that.

118
00:08:06,080 --> 00:08:06,530
Perfect.

119
00:08:06,560 --> 00:08:08,210
Now let me remove this one.

120
00:08:08,930 --> 00:08:09,650
Great.

121
00:08:09,650 --> 00:08:16,370
So now I have access to the post ID inside the controller as we did for the deleting.

122
00:08:16,760 --> 00:08:23,450
So in here everything remains the same because we are taking the ID of the post from the query string

123
00:08:23,450 --> 00:08:25,580
as we did for the deleting.

124
00:08:25,580 --> 00:08:27,770
So I think we'd be good to go.

125
00:08:27,770 --> 00:08:28,130
Right.

126
00:08:28,130 --> 00:08:38,990
So let me go back home, refresh and edit where we have this error simply because we don't have post

127
00:08:38,990 --> 00:08:47,690
inside the update comment template, but we are using the post here and we are not passing the post

128
00:08:47,690 --> 00:08:48,920
to the template.

129
00:08:48,950 --> 00:08:57,200
But the problem here is that there is no way we can have access to the post inside this template.

130
00:08:57,200 --> 00:09:03,050
So what we can do is that we are going to add the post to the model.

131
00:09:03,050 --> 00:09:08,390
So let's get back to the model and and the comment.

132
00:09:08,390 --> 00:09:11,570
We are going to add a field called post.

133
00:09:11,570 --> 00:09:19,580
So when you find a single comment, we can populate the post in that way we can use the ID inside the

134
00:09:19,580 --> 00:09:20,180
template.

135
00:09:20,180 --> 00:09:29,060
So here we going to be as post and going to be object with a type of Mongoose object ID and we are going

136
00:09:29,060 --> 00:09:32,180
to reference the post model.

137
00:09:35,110 --> 00:09:38,440
It's that and it's going to be a required field.

138
00:09:38,980 --> 00:09:44,820
So for this, upon creating a command, we are going to populate the post as well.

139
00:09:44,830 --> 00:09:52,870
So let's get into the controller of comment and let's look at the creation of the comments, which is

140
00:09:52,870 --> 00:09:53,350
this.

141
00:09:53,560 --> 00:09:56,260
So here we are going to add the post.

142
00:09:56,260 --> 00:09:58,120
We are associating to that.

143
00:09:58,120 --> 00:10:03,550
So we are going to be as post dot and as called ID.

144
00:10:03,580 --> 00:10:05,680
So now we will be good to go.

145
00:10:05,680 --> 00:10:13,270
So next time you create a post, you're going to see a post ID on that and that way we can easily use

146
00:10:13,270 --> 00:10:16,390
it inside here and let's have a look.

147
00:10:17,350 --> 00:10:20,650
So add one here as best.

148
00:10:22,780 --> 00:10:23,710
Submit.

149
00:10:24,870 --> 00:10:27,150
And let me click on edit.

150
00:10:27,720 --> 00:10:34,710
But for this one, if you console.log the comments, you're going to see the post being associated on

151
00:10:34,710 --> 00:10:35,250
that.

152
00:10:35,430 --> 00:10:42,420
Inside the comments, we are going to have a property called post as we added inside the module.

153
00:10:42,450 --> 00:10:46,260
So now I can go ahead and make use of comments.

154
00:10:48,610 --> 00:10:54,980
The Post and the Post represent the ID so we be good to go.

155
00:10:55,000 --> 00:10:56,530
I believe that.

156
00:10:56,830 --> 00:11:00,280
So let's go ahead and then refresh it.

157
00:11:01,520 --> 00:11:03,140
And edit.

158
00:11:03,410 --> 00:11:05,330
Now you can see that it works fine.

159
00:11:07,940 --> 00:11:11,360
You can see that it has been redirected me again.

160
00:11:11,420 --> 00:11:14,720
Let me update with let's say best to.

161
00:11:18,160 --> 00:11:20,230
And now you can see that it worked fine.

162
00:11:20,230 --> 00:11:22,420
So let me trust this one's.

163
00:11:24,100 --> 00:11:28,250
Unless I look in as a man before I can delete that.

164
00:11:28,270 --> 00:11:32,110
So, guys, that is how we can implement that.

