1
00:00:03,910 --> 00:00:09,095
It's time to get started on the fourth and the final assignment of this course.

2
00:00:09,095 --> 00:00:12,170
I have designed this assignment to be a consolidation

3
00:00:12,170 --> 00:00:15,610
of everything that you have learnt in this course.

4
00:00:15,610 --> 00:00:21,990
So this assignment will involve routing using the express router.

5
00:00:21,990 --> 00:00:26,340
It'll involve the design of schemas in the model,

6
00:00:26,340 --> 00:00:29,420
for our MongoDB and Mongoose,

7
00:00:29,420 --> 00:00:33,531
and then also it'll look briefly at user authentication.

8
00:00:33,531 --> 00:00:36,420
What exactly do we do in this assignment?

9
00:00:36,420 --> 00:00:41,600
This assignment is based around the ability for a user to

10
00:00:41,600 --> 00:00:49,510
save and retrieve a list of dishes as their favorite dishes on the server side.

11
00:00:49,510 --> 00:00:53,105
If you had taken the previous courses of this specialization,

12
00:00:53,105 --> 00:00:58,275
specifically the hybrid mobile app development courses,

13
00:00:58,275 --> 00:01:00,675
whether it is ionic or native script,

14
00:01:00,675 --> 00:01:06,720
you have seen that we have designed the apps to have the ability to allow the user to

15
00:01:06,720 --> 00:01:10,355
save dishes in the list of their favorite dishes

16
00:01:10,355 --> 00:01:14,480
and also retrieve dishes from that list of their favorite dishes.

17
00:01:14,480 --> 00:01:17,840
When we implemented that in the previous courses,

18
00:01:17,840 --> 00:01:24,085
we had implemented that using local storage on the specific device.

19
00:01:24,085 --> 00:01:26,330
In this assignment, you'll be extending

20
00:01:26,330 --> 00:01:30,308
the server side to be able to support exactly that.

21
00:01:30,308 --> 00:01:34,940
This way, the user can save their list of favorite dishes on

22
00:01:34,940 --> 00:01:37,805
the server side and access this list from

23
00:01:37,805 --> 00:01:41,950
any device from which they will log in to their server.

24
00:01:41,950 --> 00:01:49,140
What this means is that we need to design a new route for our server,

25
00:01:49,140 --> 00:01:52,615
which we will call as the /favorites route.

26
00:01:52,615 --> 00:01:59,314
On this route, we will have to design the express router to support the get,

27
00:01:59,314 --> 00:02:03,515
post and delete operations on this route and

28
00:02:03,515 --> 00:02:08,495
also design the schema to store the favorite dishes

29
00:02:08,495 --> 00:02:13,343
and the corresponding Mongoose model and

30
00:02:13,343 --> 00:02:19,820
connect the two together so that the information can be saved and retrieved from MongoDB.

31
00:02:19,820 --> 00:02:27,590
We will also use the Mongoose population in order to draw in the information from

32
00:02:27,590 --> 00:02:32,000
the user model and from the dishes model when we put

33
00:02:32,000 --> 00:02:37,105
together that list of favorite dishes for our user.

34
00:02:37,105 --> 00:02:41,355
The favorite dishes information that you store will not

35
00:02:41,355 --> 00:02:46,355
actually contain either the user information or the dish's information.

36
00:02:46,355 --> 00:02:53,000
Instead, it'll contain pointers to the documents for the dish document and for

37
00:02:53,000 --> 00:02:56,660
the user document and then we will publish this information

38
00:02:56,660 --> 00:03:03,155
when we are sending back this information in response to a get request.

39
00:03:03,155 --> 00:03:05,495
Let's look at how we implement

40
00:03:05,495 --> 00:03:13,135
this final assignment and also the various tasks in this final assignment.

41
00:03:13,135 --> 00:03:17,370
To help you to understand what is required in

42
00:03:17,370 --> 00:03:21,330
the final assignment or what you will be implementing in the final assignment,

43
00:03:21,330 --> 00:03:26,835
let me demonstrate what your server is expected to do at the end of this assignment,

