1
00:00:03,920 --> 00:00:09,645
As we have understood from the previous lecture in this lesson,

2
00:00:09,645 --> 00:00:12,585
our goal in this lesson is to

3
00:00:12,585 --> 00:00:16,590
integrate the REST API server that we have already developed,

4
00:00:16,590 --> 00:00:20,525
together with the access to the MongoDB database.

5
00:00:20,525 --> 00:00:24,060
So, we will start with the REST API server

6
00:00:24,060 --> 00:00:27,480
that we built in the very first lesson in this module,

7
00:00:27,480 --> 00:00:31,770
and then having learned how to interact from

8
00:00:31,770 --> 00:00:37,220
our node application to the MongoDB server using Mongoose,

9
00:00:37,220 --> 00:00:43,865
we're going to be developing our REST API server further to integrate

10
00:00:43,865 --> 00:00:50,749
the entire path between the client request coming into the server,

11
00:00:50,749 --> 00:00:53,840
all the way to the corresponding database operation to be performed,

12
00:00:53,840 --> 00:01:00,880
and then constructing and sending back the reply to that client from our server site.

13
00:01:00,880 --> 00:01:04,125
To get started of course, first,

14
00:01:04,125 --> 00:01:08,300
go to the confusion server folder that we have already created in

15
00:01:08,300 --> 00:01:15,215
the very first exercise of this module in the REST API lesson,

16
00:01:15,215 --> 00:01:17,915
and then, in the confusion folder,

17
00:01:17,915 --> 00:01:22,310
we have already built up the REST API server.

18
00:01:22,310 --> 00:01:26,630
Now, what we would be doing is to borrow

19
00:01:26,630 --> 00:01:32,035
the models that we developed in the previous exercise,

20
00:01:32,035 --> 00:01:36,050
the dishes.js file that we developed in the previous exercise,

21
00:01:36,050 --> 00:01:41,220
copied over to the confusion server project,

22
00:01:41,220 --> 00:01:44,430
and also install Bluebird Mongoose,

23
00:01:44,430 --> 00:01:51,225
and another module called as Mongoose currency, to our project.

24
00:01:51,225 --> 00:01:54,275
So, going to our node JS folder,

25
00:01:54,275 --> 00:01:57,050
we first go into the node Mongoose folder,

26
00:01:57,050 --> 00:02:01,970
and we see that in the models sub-folder of the node Mongoose folder,

27
00:02:01,970 --> 00:02:03,810
we have the dishes.js file.

28
00:02:03,810 --> 00:02:07,354
I'm just going to copy the models folder,

29
00:02:07,354 --> 00:02:10,490
and then go over to the confusion server folder,

30
00:02:10,490 --> 00:02:13,910
and then simply pierce the models folder in there.

31
00:02:13,910 --> 00:02:15,690
So, once we do that,

32
00:02:15,690 --> 00:02:22,540
then the dishes.js file which contains the schema and the model for the dishes document,

33
00:02:22,540 --> 00:02:28,155
are integrated into our REST API server.

34
00:02:28,155 --> 00:02:30,890
Of course, in order to make use of that,

35
00:02:30,890 --> 00:02:34,400
we need to install the Mongoose node module,

36
00:02:34,400 --> 00:02:40,990
and a new node module called as Mongoose currency into our project.

37
00:02:40,990 --> 00:02:47,510
So, going to the terminal in the confusion server project,

38
00:02:47,510 --> 00:02:52,640
make sure that your terminal or the command window is

39
00:02:52,640 --> 00:02:57,480
in the confusion server project where you develop the REST API earlier,

40
00:02:57,480 --> 00:03:01,070
and in this project, let's install.

41
00:03:01,070 --> 00:03:06,165
So, we'll do npm install Mongoose,

42
00:03:06,165 --> 00:03:13,110
and then, a new node module called as Mongoose currency.

43
00:03:13,250 --> 00:03:16,630
The Mongoose currency node module,

44
00:03:16,630 --> 00:03:21,800
and seen in other Schema type to our Mongoose application,

45
00:03:21,800 --> 00:03:23,770
so the Mongoose itself,

46
00:03:23,770 --> 00:03:26,390
has certainly already built-in Schema types.

47
00:03:26,390 --> 00:03:29,505
We have seen the use of the number,

48
00:03:29,505 --> 00:03:35,550
the string and the boolean, and the array.

49
00:03:35,550 --> 00:03:39,760
Now, the Mongoose currency adds in support for currency.

50
00:03:39,760 --> 00:03:42,385
Now, why would we need this currency support?

51
00:03:42,385 --> 00:03:49,040
So that Mongoose currency module adds a new type called as the currency type,

52
00:03:49,040 --> 00:03:53,140
which enables us to store a currency value.

53
00:03:53,140 --> 00:03:56,650
Since our dish is going to contain a price,

54
00:03:56,650 --> 00:04:00,760
that is why I'm going to be using the Mongoose currency module here.

55
00:04:00,760 --> 00:04:04,590
Now, the exercise here,

56
00:04:04,590 --> 00:04:07,545
we will illustrate the use of the Mongoose currency module,

57
00:04:07,545 --> 00:04:11,880
you can read more details about the Mongoose currency node module also,

58
00:04:11,880 --> 00:04:19,010
in the documentation of that a link to which is provided in the additional resources.

59
00:04:19,010 --> 00:04:22,489
So, now that we have installed these node modules,

