1
00:00:03,950 --> 00:00:09,405
The second part of this exercise deals with

2
00:00:09,405 --> 00:00:15,340
the various get put post and delete operations on sub-documents inside the document.

3
00:00:15,340 --> 00:00:19,380
As we saw in the example for dishes,

4
00:00:19,380 --> 00:00:26,680
we saw that the comments are included inside the dish itself as a sub-document,

5
00:00:26,680 --> 00:00:31,680
in the way we defined both the comment schema and the dish schema,

6
00:00:31,680 --> 00:00:35,150
and then define the dish model by including the comment schema

7
00:00:35,150 --> 00:00:38,880
into the dish schema as an array of comments.

8
00:00:38,880 --> 00:00:43,580
And we saw in the previous exercise how the comments are enclosed inside

9
00:00:43,580 --> 00:00:50,990
an array in the form of a document array inside each dish document.

10
00:00:50,990 --> 00:00:55,525
So how do we make modifications to the comments themselves?

11
00:00:55,525 --> 00:00:59,590
How do we get all the list of comments for a dish?

12
00:00:59,590 --> 00:01:06,515
How do we modify a specific comment in the dish and so on?

13
00:01:06,515 --> 00:01:10,820
So this is what we will see about how to

14
00:01:10,820 --> 00:01:16,130
support them using the various get put post and delete operations on

15
00:01:16,130 --> 00:01:22,385
the dish dishes slash dish ID slash comments and the dishes

16
00:01:22,385 --> 00:01:29,935
slash dish ID slash comments slash comment ID REST API endpoints.

17
00:01:29,935 --> 00:01:32,885
To get started on this exercise,

18
00:01:32,885 --> 00:01:35,735
again, going back to the dish router,

19
00:01:35,735 --> 00:01:44,170
they have already seen how we handle the slash endpoint and the slash dish ID endpoint.

20
00:01:44,170 --> 00:01:50,300
Now, much of this work that we do here is already useful for us,

21
00:01:50,300 --> 00:01:53,855
so what I'm going to do is to copy this code,

22
00:01:53,855 --> 00:02:01,070
and then we will edit that code appropriately to handle the comments and the comment ID.

23
00:02:01,070 --> 00:02:04,250
So I'm going to copy this entire code that we have

24
00:02:04,250 --> 00:02:08,750
here both for the slash and the slash golden dish ID,

25
00:02:08,750 --> 00:02:14,300
and then we will paste it right below here again.

26
00:02:14,300 --> 00:02:17,795
And then we will go in and modify each one of them.

27
00:02:17,795 --> 00:02:21,470
So we have seen that if you look back up,

28
00:02:21,470 --> 00:02:27,800
the first one is handling the slash which will be mounted on the slash dishes endpoint.

29
00:02:27,800 --> 00:02:30,905
And the next one is handling the slash dish ID.

30
00:02:30,905 --> 00:02:37,410
So the third one should be handling slash dish ID slash comments.

31
00:02:37,410 --> 00:02:39,060
So going down here,

32
00:02:39,060 --> 00:02:49,080
this third dish router route would be slash dish ID slash comments.

33
00:02:49,080 --> 00:02:51,300
And then the last one, of course,

34
00:02:51,300 --> 00:02:56,610
would be slash dish ID slash

35
00:02:56,610 --> 00:03:05,670
comments and then slash colon comment ID.

36
00:03:05,670 --> 00:03:09,289
So now that we have updated these routes here,

37
00:03:09,289 --> 00:03:12,380
now the next step is to go in and fix

38
00:03:12,380 --> 00:03:16,310
the code in the get put post and delete operations here.

39
00:03:16,310 --> 00:03:22,025
So we will start off with the slash dish ID slash comments endpoint.

40
00:03:22,025 --> 00:03:25,530
So in case of the slash dish ID slash comments endpoint,

41
00:03:25,530 --> 00:03:31,425
so we will do dishes find but instead of saying dishes find,

42
00:03:31,425 --> 00:03:38,100
we will do dishes find by

43
00:03:38,100 --> 00:03:45,595
ID and so we will find the specific dish that we are looking for.

44
00:03:45,595 --> 00:03:53,335
So here, we will look for that req params dish ID,

45
00:03:53,335 --> 00:03:57,820
so we will first search for the dish,

46
00:03:57,820 --> 00:04:02,900
and so this will return a specific dish.

47
00:04:02,900 --> 00:04:06,865
So when it returns that specific dish here,

48
00:04:06,865 --> 00:04:13,475
then what they will check for inside here

49
00:04:13,475 --> 00:04:20,765
is that if dish not equal to null.