44
00:03:26,835 --> 00:03:31,540
and then I will list out the three tasks that you need to perform in this assignment.

45
00:03:31,540 --> 00:03:35,460
To get started, we need a set of dishes already on

46
00:03:35,460 --> 00:03:40,250
the server side so that the user can mark some of them as their favorite dishes.

47
00:03:40,250 --> 00:03:47,805
I have gone ahead and added in four dishes to my list of dishes on the server side.

48
00:03:47,805 --> 00:03:50,065
You already know how to do this.

49
00:03:50,065 --> 00:03:52,725
You have to log in as the admin and then post

50
00:03:52,725 --> 00:03:56,670
this dish information to the server side one by one.

51
00:03:56,670 --> 00:04:01,590
To enable you get the information for each of these dishes,

52
00:04:01,590 --> 00:04:05,405
I have already provided you the db.json file which contains

53
00:04:05,405 --> 00:04:11,400
the JSON documents corresponding to each of these dishes in the assignment resources.

54
00:04:11,400 --> 00:04:14,670
You can open that, cut and paste information from there and then

55
00:04:14,670 --> 00:04:19,576
create a list of dishes on your server side.

56
00:04:19,576 --> 00:04:22,980
Here, you'll see that I have already performed the get operation on the

57
00:04:22,980 --> 00:04:26,940
localhost:3000/dishes and then you

58
00:04:26,940 --> 00:04:31,140
see that I have already obtained the list of dishes here.

59
00:04:31,140 --> 00:04:38,570
What is important for us to note is that each dish will have its own dish ID here.

60
00:04:38,570 --> 00:04:42,384
When you need to mark any dish as your favorite,

61
00:04:42,384 --> 00:04:46,470
you need access to the dish ID for each of these dishes.

62
00:04:46,470 --> 00:04:51,030
You can copy these four dish IDs and then save them so that when you

63
00:04:51,030 --> 00:04:56,094
perform the operations to add these dishes to the list of dishes,

64
00:04:56,094 --> 00:05:00,085
then you will be able to use this dish ID.

65
00:05:00,085 --> 00:05:02,730
So I'm going to copy these four dish IDs and then save

66
00:05:02,730 --> 00:05:05,820
them in a text document so that I can make use of them.

67
00:05:05,820 --> 00:05:09,325
Before I can add dishes to my list of dishes,

68
00:05:09,325 --> 00:05:12,347
I obviously need to log in as a user.

69
00:05:12,347 --> 00:05:16,370
Let me go ahead and log in as one of the users.

70
00:05:16,370 --> 00:05:25,245
To do that, let me do a post on the localhost:3000/users/login.

71
00:05:25,245 --> 00:05:28,530
And in the body of the message,

72
00:05:28,530 --> 00:05:35,940
let me type in one of the registered users.

73
00:05:35,940 --> 00:05:42,375
I don't want to log in as an admin but as one of the other normal users.

74
00:05:42,375 --> 00:05:47,442
So let me do this post to https://localhost:3443.

75
00:05:47,442 --> 00:05:48,890
Now all these operations,

76
00:05:48,890 --> 00:05:55,200
this is better to do it on the HTTPS endpoint rather than the HTTP endpoint.

77
00:05:55,200 --> 00:06:01,920
We'll do a post to this endpoint and log in our service,

78
00:06:01,920 --> 00:06:05,920
and in response, you should be able to get hold of the JSON web token.

79
00:06:05,920 --> 00:06:11,190
So let's make a copy of this JSON web token because we will need this in order to

80
00:06:11,190 --> 00:06:16,905
perform any of the operations to add dishes to our list of favorites.

81
00:06:16,905 --> 00:06:22,628
Let me copy this token and save it in my text document.

82
00:06:22,628 --> 00:06:27,935
Now, how do I add a dish to my list of favorites?

83
00:06:27,935 --> 00:06:33,450
To do that, the first step is that we have

84
00:06:33,450 --> 00:06:40,355
to access the /favorites endpoint.

85
00:06:40,355 --> 00:06:45,435
If you want to add a specific dish to the list of favorites,