60
00:04:22,489 --> 00:04:24,890
Mongoose and Mongoose currency,

61
00:04:24,890 --> 00:04:33,920
let's go to our application and set it up to communicate with the MongoDB server.

62
00:04:33,920 --> 00:04:37,570
Now, make sure that your MongoDB server is up and running.

63
00:04:37,570 --> 00:04:40,070
So, here, you see that

64
00:04:40,070 --> 00:04:44,730
my MongoDB server is running in another terminal tab on my computer.

65
00:04:44,730 --> 00:04:46,670
If you're running it on a Windows machine,

66
00:04:46,670 --> 00:04:54,005
make sure it is running in another command Window of your Windows computer.

67
00:04:54,005 --> 00:04:58,525
Going to our application in the editor,

68
00:04:58,525 --> 00:05:01,685
we will first start with the app.js file.

69
00:05:01,685 --> 00:05:03,460
Now in the app.js file,

70
00:05:03,460 --> 00:05:07,590
this is where we built up our express application earlier.

71
00:05:07,590 --> 00:05:12,935
But now, this extra suck negation is not connected to the back-end MongoDB server.

72
00:05:12,935 --> 00:05:18,085
We're going to be making use of the Mongoose module,

73
00:05:18,085 --> 00:05:20,430
in order to establish the connection with the server.

74
00:05:20,430 --> 00:05:22,385
So, going in here,

75
00:05:22,385 --> 00:05:29,540
I'm going to add in the require the Mongoose module here.

76
00:05:29,540 --> 00:05:35,850
So, we'll say, "Const Mongoose require Mongoose."

77
00:05:38,120 --> 00:05:43,280
And then also, since we have copied over the models folder,

78
00:05:43,280 --> 00:05:46,520
which contains the dishes file,

79
00:05:46,520 --> 00:05:51,220
which declares the dishes schema and the model.

80
00:05:51,220 --> 00:05:56,645
So, let me import the dishes.

81
00:05:56,645 --> 00:06:04,930
So, we'll say, "Require.slash models dishes."

82
00:06:04,930 --> 00:06:07,640
So, once we have completed that, now of course,

83
00:06:07,640 --> 00:06:11,000
we need to establish the connection with the server.

84
00:06:11,000 --> 00:06:13,560
So, set up

85
00:06:14,120 --> 00:06:24,960
URL mongodb// localhost7017/confusion,

86
00:06:27,340 --> 00:06:31,150
just like we did with the Mongoose exercise,

87
00:06:31,150 --> 00:06:33,140
and then we will say,

88
00:06:33,140 --> 00:06:44,040
"Const connect, Mongo's connect URL".

89
00:06:44,040 --> 00:06:51,450
So, this is exactly the same code that we used in the previous exercise.

90
00:06:51,450 --> 00:06:53,640
Then, let's establish the connection.

91
00:06:53,640 --> 00:07:01,695
So, we will say, ''connect'' and then we'll say,

92
00:07:01,695 --> 00:07:11,610
''DB do it console log.''

93
00:07:11,610 --> 00:07:19,030
Saying, ''Connected correctly to the server.''

94
00:07:21,020 --> 00:07:26,260
And we'll also handle the error here.

95
00:07:33,980 --> 00:07:40,855
We'll just simply do a console log of the error here, that's it.

96
00:07:40,855 --> 00:07:49,110
That will establish the connection to the server from our app.js file.

97
00:07:49,110 --> 00:07:52,375
So, once we have established the connection to the server,

98
00:07:52,375 --> 00:07:58,615
then, let's open the dishes.js file from our models.

99
00:07:58,615 --> 00:08:00,995
Now, in the dishes.js file,

100
00:08:00,995 --> 00:08:02,970
to make use of

101
00:08:02,970 --> 00:08:11,760
the node module that we have just installed.

102
00:08:11,760 --> 00:08:18,990
So, we'll say, ''Require Mongoose currency,'' and say,

103
00:08:18,990 --> 00:08:26,500
''Load the type and Mongoose.''

104
00:08:26,500 --> 00:08:32,670
So, what this will do is to load this new currency type into Mongoose.

105
00:08:32,670 --> 00:08:39,970
Thereafter, we can say const,

106
00:08:39,970 --> 00:08:48,705
currency Mongoose types currency.

107
00:08:48,705 --> 00:08:51,720
That's it. So, this new type,

108
00:08:51,720 --> 00:08:57,880
the currency type is added into Mongoose and that will add in a new type called

109
00:08:57,880 --> 00:09:00,160
currency and then so I'm going to declare

110
00:09:00,160 --> 00:09:04,840
this constant currency as the Mongoose's types currency.

111
00:09:04,840 --> 00:09:11,630
So that I can make use of this in defining the schema in my application.

112
00:09:11,630 --> 00:09:14,270
Now, in this case,

113
00:09:14,270 --> 00:09:17,970
the common schema will remain exactly the same as before but

114
00:09:17,970 --> 00:09:24,060
the dish schema as you recall from the db.json file.

115
00:09:24,060 --> 00:09:28,050
When you look at the structure of a dish document,

116
00:09:28,050 --> 00:09:35,630
you see that the dish document contains name and image which as you see here is a string,

117
00:09:35,630 --> 00:09:38,570
a category, a label,

118
00:09:38,570 --> 00:09:42,555
a price, which is a string type here.