50
00:04:20,765 --> 00:04:24,169
It is quite possible that the dish doesn't exist,

51
00:04:24,169 --> 00:04:28,000
so if we search for a specific dish with a dish ID,

52
00:04:28,000 --> 00:04:29,495
that dish may not exist,

53
00:04:29,495 --> 00:04:35,360
so we need to worry about handling that particular situation.

54
00:04:35,360 --> 00:04:39,590
So let's handle that part inside here,

55
00:04:39,590 --> 00:04:43,985
and so if it is not null then that means that the dish exists.

56
00:04:43,985 --> 00:04:51,125
So I'm going to cut this out and then paste it in there.

57
00:04:51,125 --> 00:04:54,150
And then in this case,

58
00:04:54,150 --> 00:04:56,940
since the dish is not null,

59
00:04:56,940 --> 00:05:02,300
so we will return the status code and the set header and

60
00:05:02,300 --> 00:05:07,755
then this here we will be returning dish comments,

61
00:05:07,755 --> 00:05:13,685
because we are asked to only return the comments for that particular dish, so we'll say,

62
00:05:13,685 --> 00:05:16,365
"Dishes find by ID,

63
00:05:16,365 --> 00:05:20,390
then req parameters dish ID and then dish."

64
00:05:20,390 --> 00:05:25,540
And so we will return the dish comments here.

65
00:05:25,540 --> 00:05:30,320
Otherwise, so this is the else part.

66
00:05:30,320 --> 00:05:33,500
So which means that the dish does not exist.

67
00:05:33,500 --> 00:05:35,295
So in this case,

68
00:05:35,295 --> 00:05:43,095
we would construct an error here by saying new errors and then we'll say

69
00:05:43,095 --> 00:05:56,515
dish req params dish ID not found.

70
00:05:56,515 --> 00:05:59,840
So this particular dish was not found,

71
00:05:59,840 --> 00:06:04,100
so obviously we won't be able to return the comments for the non-existing dish.

72
00:06:04,100 --> 00:06:05,560
So in this case,

73
00:06:05,560 --> 00:06:09,740
so we will create a new error object and then we will

74
00:06:09,740 --> 00:06:15,090
set the status in the error object to 404.

75
00:06:15,090 --> 00:06:17,855
Again, this is 404 not found,

76
00:06:17,855 --> 00:06:23,420
and then we will return next err.

77
00:06:23,420 --> 00:06:24,990
Now, why do we do this?

78
00:06:24,990 --> 00:06:29,885
Because if you return this as an error, as you'll recall,

79
00:06:29,885 --> 00:06:33,665
this will be handled by your app.js file,

80
00:06:33,665 --> 00:06:35,565
so in the app.js file,

81
00:06:35,565 --> 00:06:37,220
right at the bottom here,

82
00:06:37,220 --> 00:06:40,700
we have the error handler here.

83
00:06:40,700 --> 00:06:42,685
So when it comes in here,

84
00:06:42,685 --> 00:06:46,560
this will set the rest or status to error dot status,

85
00:06:46,560 --> 00:06:48,410
which we had set to 404,

86
00:06:48,410 --> 00:06:49,900
so that is what will be returned,

87
00:06:49,900 --> 00:06:56,720
and then it will simply take that error and then render that error here.

88
00:06:56,720 --> 00:07:01,020
So that will take care of rendering the errors.

89
00:07:01,020 --> 00:07:07,080
So this will return the error message back to our client.

90
00:07:07,080 --> 00:07:09,490
That is what we are invoking here,

91
00:07:09,490 --> 00:07:11,090
so if the dish does not exist,

92
00:07:11,090 --> 00:07:15,475
we're just going to return that error here from the get operation.

93
00:07:15,475 --> 00:07:19,475
Now, for post in case of post,

94
00:07:19,475 --> 00:07:24,920
we are expecting that they would be

95
00:07:24,920 --> 00:07:30,980
returned a dish ID and then we will look for the dish,

96
00:07:30,980 --> 00:07:35,840
and then we will take the set of comments

97
00:07:35,840 --> 00:07:41,555
from the body and then push it into the dish there.

98
00:07:41,555 --> 00:07:46,375
So we will say dishes, again, here,

99
00:07:46,375 --> 00:07:52,750
we will have to do the find by id because we are looking for the specific dish here.

100
00:07:52,750 --> 00:07:59,485
So for the post, we'll say dish find by ID then dish,

101
00:07:59,485 --> 00:08:01,750
and so inside here,

102
00:08:01,750 --> 00:08:04,060
if the dish exists,

103
00:08:04,060 --> 00:08:06,555
then we will handle the dish appropriately.

104
00:08:06,555 --> 00:08:10,360
If not, then we will have to send the error message.