86
00:06:45,435 --> 00:06:52,605
we just say /favorites and then add the dish ID into the URL here.

87
00:06:52,605 --> 00:06:57,970
Let me paste in a dish ID which I want to add to my list of favorites.

88
00:06:57,970 --> 00:07:03,130
Here, I have pasted in my dish ID to the list of favorites.

89
00:07:03,130 --> 00:07:08,025
The body of this host message will

90
00:07:08,025 --> 00:07:13,590
not contain anything because everything that I need is already in the URL.

91
00:07:13,590 --> 00:07:16,043
And for the header,

92
00:07:16,043 --> 00:07:18,984
I need to set up the authorization header,

93
00:07:18,984 --> 00:07:21,371
and in the authorization header,

94
00:07:21,371 --> 00:07:24,380
I need the token.

95
00:07:24,380 --> 00:07:29,487
So let me copy the token here and then in the authorization header,

96
00:07:29,487 --> 00:07:34,750
let's say bearer, and paste in the authorization header.

97
00:07:34,750 --> 00:07:39,853
You can also do the same thing to obtain the token.

98
00:07:39,853 --> 00:07:46,925
You can also do the log in through Facebook and then obtain the JSON web token.

99
00:07:46,925 --> 00:07:50,960
I have illustrated to obtain the JSON web token using

100
00:07:50,960 --> 00:07:55,655
the standard local authentication using the user name and password,

101
00:07:55,655 --> 00:07:58,940
but it doesn't matter which way you obtained the JSON web token.

102
00:07:58,940 --> 00:08:01,160
As long as you have the JSON web token,

103
00:08:01,160 --> 00:08:04,376
you should be able to perform all these operations.

104
00:08:04,376 --> 00:08:10,280
Let me perform the post operation on this endpoint and remember that this

105
00:08:10,280 --> 00:08:16,570
is the dish ID for the specific dish that I want to add to my list of favourites.

106
00:08:16,570 --> 00:08:19,895
When I post to the list of favorites,

107
00:08:19,895 --> 00:08:29,135
you will see that my server will reply back with this document here in the reply body.

108
00:08:29,135 --> 00:08:33,328
Now, let's examine this document to see what exactly it contains.

109
00:08:33,328 --> 00:08:36,680
This document, as you can see, contains an ID,

110
00:08:36,680 --> 00:08:39,511
but obviously because this is a JSON document,

111
00:08:39,511 --> 00:08:43,115
in addition it contains a

112
00:08:43,115 --> 00:08:46,335
Field here called user.

113
00:08:46,335 --> 00:08:48,425
Now what does this user field store?

114
00:08:48,425 --> 00:08:53,848
This user field is the object ID of the user

115
00:08:53,848 --> 00:09:00,620
corresponding to whom this particular list of favorites is.

116
00:09:00,620 --> 00:09:02,960
And look at the dishes.

117
00:09:02,960 --> 00:09:10,120
The dishes is an array and it is an array of dish IDs.

118
00:09:10,120 --> 00:09:15,950
And in this case, I just added the first dish to my list of favorites.

119
00:09:15,950 --> 00:09:22,690
So, that's why I have just one dish in the array of dishes in my list of favorites.

120
00:09:22,690 --> 00:09:30,140
So, this should immediately suggest to you how to design the schema for your favorites.

121
00:09:30,140 --> 00:09:34,760
So, dishes contains the object IDs of each of the dishes,

122
00:09:34,760 --> 00:09:39,124
and the user contains the object ID corresponding to the user.

123
00:09:39,124 --> 00:09:42,335
You have already seen how I use

124
00:09:42,335 --> 00:09:49,800
the information for doing population of the information when I do a get operation.

125
00:09:49,800 --> 00:09:52,690
We have done that with comments already.

126
00:09:52,690 --> 00:09:57,980
So, that should give you a big hint on how you would design the schema which contains

127
00:09:57,980 --> 00:10:04,855
the user and the array of dishes in my list of dishes here.

128
00:10:04,855 --> 00:10:07,330
Now, we have done the post on this.