119
00:09:42,555 --> 00:09:46,865
But we will declare this as a currency type,

120
00:09:46,865 --> 00:09:51,495
a feature which as you expect is a Boolean variable,

121
00:09:51,495 --> 00:09:55,015
and a description which is a string and then comments

122
00:09:55,015 --> 00:09:59,755
which is nothing but an array of comments type.

123
00:09:59,755 --> 00:10:05,790
Now, so what we will now do is to extend the dish schema to

124
00:10:05,790 --> 00:10:12,865
support all these various properties or various fields in my json document.

125
00:10:12,865 --> 00:10:15,225
So we already have the name.

126
00:10:15,225 --> 00:10:18,505
So we already have the description in place.

127
00:10:18,505 --> 00:10:23,730
So we need to add in the next few in there we already have the comments,

128
00:10:23,730 --> 00:10:27,185
the array of comments of the comment schema type there.

129
00:10:27,185 --> 00:10:30,075
So we will add in the next few.

130
00:10:30,075 --> 00:10:34,150
So the next one we will add in is the image type,

131
00:10:34,150 --> 00:10:37,640
which would be of

132
00:10:37,640 --> 00:10:44,300
the type string and

133
00:10:44,300 --> 00:10:49,140
we'll say required true.

134
00:10:49,200 --> 00:10:52,240
So this adds the image type.

135
00:10:52,240 --> 00:11:00,460
The next one that I will add is category,

136
00:11:00,460 --> 00:11:03,865
which is also the string type.

137
00:11:03,865 --> 00:11:07,990
The next one is label,

138
00:11:07,990 --> 00:11:10,720
which is also this string type.

139
00:11:10,720 --> 00:11:14,560
Since all these are of the same type and required,

140
00:11:14,560 --> 00:11:16,625
I'm just copying them in here.

141
00:11:16,625 --> 00:11:18,464
Then for the label,

142
00:11:18,464 --> 00:11:21,275
I would say that this is not required but

143
00:11:21,275 --> 00:11:26,945
instead I can also specify a default value if I want.

144
00:11:26,945 --> 00:11:29,465
So I can specify a default value like that.

145
00:11:29,465 --> 00:11:31,965
Default value is an empty string.

146
00:11:31,965 --> 00:11:37,245
So, if I don't specify required I can simply specify a default value here.

147
00:11:37,245 --> 00:11:46,300
Now, in addition, the next field that I'm going to introduce is the price field.

148
00:11:47,070 --> 00:11:53,575
The price field I will declare the type as currency.

149
00:11:53,575 --> 00:11:57,085
Recall that we had declared the currency type earlier

150
00:11:57,085 --> 00:12:00,380
here by first requiring

151
00:12:00,380 --> 00:12:03,960
the Mongoose currency module and then declaring the currency type.

152
00:12:03,960 --> 00:12:09,435
So, that is how you would use the currency type in our application.

153
00:12:09,435 --> 00:12:16,170
So we'll say price type currency and

154
00:12:16,170 --> 00:12:24,610
required is true and then I can also specify the minimum value which would be zero.

155
00:12:24,610 --> 00:12:29,590
Then the next field is

156
00:12:29,590 --> 00:12:35,830
the featured field which would be of the type Boolean,

157
00:12:35,830 --> 00:12:40,900
and the default value will be false.

158
00:12:40,900 --> 00:12:43,625
So, if my document is missing that,

159
00:12:43,625 --> 00:12:47,940
then the default value will be added into the document here.

160
00:12:47,940 --> 00:12:50,470
So notice that I have now expanded

161
00:12:50,470 --> 00:12:56,825
the dish schema by adding in the image type, the category,

162
00:12:56,825 --> 00:13:01,710
the label, the price and the feature to match

163
00:13:01,710 --> 00:13:07,925
the structure of the dish document example that I just showed you earlier.

164
00:13:07,925 --> 00:13:13,795
So now, my dishes schema is all ready to be used.

165
00:13:13,795 --> 00:13:20,185
So, let's now start working on my router.

166
00:13:20,185 --> 00:13:21,915
So where is the router?

167
00:13:21,915 --> 00:13:29,530
You recall that the router that supports the REST API endpoints for the slash dishes,

168
00:13:29,530 --> 00:13:32,110
REST API endpoint and slash dishes,

169
00:13:32,110 --> 00:13:35,170
slash dish ID endpoint is in the dish router.

170
00:13:35,170 --> 00:13:41,295
So, we will go to dish router.jsfile and then we will extend the dish router.js file.

171
00:13:41,295 --> 00:13:46,735
So, in the dish router along with Express and bodyParser,

172
00:13:46,735 --> 00:13:55,310
I'm going to now include Mongoose.

173
00:13:56,640 --> 00:14:03,370
So we'll say require Mongoose and

174
00:14:03,370 --> 00:14:10,944
then we will require dishes model.

175
00:14:10,944 --> 00:14:12,400
Where is the dishes model?

176
00:14:12,400 --> 00:14:20,080
It is in./models/dishes.

177
00:14:20,080 --> 00:14:22,405
So it is in there.

178
00:14:22,405 --> 00:14:24,470
So notice that we are in the router's folder,

179
00:14:24,470 --> 00:14:27,610
so you need to go up one level and then go into

180
00:14:27,610 --> 00:14:31,460
the model's folder and then the dishes.js file is right there.

181
00:14:31,460 --> 00:14:34,010
So that is what we are importing here.