105
00:08:10,360 --> 00:08:16,730
So let me copy this part from the get and then,

106
00:08:16,730 --> 00:08:20,710
we will replace this here.

107
00:08:22,130 --> 00:08:25,155
So obviously, if the dish does not exist,

108
00:08:25,155 --> 00:08:27,165
then we're going to return this error here,

109
00:08:27,165 --> 00:08:31,035
but if the dish exists, in that case,

110
00:08:31,035 --> 00:08:35,930
we will return the status code 200 set header.

111
00:08:35,930 --> 00:08:41,835
But before we pass back the value,

112
00:08:41,835 --> 00:08:50,400
what I am going to do is to say dish comments,

113
00:08:50,400 --> 00:08:55,020
so we are posting a new set of comments for the dish.

114
00:08:55,130 --> 00:09:02,790
So we'll say dish comments push req body.

115
00:09:02,790 --> 00:09:07,785
Because the body of the message contains all the comments that need to be pushed here,

116
00:09:07,785 --> 00:09:13,890
and then, we will say, "Dish save."

117
00:09:13,890 --> 00:09:22,010
And so if the save returns successfully with the dish here,

118
00:09:22,010 --> 00:09:27,030
so we will send the updated dish from here.

119
00:09:32,350 --> 00:09:40,600
So this part where we are constructing the status code,

120
00:09:40,600 --> 00:09:49,750
we will do that inside here and then we will send the back the

121
00:09:54,320 --> 00:10:00,875
Res.json and the dish that we obtained here.

122
00:10:00,875 --> 00:10:04,885
So, what we are doing is we are saving the dish here.

123
00:10:04,885 --> 00:10:11,430
We're first pushing the comments into the dish there,

124
00:10:11,430 --> 00:10:14,420
the new set of comments into the dish here,

125
00:10:14,420 --> 00:10:19,210
and then we are saving the updated dish here.

126
00:10:19,210 --> 00:10:20,825
Then after the save,

127
00:10:20,825 --> 00:10:29,070
we're going to be returning the updated dish back to the user here.

128
00:10:29,290 --> 00:10:32,125
Then if there is an error,

129
00:10:32,125 --> 00:10:37,370
then we will handle the error just like we have done here.

130
00:10:38,570 --> 00:10:41,420
If the dish doesn't exist, of course,

131
00:10:41,420 --> 00:10:45,825
we are going to be returning the error saying that the dish does not exist.

132
00:10:45,825 --> 00:10:47,475
So, this is for the post.

133
00:10:47,475 --> 00:10:49,100
The PUT operation, of course,

134
00:10:49,100 --> 00:10:51,680
is not supported on dishes.

135
00:10:51,680 --> 00:10:53,140
So, in this case,

136
00:10:53,140 --> 00:10:56,210
we are trying to perform the PUT operation

137
00:10:56,210 --> 00:11:06,550
on the dishes, req.params.dishId/comments.

138
00:11:08,370 --> 00:11:10,980
So, this is not allowed.

139
00:11:10,980 --> 00:11:13,415
So, that's what we are saying for the PUT operation.

140
00:11:13,415 --> 00:11:15,545
For the delete operation,

141
00:11:15,545 --> 00:11:19,130
what we will do is first find the dish.

142
00:11:19,130 --> 00:11:22,275
So, we'll say dish.findbyId.

143
00:11:22,275 --> 00:11:27,785
So, delete means that we are removing all the comments from the dish,

144
00:11:27,785 --> 00:11:28,850
not the dish itself,

145
00:11:28,850 --> 00:11:31,540
but we are removing all the comments from the dish.

146
00:11:31,540 --> 00:11:33,495
So, in this case,

147
00:11:33,495 --> 00:11:41,695
what we would end up doing is as follows: so if the dish is not null,

148
00:11:41,695 --> 00:11:47,345
recall that we need to handle it by saying then dish.

149
00:11:47,345 --> 00:11:49,400
If the dish is not null,

150
00:11:49,400 --> 00:11:53,080
then we would remove all the comments from the dish.

151
00:11:53,080 --> 00:11:57,480
So, I'm going to copy that code and then paste it in here,

152
00:11:57,480 --> 00:12:00,880
and then we will edit this code, then delete.

153
00:12:00,880 --> 00:12:06,070
We will see if dish is not null,

154
00:12:06,070 --> 00:12:10,085
then they will return the dish here.

155
00:12:10,085 --> 00:12:12,495
So, if dish is not not null,

156
00:12:12,495 --> 00:12:16,700
the wat we would handle this is as follows.

157
00:12:16,700 --> 00:12:24,975
So, we will have to go in and delete each of the comments.