129
00:10:07,330 --> 00:10:09,500
Let's get the list of favorites.

130
00:10:09,500 --> 00:10:11,750
So to get the list of favorites,

131
00:10:11,750 --> 00:10:14,405
I need to perform a get operation.

132
00:10:14,405 --> 00:10:16,561
Now, for the get operation,

133
00:10:16,561 --> 00:10:21,950
I obviously need to also have the authorization information here because

134
00:10:21,950 --> 00:10:27,635
you want a user to be able to retrieve only their list of favorites,

135
00:10:27,635 --> 00:10:32,905
and no other user can retrieve the list of favorites for a different user.

136
00:10:32,905 --> 00:10:34,590
So, this is very important.

137
00:10:34,590 --> 00:10:37,640
So, this suggests to you that you need to

138
00:10:37,640 --> 00:10:41,620
verify the user's authenticity even for the get operation.

139
00:10:41,620 --> 00:10:45,185
Now, how does the post-operation

140
00:10:45,185 --> 00:10:49,760
automatically get this object ID corresponding to the user?

141
00:10:49,760 --> 00:10:55,175
Recall that when you include the authorization header here,

142
00:10:55,175 --> 00:10:57,457
from the authorization header,

143
00:10:57,457 --> 00:11:01,055
by the way you perform the authorization of the user,

144
00:11:01,055 --> 00:11:06,240
direct that user properties already loaded onto the request object.

145
00:11:06,240 --> 00:11:10,270
And so that is where you will get access to the user's object ID.

146
00:11:10,270 --> 00:11:13,914
So, one more hint on how you will fill in this information.

147
00:11:13,914 --> 00:11:16,655
So, that is why notice that when I performed the post,

148
00:11:16,655 --> 00:11:19,625
I never specified the user because

149
00:11:19,625 --> 00:11:23,825
the user's information should automatically be

150
00:11:23,825 --> 00:11:28,484
derived from what we have supplied here in the authorization header.

151
00:11:28,484 --> 00:11:32,565
So, one more hint on how you would go about implementing this.

152
00:11:32,565 --> 00:11:36,830
So, that is why I said this assignment is a consolidation

153
00:11:36,830 --> 00:11:41,835
of all the topics that you have explored in this course.

154
00:11:41,835 --> 00:11:47,200
Now, I do not support a get operation on a specific dish ID.

155
00:11:47,200 --> 00:11:50,610
It doesn't make sense for supporting that on the favorites,

156
00:11:50,610 --> 00:11:53,345
in a specific dish ID.

157
00:11:53,345 --> 00:11:58,595
Instead, we can perform a get operation on local host 3443/favorites.

158
00:11:58,595 --> 00:12:01,670
And then we perform the get operation.

159
00:12:01,670 --> 00:12:09,398
You would immediately notice that the document that is returned to you contains

160
00:12:09,398 --> 00:12:17,715
the user's information already populated in by the get operation here.

161
00:12:17,715 --> 00:12:18,950
So, the user's information.

162
00:12:18,950 --> 00:12:22,112
So instead of the user ID there,

163
00:12:22,112 --> 00:12:26,445
the user's information is already present in there.

164
00:12:26,445 --> 00:12:28,730
This is done through Mongo's population.

165
00:12:28,730 --> 00:12:34,435
So, that is another big hint on how you're supposed to implement the get operation.

166
00:12:34,435 --> 00:12:37,130
Not only the user's information is filled in.

167
00:12:37,130 --> 00:12:39,626
Now why would I want the user's information to be filled in?

168
00:12:39,626 --> 00:12:44,540
Because that may be useful when I'm displaying information.

169
00:12:44,540 --> 00:12:46,350
This may not be necessary,

170
00:12:46,350 --> 00:12:49,445
but I'm just doing that just for the sake of completeness

171
00:12:49,445 --> 00:12:53,825
because if you are retrieving the list of favorites for a particular user,

172
00:12:53,825 --> 00:12:56,120
you automatically know who the user is,

173
00:12:56,120 --> 00:12:59,083
and that particular user is logging in anyway.