182
00:14:34,010 --> 00:14:41,039
So now, I can update my dish router to be able to interact

183
00:14:41,039 --> 00:14:46,330
with the MongoDB server using

184
00:14:46,330 --> 00:14:52,175
Mongoose and we have already imported the dishes model into my dish router.

185
00:14:52,175 --> 00:14:57,720
So, it is time for me to go and update all the methods in here.

186
00:14:57,720 --> 00:14:59,465
So for the dish router,

187
00:14:59,465 --> 00:15:03,665
the slash which means that the slash dishes endpoint.

188
00:15:03,665 --> 00:15:07,880
I'm going to remove this all from here, instead,

189
00:15:07,880 --> 00:15:12,040
I'm going to explicitly declare all the various endpoints.

190
00:15:12,040 --> 00:15:16,995
To get post put and delete I will handle each one of them independently.

191
00:15:16,995 --> 00:15:19,135
So in the get method,

192
00:15:19,135 --> 00:15:24,360
I'm going to cut that out and then in the get method, what do I need to do?

193
00:15:24,360 --> 00:15:32,760
Recall that we had defined method from Mongoose which allows us to find all the dishes.

194
00:15:32,760 --> 00:15:36,365
So when you do a get operation on the slash dishes endpoint,

195
00:15:36,365 --> 00:15:39,600
you're expecting all the dishes to be returned

196
00:15:39,600 --> 00:15:44,005
to the client in response to the get request.

197
00:15:44,005 --> 00:15:49,890
So, I'm going to go to dishes and then perform the find operation.

198
00:15:49,890 --> 00:15:53,040
So now you see that from my Express server,

199
00:15:53,040 --> 00:15:58,585
I am accessing my MongoDB.

200
00:15:58,585 --> 00:16:06,520
So, will do a find and in the find I'm going to now handle the request.

201
00:16:06,520 --> 00:16:08,310
So I can say dishes find,

202
00:16:08,310 --> 00:16:13,885
since that is going to return a promise,

203
00:16:13,885 --> 00:16:16,765
then I can handle that inside here.

204
00:16:16,765 --> 00:16:24,520
So, I'll say dish and so if the promise resolves correctly,

205
00:16:24,520 --> 00:16:33,529
I'll get it in the then and so I'll say dish and then so we'll handle res

206
00:16:33,960 --> 00:16:41,125
status code is 200 and then we'll say

207
00:16:41,125 --> 00:16:48,920
res set header content type.

208
00:16:53,100 --> 00:16:57,830
Since we are going to be returning the value as a json,

209
00:16:57,830 --> 00:17:00,770
so we'll set that to application json.

210
00:17:00,770 --> 00:17:03,580
Okay, this will return an array of dishes.

211
00:17:03,580 --> 00:17:07,955
So I can simply say dishes and then we'll say res.json.

212
00:17:07,955 --> 00:17:12,650
So the res.json will take as an input in json string

213
00:17:12,650 --> 00:17:17,680
and then send it back over to my client.

214
00:17:17,680 --> 00:17:21,785
So, when you call res.json and supply the value and then it will simply

215
00:17:21,785 --> 00:17:27,650
take the parameter that you give here and then send it back as a json response.

216
00:17:27,650 --> 00:17:30,365
It will put this dishes into the body

217
00:17:30,365 --> 00:17:33,835
of the reply message and then send it back to the server.

218
00:17:33,835 --> 00:17:39,560
Now, we can handle the error

219
00:17:39,560 --> 00:17:47,370
here by saying next error.

220
00:17:48,100 --> 00:17:59,140
We can also do a catch error just for the sake of complete.

221
00:17:59,140 --> 00:18:03,290
I am just going to put both of these into place here,

222
00:18:03,290 --> 00:18:05,960
so that both will be handled as such.

223
00:18:05,960 --> 00:18:08,305
So if an error is returned,

224
00:18:08,305 --> 00:18:11,740
then that'll simply pass off the error to

225
00:18:11,740 --> 00:18:15,260
the overall error handler for my application

226
00:18:15,260 --> 00:18:18,985
and the let that worry about how to handle the error.

227
00:18:18,985 --> 00:18:21,490
So we're going to be sending it over to that.

228
00:18:21,490 --> 00:18:28,610
So you see how I am using the find operation and then performing the request here.

229
00:18:28,610 --> 00:18:30,595
Now, for the post,

230
00:18:30,595 --> 00:18:32,904
as you would have already expected,

231
00:18:32,904 --> 00:18:37,100
I'm going to do dishes.create

232
00:18:38,790 --> 00:18:43,425
because we're going to be creating a new dish here.

233
00:18:43,425 --> 00:18:47,195
So, recall that we are already seen the dishes create

234
00:18:47,195 --> 00:18:52,400
method usage earlier and remember that the body parser would have

235
00:18:52,400 --> 00:18:56,950
already parsed whatever is in the body of the message and loaded it

236
00:18:56,950 --> 00:19:01,510
onto the body property of the request.

237
00:19:01,510 --> 00:19:08,650
So, I'm just going to take the request body and then parse it in as a parameter to my

238
00:19:08,650 --> 00:19:16,120
dishes.create method and handle the return value.

239
00:19:16,120 --> 00:19:21,325
So, we'll say then and this would return

240
00:19:21,325 --> 00:19:26,755
a dish and we'll handle that in here.