158
00:12:24,975 --> 00:12:36,380
So, we'll say for (var i =

159
00:12:36,380 --> 00:12:49,940
(dish.comments.length -1) i>= 0; i--).

160
00:12:49,940 --> 00:12:55,860
So, we are literally going in and removing each comment.

161
00:12:55,860 --> 00:12:56,380
So, we'll say

162
00:12:56,380 --> 00:13:11,370
dish.comments.id(dish.comments[i]._id).

163
00:13:11,370 --> 00:13:15,060
We'll say remove.

164
00:13:15,060 --> 00:13:19,490
So, we are going to be removing each of the dishes,

165
00:13:19,490 --> 00:13:22,520
one under each of the comments one by one.

166
00:13:23,420 --> 00:13:26,910
So, this is what we will do to the comments.

167
00:13:26,910 --> 00:13:30,600
Then once we have removed all the comments for the dish,

168
00:13:30,600 --> 00:13:34,800
then we will have to save the dish and then send back.

169
00:13:34,800 --> 00:13:38,345
So, earlier also when we modify the dish,

170
00:13:38,345 --> 00:13:39,760
this is what we did here.

171
00:13:39,760 --> 00:13:43,250
So, I'm going to copy this part,

172
00:13:43,610 --> 00:13:46,415
and then come in here,

173
00:13:46,415 --> 00:13:52,060
and then say dish.save,

174
00:13:52,060 --> 00:13:57,085
then we will return the dish

175
00:13:57,085 --> 00:14:02,470
here to indicate the updated dish being returned here.

176
00:14:02,470 --> 00:14:05,130
So, this is how we will delete the comments.

177
00:14:05,130 --> 00:14:07,225
So, here we are going in,

178
00:14:07,225 --> 00:14:10,800
now there is no easy way of literally pulling out

179
00:14:10,800 --> 00:14:15,330
all the comments from the array when you have a sub-document.

180
00:14:15,330 --> 00:14:19,680
So, you have to go in and delete each sub-document one by one.

181
00:14:19,680 --> 00:14:22,720
So, that is what I am doing in this for loop here.

182
00:14:22,720 --> 00:14:27,500
So, I'm saying for (var i=dish.comments.length).

183
00:14:27,500 --> 00:14:31,270
So, I'm looking at the array of comments and then starting from

184
00:14:31,270 --> 00:14:35,430
the last comment in that array all the way to the very first comment,

185
00:14:35,430 --> 00:14:38,940
I'm going in and then deleting comment by comment here by using

186
00:14:38,940 --> 00:14:42,905
the remove operation on the subdocument.

187
00:14:42,905 --> 00:14:47,320
So, the way we will access a subdocument is by saying

188
00:14:47,320 --> 00:14:53,720
dish and then comments is the field name and then I say id here.

189
00:14:53,720 --> 00:14:56,259
So, this is how you access a subdocument,

190
00:14:56,259 --> 00:15:02,115
and inside here you will specify the id of the subdocuments that you're trying to access.

191
00:15:02,115 --> 00:15:06,310
So, this whole thing will give you access to the subdocument,

192
00:15:06,310 --> 00:15:09,670
and then we call the remove method on the subdocument,

193
00:15:09,670 --> 00:15:14,320
and so that subdocument will be removed from the array of subdocuments.

194
00:15:14,320 --> 00:15:17,980
Then after that, after we have deleted all the comments,

195
00:15:17,980 --> 00:15:21,075
I'll save the changes and that's it.

196
00:15:21,075 --> 00:15:25,840
So, all my comments will be deleted from then dish.

197
00:15:25,840 --> 00:15:33,335
Now, we will deal with the next end point which is dishIdcomments/:commentId.

198
00:15:33,335 --> 00:15:35,460
So, in this case,

199
00:15:35,460 --> 00:15:42,410
we will start out by first locating the dish and then after we locate the dish,

200
00:15:42,410 --> 00:15:47,460
then we have to make sure that the dish exists,

201
00:15:47,460 --> 00:15:49,310
and then deal with the dish.

202
00:15:49,310 --> 00:15:52,320
So, I'm going to delete this part,

203
00:15:52,320 --> 00:15:59,350
and then we'll say let's copy this part from the git that we had earlier

204
00:16:09,140 --> 00:16:13,650
because we need to be able to deal with the situation

205
00:16:13,650 --> 00:16:20,095
where either the dish doesn't exist or the dish exists but the comments don't exist,

206
00:16:20,095 --> 00:16:23,870
or the dish itself does not exist.

207
00:16:23,870 --> 00:16:28,280
So, there are three conditions that I need to test.