174
00:12:59,083 --> 00:13:01,385
But in case you want the user's information,

175
00:13:01,385 --> 00:13:06,278
you can perform this operation to get hold of that user's information.

176
00:13:06,278 --> 00:13:10,720
So, just to illustrate to you that Mongo's population can be used,

177
00:13:10,720 --> 00:13:16,515
I have illustrated not only that in the list of dishes here,

178
00:13:16,515 --> 00:13:22,820
the information about each dish is already populated into this array.

179
00:13:22,820 --> 00:13:24,907
So, if you have a bunch of dishes,

180
00:13:24,907 --> 00:13:30,385
the information of each of those dish should be populated into this array.

181
00:13:30,385 --> 00:13:34,670
So, this is another Mongo's population usage

182
00:13:34,670 --> 00:13:38,555
that you are seeing to fill in the dishes information.

183
00:13:38,555 --> 00:13:44,014
Just by the way you set up the schema for your favorites,

184
00:13:44,014 --> 00:13:47,840
this should be automatically possible for you by using

185
00:13:47,840 --> 00:13:52,010
just the dot populate on the list of dishes here.

186
00:13:52,010 --> 00:13:54,103
So, thats the second part.

187
00:13:54,103 --> 00:13:59,213
Now that we're able to retrieve the get dishes method,

188
00:13:59,213 --> 00:14:03,590
let me show you how you can perform a post operation where you can

189
00:14:03,590 --> 00:14:08,750
add a bunch of dishes into your list of favorites.

190
00:14:08,750 --> 00:14:11,145
Now, going back to this post,

191
00:14:11,145 --> 00:14:18,145
if you need to add not just one but a set of dishes to your list of favorites,

192
00:14:18,145 --> 00:14:19,850
now why would you want this?

193
00:14:19,850 --> 00:14:24,980
Maybe on the client's side you might have a button which when

194
00:14:24,980 --> 00:14:31,255
clicked will allow you to check mark a set of dishes from the list of dishes,

195
00:14:31,255 --> 00:14:34,710
and then add them at one shot into your list of favorites.

196
00:14:34,710 --> 00:14:37,390
So, to support that kind of an operation,

197
00:14:37,390 --> 00:14:42,295
you may wish to allow a post to be performed on the slash favorites here.

198
00:14:42,295 --> 00:14:45,641
Now, when the post is performed on the slash favorites, again,

199
00:14:45,641 --> 00:14:48,959
you'll still need the authorization header.

200
00:14:48,959 --> 00:14:51,460
But in the body of the message,

201
00:14:51,460 --> 00:14:57,493
we're going to be storing the list of dishes as an array of this format.

202
00:14:57,493 --> 00:15:01,490
So, this is a JSON document here,

203
00:15:01,490 --> 00:15:04,340
so let me make sure that in my header,

204
00:15:04,340 --> 00:15:08,107
the content type is already present there.

205
00:15:08,107 --> 00:15:14,545
Now, this document will

206
00:15:14,545 --> 00:15:18,735
contain an array of dishes like this.

207
00:15:18,735 --> 00:15:22,700
So, each dish will be identified by

208
00:15:22,700 --> 00:15:29,860
saying underscore ID colon,

209
00:15:29,860 --> 00:15:34,835
and then here they will have the dish ID,

210
00:15:34,835 --> 00:15:39,760
and so this would be nothing but an array of dishes.

211
00:15:39,760 --> 00:15:46,630
So, let me add two dishes to my list of favorites here just to illustrate the point.

212
00:15:46,630 --> 00:15:51,726
So, let me paste in the IDs from a couple of dishes into this.

213
00:15:51,726 --> 00:15:56,823
So, when you perform a post on local host 3443/favorites,

214
00:15:56,823 --> 00:16:02,295
this is how you compose the body of the post message here.

215
00:16:02,295 --> 00:16:07,497
So, each dish that you want to add will be included in this format here.

216
00:16:07,497 --> 00:16:09,670
Now, when you include them in this format,

217
00:16:09,670 --> 00:16:12,425
and then post to that server side,