241
00:19:26,755 --> 00:19:35,220
So we'll say if the dishes return correctly and if dishes posted correctly, will say res.

242
00:19:36,060 --> 00:19:43,045
Okay let's do a console.log for our own usage.

243
00:19:43,045 --> 00:19:52,330
On the server-side we'll do a console.log saying "Dish Created" dish here.

244
00:19:52,330 --> 00:20:02,125
Let's log that dish to the console and then we'll say these two rest status code.

245
00:20:02,125 --> 00:20:09,820
We'll just copy that code and then paste it in there and in this case,

246
00:20:09,820 --> 00:20:12,445
we are returning the dish here.

247
00:20:12,445 --> 00:20:16,480
The dish that has come in as a parameter here and then let

248
00:20:16,480 --> 00:20:22,825
the client deal with that value on the client-side,

249
00:20:22,825 --> 00:20:24,985
whatever is returned in the dish.

250
00:20:24,985 --> 00:20:27,745
Now, also they will add in

251
00:20:27,745 --> 00:20:41,770
this here and then the catch.

252
00:20:41,770 --> 00:20:44,860
So this is how we handle the post.

253
00:20:44,860 --> 00:20:47,830
For the PUT, because PUT is not allowed,

254
00:20:47,830 --> 00:20:50,365
so we're going to leave it as such.

255
00:20:50,365 --> 00:20:54,250
For DELETE, we are going to be deleting all the dishes.

256
00:20:54,250 --> 00:21:02,240
So we'll say, "Dishes.remove."

257
00:21:03,990 --> 00:21:08,185
This is essentially a dangerous operation.

258
00:21:08,185 --> 00:21:11,080
So you're removing all the dishes from

259
00:21:11,080 --> 00:21:18,610
the server and so we will say,

260
00:21:18,610 --> 00:21:25,600
"Dishes.remove then" and the "then" will get some response.

261
00:21:25,600 --> 00:21:32,200
So we'll just say, "resp here" and the way we would handle

262
00:21:32,200 --> 00:21:40,550
that response is simply to take that value and then return it to the client.

263
00:21:40,620 --> 00:21:48,550
So we'll say, "Res.statusCode 200 content type application json," and

264
00:21:48,550 --> 00:21:56,660
then we'll simply send the response back to the client and we will handle the error

265
00:22:06,000 --> 00:22:08,830
just like we did earlier.

266
00:22:08,830 --> 00:22:10,390
That is the DELETE operation.

267
00:22:10,390 --> 00:22:14,110
So you see that now we are doing the GET,

268
00:22:14,110 --> 00:22:17,545
POST, PUT and the DELETE operation.

269
00:22:17,545 --> 00:22:26,425
Now we're going to be continuing the same with the /dishId end point.

270
00:22:26,425 --> 00:22:28,270
So in this case,

271
00:22:28,270 --> 00:22:34,040
we are specifically motif getting a specific dish.

272
00:22:34,040 --> 00:22:39,480
We are going to be returning that specific dish value.

273
00:22:39,480 --> 00:22:41,445
So in the GET,

274
00:22:41,445 --> 00:22:51,275
what we do is we will say "Dishes.findById.

275
00:22:51,275 --> 00:22:57,965
So the findById is a method that is available from mongo's as well as the MongoDB driver.

276
00:22:57,965 --> 00:23:02,020
So we'll say," req.params.dishId."

277
00:23:03,600 --> 00:23:11,030
Recall that we already know that the dish ID is present in the params property.

278
00:23:11,030 --> 00:23:14,140
You have already learned about this earlier.

279
00:23:14,140 --> 00:23:20,260
So I will say, "findById(req.params.dishId)" and

280
00:23:20,260 --> 00:23:24,565
then and the else.

281
00:23:24,565 --> 00:23:30,520
So I'm just going to copy that then and else from right

282
00:23:30,520 --> 00:23:38,170
there and then come down to the dishRouter and

283
00:23:38,170 --> 00:23:46,150
then simply paste that in here.

284
00:23:46,150 --> 00:23:49,190
So we'll say, " res.statusCode200

285
00:23:49,440 --> 00:23:55,585
application json.res.jsondish and then the error handling.

286
00:23:55,585 --> 00:24:05,350
For the POST, obviously we're not going to be handling the post for a /dishId endpoint.

287
00:24:05,350 --> 00:24:07,635
So we're going to be leaving it as such.

288
00:24:07,635 --> 00:24:12,740
For PUT, we are going to be updating

289
00:24:12,740 --> 00:24:17,975
a specific dish which is identified by its dish ID.

290
00:24:17,975 --> 00:24:25,270
So this is where we will use dishes.findByIdAndUpdate.

291
00:24:25,270 --> 00:24:27,690
So this is the method that we're going to be using,

292
00:24:27,690 --> 00:24:35,539
findbyIDAndUpdate and this takes as the first parameter

293
00:24:37,410 --> 00:24:44,410
req.params.dishId and the second value is

294
00:24:44,410 --> 00:24:53,290
the set and the update

295
00:24:53,290 --> 00:24:55,150
will be in the body of the message.

296
00:24:55,150 --> 00:24:57,580
So I'm just going to retrieve that from

297
00:24:57,580 --> 00:25:05,410
the req body and then also the other flag that I'm going to have.

298
00:25:05,410 --> 00:25:10,840
So will say, "new: true," so that this findbyId method will return