208
00:16:28,280 --> 00:16:32,650
So, we'll say if (dish!=

209
00:16:32,650 --> 00:16:51,180
null && dish.comments.id (req.params.commentId)!=

210
00:16:51,180 --> 00:17:00,545
null, so which means that the dish itself exists and also the comments exist in the dish,

211
00:17:00,545 --> 00:17:05,560
only then we can send back a specific comment that has been asked for.

212
00:17:05,560 --> 00:17:07,680
So, in that case,

213
00:17:07,680 --> 00:17:14,525
we will say statusCode is 200, setHeader application json,

214
00:17:14,525 --> 00:17:19,895
and then here we will send back dish.comments,

215
00:17:19,895 --> 00:17:25,300
and we are sending back this particular comment.

216
00:17:25,300 --> 00:17:29,350
So, that is the specific comment that we are sending max.

217
00:17:29,350 --> 00:17:37,925
So, this will allow us to retrieve a specific comment from the set of comments.

218
00:17:37,925 --> 00:17:46,895
Now, we'll say else if dish equal to null.

219
00:17:46,895 --> 00:17:49,180
So the dishes null,

220
00:17:49,180 --> 00:17:55,830
then of course we will have to indicate that the dish doesn't exist and

221
00:17:55,830 --> 00:18:02,105
the last condition is if the comment itself is null.

222
00:18:02,105 --> 00:18:08,360
So, in that case also I have to indicate that the comment itself doesn't exist,

223
00:18:08,360 --> 00:18:13,130
that particular comment ID doesn't exist so in that case,

224
00:18:13,130 --> 00:18:15,355
I will say new error

225
00:18:15,355 --> 00:18:23,930
comment req.params.commentId not found.

226
00:18:23,930 --> 00:18:25,520
So, three conditions.

227
00:18:25,520 --> 00:18:31,045
First, you have to make sure first that the dish exists and the dish comments exist,

228
00:18:31,045 --> 00:18:34,285
if they do then i can send back the specific comment,

229
00:18:34,285 --> 00:18:41,505
if not, if the dish itself does not exist on the server side in the database,

230
00:18:41,505 --> 00:18:44,340
then I will indicate that the dish does not exist but if

231
00:18:44,340 --> 00:18:47,300
the dish exists but the comment does not exist,

232
00:18:47,300 --> 00:18:50,870
then I will send back the message saying the comment does not exist.

233
00:18:50,870 --> 00:18:55,495
So, notice that they have to handle all the possible conditions here for the GET.

234
00:18:55,495 --> 00:18:57,500
For the POST of course,

235
00:18:57,500 --> 00:19:01,895
the post operation is not supported on this endpoint,

236
00:19:01,895 --> 00:19:04,060
so we'll say

237
00:19:08,900 --> 00:19:18,100
comments slash req.params.commentId.

238
00:19:18,950 --> 00:19:26,205
So, the POST operation is not supported on this comment Id. Now what about PUT?

239
00:19:26,205 --> 00:19:31,360
PUT is a more interesting case here.

240
00:19:31,360 --> 00:19:33,550
So, again even for the put,

241
00:19:33,550 --> 00:19:40,205
I to have to handle all these possible conditions,

242
00:19:40,205 --> 00:19:49,150
so I am going to look at this and say, okay for this,

243
00:19:49,370 --> 00:19:55,045
let me copy this whole code because we have to handle

244
00:19:55,045 --> 00:20:03,260
all these possibilities and then I'll tell you exactly how we handle the PUT case here.

245
00:20:03,260 --> 00:20:06,685
So, in case of PUT,

246
00:20:06,685 --> 00:20:10,140
first of course we need to locate the carnage.

247
00:20:10,140 --> 00:20:13,600
So they'll say dishes find by Id req param Id,

248
00:20:13,600 --> 00:20:20,845
then if the dish exists and the comment itself exists,

249
00:20:20,845 --> 00:20:23,080
then I know that I can update the comment.

250
00:20:23,080 --> 00:20:28,660
Otherwise, these two conditions obviously have to be taken care of,

251
00:20:28,660 --> 00:20:32,740
that comment does not exist or the dish does not exist.

252
00:20:32,740 --> 00:20:35,350
These two are already handled by that case.

253
00:20:35,350 --> 00:20:38,190
Now, here is the special situation that we have.

254
00:20:38,190 --> 00:20:45,355
So,if the dish exists and the specific comment that I am looking for also exists,

255
00:20:45,355 --> 00:20:50,300
then what I know is that in the body of the message,

256
00:20:50,300 --> 00:20:53,760
the update for the comment is specified.

257
00:20:53,760 --> 00:20:55,625
So I need to update that.