218
00:16:12,425 --> 00:16:20,161
the server in return will send back the updated list of favorites for you.

219
00:16:20,161 --> 00:16:23,395
So, when you receive the updated list of favorites from the server side,

220
00:16:23,395 --> 00:16:26,300
you'll see that the user's information is already there,

221
00:16:26,300 --> 00:16:34,685
but notice how the favorite dishes are stored in this array of dish object IDs.

222
00:16:34,685 --> 00:16:38,320
So, the first one we had already added in the previous operation.

223
00:16:38,320 --> 00:16:39,485
So the remaining two,

224
00:16:39,485 --> 00:16:43,990
we just added to the list of our favorites.

225
00:16:43,990 --> 00:16:47,545
So, those two object IDs are also added into

226
00:16:47,545 --> 00:16:53,110
my dishes array in my favorites document here.

227
00:16:53,110 --> 00:16:56,050
So, there you go. So, that is how you're supposed to perform

228
00:16:56,050 --> 00:17:02,763
the favorites post on the endpoint like this.

229
00:17:02,763 --> 00:17:05,340
Now, let's again perform a get operation on

230
00:17:05,340 --> 00:17:10,160
the favorites just to see what the get operation features for us.

231
00:17:10,160 --> 00:17:12,600
So when we perform the GET operation,

232
00:17:12,600 --> 00:17:21,345
you will immediately notice that in the return favorites document,

233
00:17:21,345 --> 00:17:23,640
you have the user's information there,

234
00:17:23,640 --> 00:17:25,065
and then down below here,

235
00:17:25,065 --> 00:17:29,820
the dishes is nothing but an array of Json documents here,

236
00:17:29,820 --> 00:17:32,670
each one corresponding to a dish.

237
00:17:32,670 --> 00:17:35,010
Now this is automatically taken care of by the mongoose

238
00:17:35,010 --> 00:17:38,360
populate that we have done on the server side.

239
00:17:38,360 --> 00:17:44,525
So all this information should be automatically populated on the server-side.

240
00:17:44,525 --> 00:17:50,430
So that is how your post operations on the two endpoints work.

241
00:17:50,430 --> 00:17:56,395
Now let me show you how you would perform a DELETE operation.

242
00:17:56,395 --> 00:18:00,735
Now to delete a specific dish.

243
00:18:00,735 --> 00:18:08,193
So let me perform a delete operation on a specific endpoint.

244
00:18:08,193 --> 00:18:13,095
So we will go back to this endpoint,

245
00:18:13,095 --> 00:18:16,470
and then let me just copy one of these,

246
00:18:16,470 --> 00:18:18,985
and then I will delete one of the dishes.

247
00:18:18,985 --> 00:18:22,585
So to perform the delete operation on one of the dishes,

248
00:18:22,585 --> 00:18:23,750
of course as you see,

249
00:18:23,750 --> 00:18:26,453
we don't need the body yet,

250
00:18:26,453 --> 00:18:29,485
and we don't need the content here.

251
00:18:29,485 --> 00:18:35,580
All that we need to do is to perform a delete operation on this endpoint.

252
00:18:35,580 --> 00:18:43,125
So here you see that I'm doing localhost:3443/favorites/ and then the dish ID.

253
00:18:43,125 --> 00:18:46,231
And then let me post this.

254
00:18:46,231 --> 00:18:51,760
So when I delete a specific dish from my list of favorites,

255
00:18:51,760 --> 00:18:53,695
you immediately notice that

256
00:18:53,695 --> 00:19:01,555
my updated list of favorites is sent back to the client side by the server.

257
00:19:01,555 --> 00:19:03,010
So you have the user here,

258
00:19:03,010 --> 00:19:06,220
but note that in the dishes array I only have two dishes now.

259
00:19:06,220 --> 00:19:11,105
The one that I have just deleted is gone from that list of dishes.

260
00:19:11,105 --> 00:19:12,820
So if you perform a GET now,

261
00:19:12,820 --> 00:19:18,265
your dishes array will continually to dishes in there.

262
00:19:18,265 --> 00:19:24,635
Now, suppose I perform a DELETE on the favorites here.