299
00:25:10,840 --> 00:25:18,730
the updated dish as a json string in the reply.

300
00:25:18,730 --> 00:25:23,650
So that is what I'm going to be getting here and then when that value comes in,

301
00:25:23,650 --> 00:25:26,230
I'm just going to take the dish and

302
00:25:26,230 --> 00:25:39,040
then simply return the dish to the client-side.

303
00:25:39,040 --> 00:25:47,095
So I'll say res.jason(dish) and then we'll handle the error correspondingly.

304
00:25:47,095 --> 00:25:49,090
Finally for the DELETE.

305
00:25:49,090 --> 00:25:50,695
For the DELETE again,

306
00:25:50,695 --> 00:25:54,880
the corresponding method that we are going to be

307
00:25:54,880 --> 00:26:01,165
using is the Mongo's method called findbyIdAndRemove.

308
00:26:01,165 --> 00:26:03,760
So you can see that we have this method called

309
00:26:03,760 --> 00:26:08,080
findbyIdAndRemove and this findbyIDandremove,

310
00:26:08,080 --> 00:26:18,355
will take req.params.dishId because that's the dish that we are trying to remove.

311
00:26:18,355 --> 00:26:20,970
Then when this is deleted,

312
00:26:20,970 --> 00:26:23,940
so just like we handled this here,

313
00:26:23,940 --> 00:26:30,830
so I'm just going to copy this code from the Dishes.remove.

314
00:26:30,830 --> 00:26:34,350
Is the same thing that I'm going to be doing here also.

315
00:26:34,350 --> 00:26:38,750
So findbyIdAndRemove and whatever response I get,

316
00:26:38,750 --> 00:26:41,970
I'm going to return it to my client.

317
00:26:41,970 --> 00:26:45,490
With this we have updated the dishRouter.

318
00:26:45,490 --> 00:26:49,960
Let's save all the changes that we have done so

319
00:26:49,960 --> 00:26:57,185
far and then we'll go and start up our server and then see what it does.

320
00:26:57,185 --> 00:27:01,370
So going to the terminal or the command window, start the server.

321
00:27:01,370 --> 00:27:06,045
So I'll say "npm start" and the server is now up and running.

322
00:27:06,045 --> 00:27:12,030
We're going to be using the postman to communicate with this server.

323
00:27:12,030 --> 00:27:15,700
So let's go to postman and then perform certain operations.

324
00:27:15,700 --> 00:27:19,030
So here you see my postman up and running here.

325
00:27:19,030 --> 00:27:26,450
So let me do a GET operation on the localhost:3000/dishes.

326
00:27:28,300 --> 00:27:31,875
So when you do a GET operation as you see,

327
00:27:31,875 --> 00:27:33,205
it'll return an empty string.

328
00:27:33,205 --> 00:27:36,715
My database is now empty so I don't have anything there.

329
00:27:36,715 --> 00:27:40,205
So I'm just going to be returned an empty string.

330
00:27:40,205 --> 00:27:45,600
Let's post a dish.

331
00:27:46,020 --> 00:27:48,340
So when you post a dish,

332
00:27:48,340 --> 00:27:50,254
obviously in the body,

333
00:27:50,254 --> 00:27:58,175
you will be enclosing a dish and the body will be set to be the application json type.

334
00:27:58,175 --> 00:28:00,785
Now, to post a dish,

335
00:28:00,785 --> 00:28:06,770
I have already given you the db.json file in the exercise resources.

336
00:28:06,770 --> 00:28:10,880
So just open the db.json file and then copy the very first dish from

337
00:28:10,880 --> 00:28:15,390
there and then we'll paste it in here and then post that dish.

338
00:28:15,390 --> 00:28:18,735
So, let me go to the db.jason file.

339
00:28:18,735 --> 00:28:21,550
Let me copy the very first dish from here.

340
00:28:21,550 --> 00:28:22,810
So I am just going to copy

341
00:28:22,810 --> 00:28:32,765
the entire dish all the way up to there and then I'm going to post this dish.

342
00:28:32,765 --> 00:28:36,610
This contains a lot of the fields that we have here already.

343
00:28:36,610 --> 00:28:39,895
Let's post this dish to the server and see what happens.

344
00:28:39,895 --> 00:28:44,605
So coming back to the postman.

345
00:28:44,605 --> 00:28:47,620
In here, in the form data,

346
00:28:47,620 --> 00:28:51,535
in the body, let me paste the dish into place.

347
00:28:51,535 --> 00:28:54,760
So, we have the full details of the dish there.

348
00:28:54,760 --> 00:28:58,010
Let's POST this dish to the server.

349
00:28:58,320 --> 00:29:01,780
Then, once the dish is posted to the server,

350
00:29:01,780 --> 00:29:06,580
you see that the Postman has,

351
00:29:06,580 --> 00:29:09,700
let me just shrink this,

352
00:29:09,700 --> 00:29:18,370
and then you see at the bottom that this particular dish has been posted to the database,

353
00:29:18,370 --> 00:29:20,290
to the MongoDB database by my server.

354
00:29:20,290 --> 00:29:23,560
So, you see that the returned value

355
00:29:23,560 --> 00:29:30,760
here shows when the dish was inserted into that server.

356
00:29:30,760 --> 00:29:35,020
So, you have the createdAt and updatedAt added there.

357
00:29:35,020 --> 00:29:39,880
You see that the remaining fields are all stored there.