258
00:20:55,625 --> 00:21:00,760
So, somewhere here I do modifications,

259
00:21:00,760 --> 00:21:02,910
I do a dish save here,

260
00:21:02,910 --> 00:21:10,110
so I am going to go in and copy a little bit of this code from here,

261
00:21:10,960 --> 00:21:17,680
of course this is not the right thing but I just need a part of the code from there,

262
00:21:17,680 --> 00:21:19,940
so I will come into the PUT.

263
00:21:19,940 --> 00:21:23,270
Let me paste it into place here and

264
00:21:23,270 --> 00:21:27,340
then I'll tell you exactly how they are going to handle this.

265
00:21:27,700 --> 00:21:31,200
Because I need the dish save part.

266
00:21:31,200 --> 00:21:35,950
So, how do we handle the update to a specific comment?

267
00:21:35,950 --> 00:21:39,510
So for the update to a specific comment,

268
00:21:39,510 --> 00:21:45,265
what do we know is that this comment needs to be updated,

269
00:21:45,265 --> 00:21:49,900
so we know that the dish exists and the comment itself exists.

270
00:21:49,900 --> 00:21:53,885
I want to update the fields of the current.

271
00:21:53,885 --> 00:21:56,690
Now, if a comment already exists then I don't want

272
00:21:56,690 --> 00:21:59,530
to allow the user to change the author of the comment,

273
00:21:59,530 --> 00:22:00,890
the author should retained.

274
00:22:00,890 --> 00:22:09,090
The only two fields that I would allow the user update is the rating.

275
00:22:09,090 --> 00:22:12,380
So within the body of the incoming PUT message,

276
00:22:12,380 --> 00:22:15,089
I can either include

277
00:22:15,089 --> 00:22:17,680
the updated rating

278
00:22:21,830 --> 00:22:28,110
or the updated comment.

279
00:22:28,110 --> 00:22:33,475
These are the only two things that I will allow the user to change.

280
00:22:33,475 --> 00:22:40,190
So if the if the rating property ies indirect body to call that the body

281
00:22:40,190 --> 00:22:47,940
will contain the update that we are trying to do so in this case we will say dish.

282
00:22:48,240 --> 00:22:51,460
So we'll say dish comments,

283
00:22:51,460 --> 00:22:54,000
so let me just copy this part.

284
00:22:54,000 --> 00:22:56,980
Dish comment comment Id.

285
00:22:56,980 --> 00:23:02,770
Now this is a simple way of handling this issue here.

286
00:23:05,950 --> 00:23:11,525
Because there is no specific way of updating

287
00:23:11,525 --> 00:23:16,655
a specific comment which is in a sub-document,

288
00:23:16,655 --> 00:23:19,330
there is no specific method for doing this.

289
00:23:19,330 --> 00:23:24,950
So this is the workaround that I found that seems to work for us,

290
00:23:24,950 --> 00:23:32,820
so we'll say dish comments req parameter comment Id rating equal to req body.rating.

291
00:23:34,700 --> 00:23:37,640
So I will update the rating.

292
00:23:37,640 --> 00:23:42,420
Similarly, if the comment exists there,

293
00:23:42,420 --> 00:23:51,995
then I will say req comment Id comment req body comment.

294
00:23:51,995 --> 00:23:57,655
So, that is how I'm going to be updating the rating and the comment,

295
00:23:57,655 --> 00:24:00,230
and I once say about updated the rating and the comment,

296
00:24:00,230 --> 00:24:02,155
and they will save the dish,

297
00:24:02,155 --> 00:24:08,140
and then once I save the dish then I will send back the reply.

298
00:24:08,140 --> 00:24:15,120
So, note that this is the only way that I found which is easy enough

299
00:24:15,120 --> 00:24:22,785
to update a embedded sub-document inside a document of Mongoose.

300
00:24:22,785 --> 00:24:28,520
There is no explicit way that Mongoose supports for updating an embedded document.

301
00:24:28,520 --> 00:24:34,275
So this is the workaround that I found that enables us to carry out this operation.

302
00:24:34,275 --> 00:24:38,825
Now for the delete operation.

303
00:24:38,825 --> 00:24:43,940
For the delete operation we are deleting only a specific-

304
00:24:43,940 --> 00:24:48,880
Comment, so I will take this information from

305
00:24:48,880 --> 00:24:53,915
the deleter for all the comments

306
00:24:53,915 --> 00:24:55,880
and instead of deleting all the comments

307
00:24:55,880 --> 00:24:58,190
I'm only going to be deleting a specific comment.

308
00:24:58,190 --> 00:25:03,095
So, let me copy that over from the delete.