263
00:19:24,635 --> 00:19:30,565
So if I perform DELETE on the favorites endpoint itself directly,

264
00:19:30,565 --> 00:19:34,540
then what it is going to do is to delete all my favorites and

265
00:19:34,540 --> 00:19:39,096
also delete my favorites document completely from the server side.

266
00:19:39,096 --> 00:19:42,250
So let me perform that delete operation on the server side,

267
00:19:42,250 --> 00:19:47,000
and then it will return the fact that it has deleted this particular document,

268
00:19:47,000 --> 00:19:49,675
so it just returns this document anyway.

269
00:19:49,675 --> 00:19:54,670
But now if I perform a GET operation on my list of favorites,

270
00:19:54,670 --> 00:19:57,580
I will retrieve a null there.

271
00:19:57,580 --> 00:20:00,880
Meaning that I don't have this document on the server side.

272
00:20:00,880 --> 00:20:03,800
So the favorites document is completely gone,

273
00:20:03,800 --> 00:20:09,352
when I perform DELETE operation on local host column three from formerly slash favorites.

274
00:20:09,352 --> 00:20:12,805
Now again, when I add a new dish to my list of favorites,

275
00:20:12,805 --> 00:20:16,580
my favorites document will be set up all over again.

276
00:20:16,580 --> 00:20:21,880
That's perfectly fine for us to operate with.

277
00:20:21,880 --> 00:20:26,440
So with this demonstration I have shown you

278
00:20:26,440 --> 00:20:32,680
all the various operations that you need to perform on your server side.

279
00:20:32,680 --> 00:20:36,670
All the various operations on the various route endpoints

280
00:20:36,670 --> 00:20:40,754
that you need to perform or implement on the server side.

281
00:20:40,754 --> 00:20:42,265
Now that you have seen

282
00:20:42,265 --> 00:20:47,720
all the various operations that the server side should support on the slash favorites,

283
00:20:47,720 --> 00:20:48,895
and the slash favorites,

284
00:20:48,895 --> 00:20:50,995
slash dish ID endpoints,

285
00:20:50,995 --> 00:20:56,033
let me list out the three tasks that you need to complete in this assignment.

286
00:20:56,033 --> 00:21:03,340
The first task of course is to implement the favorite schema and model.

287
00:21:03,340 --> 00:21:09,995
This you will implement in the models folder in a file called favorite nodeJS.

288
00:21:09,995 --> 00:21:12,460
There you will implement the favorite schema.

289
00:21:12,460 --> 00:21:13,952
Remember that the favorite schema will store

290
00:21:13,952 --> 00:21:19,720
the reference to the user's document object ID,

291
00:21:19,720 --> 00:21:26,212
and an array of dishes document object IDs.

292
00:21:26,212 --> 00:21:29,440
That's the big hint about how you are supposed to

293
00:21:29,440 --> 00:21:33,355
design the schema and the model for your favorites.

294
00:21:33,355 --> 00:21:39,190
The second task in your assignment is to implement the GET,

295
00:21:39,190 --> 00:21:46,555
POST, and DELETE operations on the slash favorites endpoint.

296
00:21:46,555 --> 00:21:49,245
GET will get you all the list of favorites,

297
00:21:49,245 --> 00:21:52,225
populated automatically by Mongo's populate,

298
00:21:52,225 --> 00:21:58,360
the POST should contain a list of dishes as specified

299
00:21:58,360 --> 00:22:04,425
in the format that I have just demonstrated to you in the body of the POST message.

300
00:22:04,425 --> 00:22:08,695
And so a bunch of dishes will be added into your list of favorites,

301
00:22:08,695 --> 00:22:11,800
and DELETE operation on the slash favorites endpoint will

302
00:22:11,800 --> 00:22:16,215
delete your favorites document completely from the server side.

303
00:22:16,215 --> 00:22:22,225
Now if you perform a POST operation on slash favorite slash dish ID,

304
00:22:22,225 --> 00:22:27,405
then the particular dish will be added into your list of favorites.