358
00:29:39,880 --> 00:29:45,865
Note particularly how the price value is being stored there.

359
00:29:45,865 --> 00:29:49,820
This is how currency stores the price value.

360
00:29:50,630 --> 00:29:53,825
So, when you get the return value,

361
00:29:53,825 --> 00:29:59,370
you need to interpret this appropriately on your client side what that means.

362
00:29:59,370 --> 00:30:08,510
Note also that the ID has been added to my dish and for each comment itself,

363
00:30:08,510 --> 00:30:11,445
because each of the comments is itself is a sub-document.

364
00:30:11,445 --> 00:30:14,370
You'll have the updatedAt and createdAt added,

365
00:30:14,370 --> 00:30:21,115
and the ID for each of the comments also added in there automatically by my database.

366
00:30:21,115 --> 00:30:23,080
There you go. So now,

367
00:30:23,080 --> 00:30:26,875
this dish has been added to my database.

368
00:30:26,875 --> 00:30:29,589
Let's again perform the GET operation,

369
00:30:29,589 --> 00:30:32,244
and obviously at this point,

370
00:30:32,244 --> 00:30:37,135
the server should return that one particular dish that has been added in.

371
00:30:37,135 --> 00:30:39,775
So, it will return an array of dishes here,

372
00:30:39,775 --> 00:30:42,190
so as you can see, it returns an array of dishes.

373
00:30:42,190 --> 00:30:44,050
Of course, this array contains only one dish

374
00:30:44,050 --> 00:30:47,245
or that particular dish has been returned here.

375
00:30:47,245 --> 00:30:49,585
So far, so good.

376
00:30:49,585 --> 00:30:54,895
So, let's do a PUT on the dishes and see what happens.

377
00:30:54,895 --> 00:30:56,980
When you do a PUT, obviously, it says,

378
00:30:56,980 --> 00:31:02,255
"PUT operation not supported on dishes", as we expect.

379
00:31:02,255 --> 00:31:03,770
Let's do a DELETE.

380
00:31:03,770 --> 00:31:05,575
Doing a DELETE operation,

381
00:31:05,575 --> 00:31:09,030
it returns this response saying,

382
00:31:09,030 --> 00:31:10,890
"N is equal to one", okay,

383
00:31:10,890 --> 00:31:13,570
one meaning that it has deleted one dish.

384
00:31:13,570 --> 00:31:15,660
Let's now again, perform a GET operation,

385
00:31:15,660 --> 00:31:22,850
and then you would see that my dishes are empty as we expected.

386
00:31:22,980 --> 00:31:25,930
So, you see that the GET, PUT, POST,

387
00:31:25,930 --> 00:31:28,405
and DELETE operations are all working correctly.

388
00:31:28,405 --> 00:31:31,225
Now, let me POST the dish again

389
00:31:31,225 --> 00:31:36,110
to the server because I want to have one dish in the server.

390
00:31:36,270 --> 00:31:38,725
So, let me POST that dish,

391
00:31:38,725 --> 00:31:41,425
and you would notice that the ID has now changed.

392
00:31:41,425 --> 00:31:44,110
So, let me select that ID,

393
00:31:44,110 --> 00:31:50,630
and then we will do a GET with the ID in place.

394
00:31:51,990 --> 00:31:55,405
When you do a GET with the ID in place,

395
00:31:55,405 --> 00:32:02,300
you see that it returns that specific dish as you expect it.

396
00:32:02,760 --> 00:32:07,630
Let's go to the terminal and see what is

397
00:32:07,630 --> 00:32:12,125
being printed on the terminal or your command window.

398
00:32:12,125 --> 00:32:15,300
So, going to the terminal or your command window,

399
00:32:15,300 --> 00:32:19,360
you see that it is printing out all these things on the command window.

400
00:32:19,360 --> 00:32:21,540
So, when we did the first GET operation,

401
00:32:21,540 --> 00:32:22,640
it says, GET /dishes.

402
00:32:22,640 --> 00:32:24,375
So, this is again,

403
00:32:24,375 --> 00:32:28,155
Morgan doing this work for you, it is printing out,

404
00:32:28,155 --> 00:32:31,170
tracing this information and it says dish created

405
00:32:31,170 --> 00:32:34,575
and then that particular dish information has been printed,

406
00:32:34,575 --> 00:32:38,190
and then it says POST /dishes, GET /dishes,

407
00:32:38,190 --> 00:32:41,130
and then when you did a PUT it returned a 403

408
00:32:41,130 --> 00:32:44,170
there and you again created dishes and so on.

409
00:32:44,170 --> 00:32:47,145
So, you see that your server is actually doing all the work,

410
00:32:47,145 --> 00:32:53,110
and these things are getting inserted into your MongoDB database as you expected.

411
00:32:53,110 --> 00:32:58,540
Now, getting back to the Postman,

412
00:32:58,540 --> 00:33:00,850
let's do a POST on the dishes.

413
00:33:00,850 --> 00:33:03,605
Now, this is not supported on the server side,

414
00:33:03,605 --> 00:33:04,750
so your server should say,

415
00:33:04,750 --> 00:33:10,225
"POST operation is not supported on that particular endpoint," as you may expect it.

416
00:33:10,225 --> 00:33:13,070
Let's do a PUT operation.

417
00:33:14,430 --> 00:33:17,190
When you do a PUT operation,