309
00:25:03,095 --> 00:25:07,920
Let me paste it in here and then we will update this.

310
00:25:07,920 --> 00:25:13,910
So, if I am deleting a specific comment so I'll first find the dish.

311
00:25:13,910 --> 00:25:17,240
If the dish is not null,

312
00:25:17,240 --> 00:25:21,480
recall that in this situation I have to handle both the dish

313
00:25:21,480 --> 00:25:26,395
not being null and also the comment not being null.

314
00:25:26,395 --> 00:25:29,150
So, I'll say if

315
00:25:32,890 --> 00:25:39,840
dish not equal to null and dish comments id parameter not equal to null,

316
00:25:39,840 --> 00:25:44,335
then I know that I have a specific comment that I want to delete.

317
00:25:44,335 --> 00:25:45,850
I'm not deleting all the comments,

318
00:25:45,850 --> 00:25:49,770
I am deleting only a specific comment here.

319
00:25:49,770 --> 00:25:58,700
So, I'll say dish comments id and then I am deleting the specific comment here which

320
00:25:58,700 --> 00:26:07,780
is req.params.commentId and remove

321
00:26:07,780 --> 00:26:14,565
that particular comment and then I will save the updated dish and then move on with that.

322
00:26:14,565 --> 00:26:19,225
Now, the else part I need to handle both the situations.

323
00:26:19,225 --> 00:26:23,180
So, let me copy this part else if dish not equal to null and

324
00:26:23,180 --> 00:26:28,420
so on and then replace this here.

325
00:26:28,420 --> 00:26:33,480
So, if the dish is null then I will send back the error saying the dish doesn't exist.

326
00:26:33,480 --> 00:26:37,640
If the comment is null then I'll send back the error saying comment doesn't exist.

327
00:26:37,640 --> 00:26:39,520
So, that is it,

328
00:26:39,520 --> 00:26:41,770
this is the update that I'm doing here.

329
00:26:41,770 --> 00:26:44,395
For deleting a specific comment,

330
00:26:44,395 --> 00:26:47,670
I will first ensure that the dish and the comment

331
00:26:47,670 --> 00:26:52,040
exist then I will delete the specific comment and then I will save

332
00:26:52,040 --> 00:26:54,520
the changes to the dish and then proceed

333
00:26:54,520 --> 00:27:00,315
on with handling the rest of the cases, that's it.

334
00:27:00,315 --> 00:27:05,310
Let's save the changes and then look at how this works.

335
00:27:05,310 --> 00:27:07,210
Going to the terminal,

336
00:27:07,210 --> 00:27:12,150
I see MongoDB server is not up and running so let's start there.

337
00:27:12,150 --> 00:27:14,000
Make sure, of course,

338
00:27:14,000 --> 00:27:16,075
that the MongoDB server is up and running,

339
00:27:16,075 --> 00:27:19,070
let's also start our express server.

340
00:27:19,890 --> 00:27:23,165
Once your express server is up and running,

341
00:27:23,165 --> 00:27:27,185
what we will do is we are now looking specifically at

342
00:27:27,185 --> 00:27:32,420
the comments and then performing operations on the comments.

343
00:27:32,420 --> 00:27:36,320
So, let's go to the Postman and

344
00:27:36,320 --> 00:27:44,540
then check out what we have in the database and then try to modify the comments.

345
00:27:44,540 --> 00:27:47,760
So, here I am in Postman,

346
00:27:47,760 --> 00:27:53,265
so let me first get the dishes and now I see that the dishes are all empty.

347
00:27:53,265 --> 00:28:02,385
So let me post a dish to list of dishes so let me post one dish.

348
00:28:02,385 --> 00:28:04,860
Recall that we have this dish her,

349
00:28:04,860 --> 00:28:11,540
so in my Postman I had already done that for the previous exercise.

350
00:28:11,540 --> 00:28:16,160
I'm just going to select that and then post that dish and then you

351
00:28:16,160 --> 00:28:21,205
see that that particular dish has been posted here, down below here.

352
00:28:21,205 --> 00:28:25,060
You can see that the dish has been created right there.

353
00:28:25,060 --> 00:28:33,970
Let me now copy that particular dish id and then I'm going to

354
00:28:33,970 --> 00:28:45,505
now get that dish here.

355
00:28:45,505 --> 00:28:49,920
So, you see that that particular dish exists here.

356
00:28:49,920 --> 00:28:52,575
First, let's get all the comments.

357
00:28:52,575 --> 00:28:55,480
So, if I do a get on the comments you notice that

358
00:28:55,480 --> 00:28:58,370
all the comments for that particular dish have been returned.

359
00:28:58,370 --> 00:29:02,195
We can do the post and the delete also.