305
00:22:27,405 --> 00:22:29,635
If your favorite document doesn't exist,

306
00:22:29,635 --> 00:22:32,455
you obviously need to create that favorite document.

307
00:22:32,455 --> 00:22:36,250
So that is also part of that POST operation.

308
00:22:36,250 --> 00:22:39,760
So if at any point you find that the favorite document doesn't exist,

309
00:22:39,760 --> 00:22:41,770
you need to first create the favorite document and

310
00:22:41,770 --> 00:22:44,550
then add this dish into the favorite document.

311
00:22:44,550 --> 00:22:47,155
So that is an important point also to note.

312
00:22:47,155 --> 00:22:51,305
When you perform a POST on the slash favorites endpoint,

313
00:22:51,305 --> 00:22:53,290
if the favorite document doesn't exist,

314
00:22:53,290 --> 00:22:58,150
you need to create a favorite document and then add the dishes to your favorite document.

315
00:22:58,150 --> 00:22:59,410
If it already exists,

316
00:22:59,410 --> 00:23:02,765
then you only update that favorite document.

317
00:23:02,765 --> 00:23:08,480
So this is one more hint on how you're supposed to implement the POST operations.

318
00:23:08,480 --> 00:23:13,735
If you perform a DELETE operation on the slash favorites slash dish ID,

319
00:23:13,735 --> 00:23:18,640
then that specific dish will be deleted from the list of favorites.

320
00:23:18,640 --> 00:23:23,125
Now in performing the POST and the DELETE operations,

321
00:23:23,125 --> 00:23:28,110
you should ensure that you won't add duplicates to the dish ID.

322
00:23:28,110 --> 00:23:33,165
So before you add a dish object ID to the list of dishes,

323
00:23:33,165 --> 00:23:39,490
always check to make sure if the dish ID already exists in the list of dishes.

324
00:23:39,490 --> 00:23:45,430
Now this is where you can use the array indexOf method to check to see

325
00:23:45,430 --> 00:23:52,395
if a document ID already exists in an array of documents.

326
00:23:52,395 --> 00:23:56,725
So that's another big hint on how you are going to be implementing that part.

327
00:23:56,725 --> 00:23:59,610
And the third task of course is to mount

328
00:23:59,610 --> 00:24:05,725
the favorite router on the slash favorites endpoints by updating app.js file.

329
00:24:05,725 --> 00:24:08,490
The favorite router itself will implemented in

330
00:24:08,490 --> 00:24:13,710
the routes folder in favoriterouter.js file.

331
00:24:13,710 --> 00:24:17,010
So with this I have described what you're

332
00:24:17,010 --> 00:24:21,010
supposed to implement in the fourth and final assignment.

333
00:24:21,010 --> 00:24:24,015
And I hope that this assignment will help you

334
00:24:24,015 --> 00:24:27,930
to consolidate everything that you have learned in the course,

335
00:24:27,930 --> 00:24:32,685
all the way from designing express router,

336
00:24:32,685 --> 00:24:40,720
to doing MongoDB and also Mongo's schema and models,

337
00:24:40,720 --> 00:24:45,095
and also performing the appropriate user authentication.

338
00:24:45,095 --> 00:24:47,685
And as I have mentioned again,

339
00:24:47,685 --> 00:24:52,965
only an authenticated user will get access to his or her list of favorites.

340
00:24:52,965 --> 00:24:55,920
Even the GET operation can only be performed by

341
00:24:55,920 --> 00:25:00,750
an authenticated user on the slash favorites endpoint.

342
00:25:00,750 --> 00:25:05,640
And you would only fetch the favorites for that particular user.

343
00:25:05,640 --> 00:25:12,000
You cannot allow a different user to access the favorites of another user.

344
00:25:12,000 --> 00:25:15,840
So again, that also tells you how you're supposed to perform

345
00:25:15,840 --> 00:25:20,890
the authentication on the different route endpoints.

346
00:25:20,890 --> 00:25:27,595
With this I complete the description of the tasks in the fourth assignment.

347
00:25:27,595 --> 00:25:31,790
Have fun completing this assignment.