418
00:33:17,190 --> 00:33:20,955
what I am going to do in the PUT operation is that I'm going to be

419
00:33:20,955 --> 00:33:26,860
replacing the label in there.

420
00:33:26,860 --> 00:33:29,550
So, in my body of the message.

421
00:33:29,550 --> 00:33:34,570
So, recall that if you look at the DB.json file,

422
00:33:34,570 --> 00:33:38,180
the label for that would be new,

423
00:33:38,180 --> 00:33:51,160
and so I'm going to change that label to hot.

424
00:33:51,160 --> 00:33:53,040
Since this needs to be in Json,

425
00:33:53,040 --> 00:33:57,895
so label also in quotes Json label hot,

426
00:33:57,895 --> 00:34:02,810
and then let's do a PUT up on that particular endpoint.

427
00:34:04,080 --> 00:34:07,615
The PUT operation was successful,

428
00:34:07,615 --> 00:34:12,280
and so you see that when the PUT operation was done,

429
00:34:12,280 --> 00:34:19,150
then you would notice that the label now has changed from new to hot here,

430
00:34:19,150 --> 00:34:22,630
and note in particular,

431
00:34:22,630 --> 00:34:28,900
createdAt value and the updatedAt value.

432
00:34:28,900 --> 00:34:31,990
So, notice that this record was created

433
00:34:31,990 --> 00:34:36,970
at this time point and was updated a little bit later.

434
00:34:36,970 --> 00:34:40,000
So, the update was done by the PUT operation that I just

435
00:34:40,000 --> 00:34:43,450
performed on that particular dish.

436
00:34:43,450 --> 00:34:46,240
Let's delete the dish.

437
00:34:46,240 --> 00:34:49,795
This is allowed. So, we'll delete the dish,

438
00:34:49,795 --> 00:34:54,100
and then the dish will be deleted and the value will be returned.

439
00:34:54,100 --> 00:34:58,930
Now, if you do a GET operation on

440
00:34:58,930 --> 00:35:04,615
the dishes endpoint,you will see that this will return an empty.

441
00:35:04,615 --> 00:35:09,385
So, you've just managed to delete the dish from our database.

442
00:35:09,385 --> 00:35:14,095
What I'm going to do is to perform

443
00:35:14,095 --> 00:35:20,630
a GET operation on a non-existent dish and see what happens.

444
00:35:20,630 --> 00:35:23,905
When I perform a GET operation on non-existent dish,

445
00:35:23,905 --> 00:35:27,300
it returns null because that dish doesn't exist.

446
00:35:27,300 --> 00:35:31,570
So, it returns a null value saying that the dish doesn't exist.

447
00:35:31,570 --> 00:35:40,525
Now, let me perform a GET operation on a non-ObjectID and see what happens.

448
00:35:40,525 --> 00:35:44,980
It returns as you see.

449
00:35:44,980 --> 00:35:46,465
Let me preview that.

450
00:35:46,465 --> 00:35:51,840
So, it says, "Cast to ObjectID failed for value here at path."

451
00:35:51,840 --> 00:35:53,840
So, I'll be obviously this is not

452
00:35:53,840 --> 00:35:57,565
a valid ObjectID so I just managed to delete part of it,

453
00:35:57,565 --> 00:36:02,050
and then perform the operation so it returns an error saying,

454
00:36:02,050 --> 00:36:04,915
so you see there's 500 internal server error.

455
00:36:04,915 --> 00:36:09,505
The server was not able to handle this and then return this value here.

456
00:36:09,505 --> 00:36:11,925
So, it says, "No,

457
00:36:11,925 --> 00:36:13,230
this is not allowed."

458
00:36:13,230 --> 00:36:18,385
So, because that is not a valid ObjectID.

459
00:36:18,385 --> 00:36:22,275
So, even errors are handled appropriately as you see here.

460
00:36:22,275 --> 00:36:26,050
So, let me again, do a GET operation on the dishes,

461
00:36:26,050 --> 00:36:27,975
and your server is still running,

462
00:36:27,975 --> 00:36:30,650
and it will return an empty value here.

463
00:36:30,650 --> 00:36:34,465
So, we have seen how by modifying

464
00:36:34,465 --> 00:36:40,900
our REST API server to be able to interact with the MongoDB server.

465
00:36:40,900 --> 00:36:45,775
We now have a full-fledged REST API server which is able to store,

466
00:36:45,775 --> 00:36:48,090
and retrieve, and perform various operations

467
00:36:48,090 --> 00:36:51,220
on the data that is stored on my MongoDB server.

468
00:36:51,220 --> 00:36:54,535
With this, we complete this exercise.

469
00:36:54,535 --> 00:36:56,290
So, in this exercise,

470
00:36:56,290 --> 00:37:05,200
we have seen how we are able to interact with our REST API server,

471
00:37:05,200 --> 00:37:07,560
and in turn, with the MongoDB server,

472
00:37:07,560 --> 00:37:12,400
and then we are leveraging the MongoDB server to store and retrieve data from the server.

473
00:37:12,400 --> 00:37:14,770
You're able to interact from

474
00:37:14,770 --> 00:37:20,225
our Express application with the MongoDB server using Mongoose.

475
00:37:20,225 --> 00:37:24,700
This is a good time for you to do a GIT commit with the message,

476
00:37:24,700 --> 00:37:31,550
"Express REST API with Mongoose Part One."