360
00:29:02,195 --> 00:29:05,865
Let's do a put on that and obviously you can't do a put on

361
00:29:05,865 --> 00:29:09,960
back so you notice that the put operation doesn't work on that.

362
00:29:09,960 --> 00:29:13,110
Let's post a new comment.

363
00:29:13,110 --> 00:29:18,070
So to post a new comment we'll go into the body,

364
00:29:18,380 --> 00:29:28,990
and then let me cut out all these and then we'll edit one of

365
00:29:28,990 --> 00:29:31,130
the comments and then

366
00:29:35,790 --> 00:29:40,045
try to post that comment.

367
00:29:40,045 --> 00:29:45,130
So, we'll say rating three and then we'll

368
00:29:45,130 --> 00:29:56,110
say test comment, test user.

369
00:29:56,110 --> 00:29:58,425
Let's post this comment.

370
00:29:58,425 --> 00:30:00,190
When you post this comment,

371
00:30:00,190 --> 00:30:05,920
you see that the update dish has been returned here and you see that within

372
00:30:05,920 --> 00:30:14,530
this dish you see that the new comment has been included in there.

373
00:30:14,530 --> 00:30:20,335
So, let's copy this whole thing.

374
00:30:20,335 --> 00:30:27,515
Now, we will try to get that particular comment that we have just inserted.

375
00:30:27,515 --> 00:30:31,440
For that particular comment this is the id of that comment.

376
00:30:31,440 --> 00:30:35,180
So let me copy the id for that specific comment.

377
00:30:35,180 --> 00:30:38,545
Going back to Postman,

378
00:30:38,545 --> 00:30:43,915
let me try to retrieve that specific comment by doing a get operation.

379
00:30:43,915 --> 00:30:48,380
There you see that that particular comment has been retrieved here.

380
00:30:48,380 --> 00:30:52,050
Now, if you're try to do a post on that,that will not work

381
00:30:52,050 --> 00:30:56,445
so obviously you see that the post operation is not allowed on that.

382
00:30:56,445 --> 00:31:01,370
Let's do a put operation,

383
00:31:03,860 --> 00:31:06,385
on that particular comment.

384
00:31:06,385 --> 00:31:10,010
So, if I were to put operation in the body,

385
00:31:10,390 --> 00:31:18,285
I'm going to say updated test comment.

386
00:31:18,285 --> 00:31:23,200
So, I'm going to update the comment field of that comment.

387
00:31:23,200 --> 00:31:28,745
If you're saying to put on that you will see that for the specific comment,

388
00:31:28,745 --> 00:31:33,670
the comment has now been updated to reflect the updated test comment.

389
00:31:33,670 --> 00:31:39,515
So, you see that the put operation on the comments field also works correctly.

390
00:31:39,515 --> 00:31:42,820
Let's delete that comment,

391
00:31:42,820 --> 00:31:46,660
so I can delete the comment and then when I delete the comment you'll

392
00:31:46,660 --> 00:31:50,475
see that that comment has disappeared from the list of comments.

393
00:31:50,475 --> 00:31:53,915
So, you see that the delete operation also works.

394
00:31:53,915 --> 00:31:55,970
Let's delete all the comments.

395
00:31:55,970 --> 00:32:00,595
So I'm going to go in and delete all the comments.

396
00:32:00,595 --> 00:32:04,650
Then you see that all the comments have been deleted,

397
00:32:04,650 --> 00:32:09,380
and so you see that the comments area is now empty.

398
00:32:09,380 --> 00:32:13,050
So, now you can see them all the get, put, post,

399
00:32:13,050 --> 00:32:14,905
and delete operations on

400
00:32:14,905 --> 00:32:21,064
the dishes/dishId/ comments

401
00:32:21,064 --> 00:32:25,455
and the dishes/dishId/comments/commentId,

402
00:32:25,455 --> 00:32:29,710
REST API endpoints all work as expected.

403
00:32:29,710 --> 00:32:32,670
So with this we complete this exercise.

404
00:32:32,670 --> 00:32:38,365
So, in this exercise we have completed the implementation of the slash dishes,

405
00:32:38,365 --> 00:32:42,395
REST API endpoint and supported all the way up to

406
00:32:42,395 --> 00:32:47,070
allowing us to manipulate a single dish,

407
00:32:47,070 --> 00:32:48,730
the collection of dishes,

408
00:32:48,730 --> 00:32:52,165
a single comment and also the collection of comments.

409
00:32:52,165 --> 00:32:56,930
This is a good time for you to do a git commit with the message,

410
00:32:56,930 --> 00:33:02,600
express, REST API with Mongoose part two.