1
00:00:00,025 --> 00:00:04,580
[MUSIC]

2
00:00:04,580 --> 00:00:09,730
In the previous exercise we have got our
loopback servers up to a certain stage.

3
00:00:09,730 --> 00:00:13,549
We are able to now connect our loopback
servers to the mongolab database,

4
00:00:15,040 --> 00:00:19,290
the database acting as the data source for
our loopback servers.

5
00:00:19,290 --> 00:00:24,042
We also define access controlled lists
to control access to the radius,

6
00:00:24,042 --> 00:00:28,010
rest are here,
in parts with our loopback server.

7
00:00:28,010 --> 00:00:31,590
In this exercise,
we will explore loopback relations.

8
00:00:31,590 --> 00:00:36,510
We can now define models and
define relationships between the models.

9
00:00:36,510 --> 00:00:40,280
So, for example,
let's take the example of a comment.

10
00:00:40,280 --> 00:00:44,860
When you post a comment about a dish,
the comment is related to the dish.

11
00:00:44,860 --> 00:00:46,980
A comment belongs to a dish.

12
00:00:46,980 --> 00:00:50,610
Similarly, a comment is
posted by a specific user.

13
00:00:50,610 --> 00:00:55,730
So the comment has belongs to
the relationship with this specific user.

14
00:00:55,730 --> 00:00:59,370
Now, when you look at the relationship
in the reverse direction,

15
00:00:59,370 --> 00:01:02,555
a dish may have multiple
comments associated with it, and

16
00:01:02,555 --> 00:01:05,616
similarly, a user may have
posted multiple comments.

17
00:01:05,616 --> 00:01:10,512
So these are the biggest kinds of
relationships that you can define between

18
00:01:10,512 --> 00:01:14,640
the various models within
our loopback server.

19
00:01:14,640 --> 00:01:18,930
Also we will look at the use
of a mixed-in loopback,

20
00:01:18,930 --> 00:01:23,460
which allows us to add additional
properties to our models.

21
00:01:23,460 --> 00:01:28,850
At the end of this exercise you will have
a pretty much complete loopback server,

22
00:01:28,850 --> 00:01:32,770
which we can then connect
to our angular client and

23
00:01:32,770 --> 00:01:37,400
be able to make use of it as
the back-end for our angular client.

24
00:01:39,650 --> 00:01:46,350
To continue with the exercise,
in our loopback server's folder,

25
00:01:46,350 --> 00:01:50,910
let's create a new model for a comment.

26
00:01:50,910 --> 00:01:55,040
So this model will define as

27
00:01:56,610 --> 00:02:01,930
comment, and
in this case that model is also saved

28
00:02:01,930 --> 00:02:07,030
in MongoDB and
will be as a persistent model,

29
00:02:07,030 --> 00:02:12,040
and we will expose the comment
via the REST API and

30
00:02:13,580 --> 00:02:18,180
the model folder is in
the common folder and for

31
00:02:18,180 --> 00:02:22,170
this comment we will have two
properties associated with it.

32
00:02:22,170 --> 00:02:28,489
And the first property is Rating.

33
00:02:30,647 --> 00:02:34,499
Which is of the type Number.

34
00:02:34,499 --> 00:02:39,300
This is required and the default value,
I will leave that as 5.

35
00:02:39,300 --> 00:02:45,988
And the second property would be Comment,
which is of the type string.

36
00:02:45,988 --> 00:02:52,410
This is required and
the default value is empty.

37
00:02:52,410 --> 00:02:56,320
So each comment will have these two
properties, rating and comment,

38
00:02:56,320 --> 00:02:57,800
associated with it.

39
00:02:57,800 --> 00:03:01,190
Also, we need to now define
the relationship between the comment and

40
00:03:01,190 --> 00:03:06,000
the user that is submitting the comment,
and also the comment and

41
00:03:06,000 --> 00:03:10,260
the dish with which this
comment is associated.

42
00:03:10,260 --> 00:03:15,010
Similarly, we have seen in the earlier
exercise that they have added

43
00:03:15,010 --> 00:03:17,360
their favorites for a user.

44
00:03:17,360 --> 00:03:22,610
So to define the favorites,
let me again define a model,

45
00:03:22,610 --> 00:03:29,390
and that model name is Favorite and
this is also saved in the MongoDB,

46
00:03:29,390 --> 00:03:33,870
and this is also a PersistedModel, and

47
00:03:33,870 --> 00:03:39,120
we will expose the favorite for
the rest API and is in the common folder.

48
00:03:39,120 --> 00:03:43,020
Now the favorite itself doesn't need
any specific property because all of

49
00:03:43,020 --> 00:03:48,770
the favorite does is associates
the specific user with this specific dish.

50
00:03:48,770 --> 00:03:52,790
So the favorite here is going to be
used simply as a bridge between the user

51
00:03:52,790 --> 00:03:53,770
and the dish.

52
00:03:53,770 --> 00:03:58,385
So we will use the favorite as a way of
defining relations between the dish and

53
00:03:58,385 --> 00:04:00,320
a user.

54
00:04:00,320 --> 00:04:02,710
So it doesn't have any
specific properties.

55
00:04:02,710 --> 00:04:07,970
So now we have added two models
into our loopback server.

56
00:04:07,970 --> 00:04:10,950
Now, let's start defining
the model relations.

57
00:04:10,950 --> 00:04:13,110
Now as I explained earlier,

58
00:04:13,110 --> 00:04:17,260
there are various kinds of model
relationships that we can define.

59
00:04:17,260 --> 00:04:22,230
Let's start with defining the relationship
between a dish and a comment.

60
00:04:22,230 --> 00:04:27,820
So to define a model in loopback,
we'll say, lb relation.

61
00:04:27,820 --> 00:04:29,890
And when the relationship comes in,

62
00:04:29,890 --> 00:04:34,370
we need to specify the model from
which to create the relation.

63
00:04:34,370 --> 00:04:38,700
So in the first instance,
we will create a relation between a dish.

64
00:04:38,700 --> 00:04:45,080
So from the dishes, and a dish might
have many comments associated with it,

65
00:04:45,080 --> 00:04:50,160
so that's why the relation
type has many and

66
00:04:50,160 --> 00:04:56,480
is associated with a comment and
the property name for the relation

67
00:04:56,480 --> 00:05:02,060
I will leave it as the default with
just comment with a small letter c.

68
00:05:04,320 --> 00:05:09,470
Now we can create a specific
custom foreign key if we want to.

69
00:05:09,470 --> 00:05:13,600
If we don't, loopback will
automatically assign the foreign key,

70
00:05:13,600 --> 00:05:18,290
which would be nothing but
the comments and an id associated with it.

71
00:05:18,290 --> 00:05:21,700
So it'll look back to
define it automatically.

72
00:05:21,700 --> 00:05:25,860
We can also have through models,
which in this case is not applicable.

73
00:05:25,860 --> 00:05:31,490
A through model will allow us to define
relation to one and to another model and

74
00:05:31,490 --> 00:05:36,900
in this case, we don't have such
relationship, so we'll just say no and

75
00:05:36,900 --> 00:05:40,670
we won't nest the REST API, and also,

76
00:05:40,670 --> 00:05:45,430
we'll disable the relation
from being included.

77
00:05:45,430 --> 00:05:48,480
And so, we have specified

78
00:05:48,480 --> 00:05:53,000
that the disabling of the relationship
from being included is no.

79
00:05:53,000 --> 00:05:55,790
Later on you will see
why this is important.

80
00:05:55,790 --> 00:05:59,910
We would have a way of
including information from one

81
00:06:00,950 --> 00:06:04,370
model into another model
by doing an include there.

82
00:06:04,370 --> 00:06:10,500
So that is something that we
have asked not to be prevented.

83
00:06:10,500 --> 00:06:14,990
Now similarly,
we can define the next relationship

84
00:06:14,990 --> 00:06:18,820
between comment and the dishes.

85
00:06:20,430 --> 00:06:24,720
So when you define a relationship
to the comment and the dishes, so

86
00:06:24,720 --> 00:06:31,380
let me say it lb relation, and we will now
define the relation between the comment.

87
00:06:31,380 --> 00:06:35,620
And since a comment can only
belong to a specific dish, so

88
00:06:35,620 --> 00:06:40,700
we'll define it belongs to a relationship,
and this is belonging

89
00:06:40,700 --> 00:06:46,180
to the dish here and the property name for
the relation would be dishes.

90
00:06:46,180 --> 00:06:50,226
And we will not use a custom foreign key,

91
00:06:50,226 --> 00:06:56,067
we'll allow the relationship
to be used in the REST APIs.

92
00:06:56,067 --> 00:07:01,172
Actually we don't want that, so
I should have typed in no for that,

93
00:07:01,172 --> 00:07:06,026
but I'll just go and edit the created
relation in my code there.

94
00:07:06,026 --> 00:07:09,816
So I can go into
the comment.jason file and

95
00:07:09,816 --> 00:07:13,604
just change this from
a yes to a no in there,

96
00:07:13,604 --> 00:07:19,080
I'll do that because I don't
want it to be nested next.

97
00:07:19,080 --> 00:07:20,430
So let me go ahead and

98
00:07:20,430 --> 00:07:25,400
make that change in the comment called
Jason before I forget to do that.

99
00:07:25,400 --> 00:07:28,070
And then open the comment called Jason.

100
00:07:28,070 --> 00:07:33,210
And you see that in the comment called
Jason we have this relationship here and

101
00:07:33,210 --> 00:07:40,810
then I am going to cut this out because
I don't want that nesting to be done.

102
00:07:41,860 --> 00:07:48,422
That's a mistake that I made while
typing in, so I'll delete that.

103
00:07:48,422 --> 00:07:53,350
The next relationship that I'm going
to define is between the comment and

104
00:07:53,350 --> 00:07:54,760
the customer.

105
00:07:54,760 --> 00:07:57,540
Let me again, type lb relation here.

106
00:07:59,670 --> 00:08:02,970
So we'll define the relationship
between the comment and the customer.

107
00:08:02,970 --> 00:08:09,076
A comment belongs to
a customer as we would expect,

108
00:08:09,076 --> 00:08:15,000
and, Here let me explicitly

109
00:08:15,000 --> 00:08:21,050
enter, A custom foreign key.

110
00:08:21,050 --> 00:08:24,350
I'm just doing this to
illustrate to you that I can

111
00:08:25,400 --> 00:08:28,390
define a specific foreign key here.

112
00:08:28,390 --> 00:08:32,910
I'll not nest, and I'll not disable here.

113
00:08:32,910 --> 00:08:36,856
So, this is the relationship that
I'll define between the comment and

114
00:08:36,856 --> 00:08:38,210
the customer.

115
00:08:38,210 --> 00:08:44,092
Next, Let me define the relationship
between the customer and the comment.

116
00:08:44,092 --> 00:08:49,897
So here, from a customer,
a customer has many comments,

117
00:08:49,897 --> 00:08:54,886
and their property name I
will enter as comments.

118
00:08:54,886 --> 00:09:01,576
And then, I will define
the foreign key as customerId.

119
00:09:01,576 --> 00:09:07,990
And I don't require a through model here,
I will not nest and I'll not disable.

120
00:09:07,990 --> 00:09:13,260
So now I have defined all of
the relationships between the comments and

121
00:09:13,260 --> 00:09:16,210
the customer, and
the comments and the dishes.

122
00:09:16,210 --> 00:09:20,238
Now, same thing let's
work on the favorites.

123
00:09:20,238 --> 00:09:26,113
Now, for the favorites, Let me define

124
00:09:26,113 --> 00:09:33,560
the relationship between the dishes and
the favorites.

125
00:09:33,560 --> 00:09:39,310
So a dish could have been favorites for
many users,

126
00:09:39,310 --> 00:09:45,470
so it has many relationship
with a favorite.

127
00:09:45,470 --> 00:09:49,120
And we'll use favorites there, and

128
00:09:49,120 --> 00:09:53,603
we'll not need a through model and
no and no.

129
00:09:53,603 --> 00:09:58,269
So, now we have defined it has many
relationships between the dish and

130
00:09:58,269 --> 00:09:59,150
a favorite.

131
00:10:00,848 --> 00:10:06,153
Let me now define a relationship
between a favorite and a dish.

132
00:10:06,153 --> 00:10:11,282
So to define the relationship
between the favorite and

133
00:10:11,282 --> 00:10:15,852
the dish let me say favorite
belongs to dish, and

134
00:10:15,852 --> 00:10:20,995
we'll use the dishes and
no foreign key, not nested.

135
00:10:25,515 --> 00:10:29,070
And we'll not disable
the relation from being included.

136
00:10:31,485 --> 00:10:36,397
Now, we'll have to define
that relationship between

137
00:10:36,397 --> 00:10:39,560
a favorite and a customer.

138
00:10:39,560 --> 00:10:47,149
So we'll say favorite
belongs to a customer and,

139
00:10:50,393 --> 00:10:55,724
We'll define it foreign
key as a customerId.

140
00:10:55,724 --> 00:11:03,370
We'll not allow the nesting of
the REST API and we'll not disable.

141
00:11:03,370 --> 00:11:09,430
Now the last relationship,
Is between the customer and the favorite.

142
00:11:10,910 --> 00:11:16,339
So, now we'll define the relationship
between the customer has many,

143
00:11:18,264 --> 00:11:21,440
Favorites, and this,

144
00:11:23,506 --> 00:11:29,194
I'll define the customerId as the foreign
key, I don't require a through model,

145
00:11:29,194 --> 00:11:33,433
and I won't allow the nesting and
I won't disable anything.

146
00:11:33,433 --> 00:11:38,353
So that's it, we have defined all
the relationships that we need

147
00:11:38,353 --> 00:11:42,590
between the comments and
the customers and the dishes.

148
00:11:42,590 --> 00:11:46,680
Between the favorites,
the customers, and the dishes.

149
00:11:46,680 --> 00:11:51,080
Now, lets examine the use of a mixin.

150
00:11:51,080 --> 00:11:57,340
Now, this mixin that I'm going to
use with my Loopback server is

151
00:11:57,340 --> 00:12:03,900
a mixin that adds timestamps to
anything that is saved or updated.

152
00:12:03,900 --> 00:12:08,780
So recall that in our Express server,
we had the created at and

153
00:12:08,780 --> 00:12:14,550
updated at added automatically
by using Mongoose.

154
00:12:14,550 --> 00:12:18,399
Now for Loopback,
I need to use this specific

155
00:12:18,399 --> 00:12:22,365
mixin called as Lookback
ds timestamp mixin.

156
00:12:22,365 --> 00:12:27,130
so toSdo that I have to first
install the mixin into my project.

157
00:12:27,130 --> 00:12:30,955
So I'll say npm install

158
00:12:30,955 --> 00:12:38,609
loopback-ds-timestamp-mixin --save,

159
00:12:38,609 --> 00:12:44,164
and the install this node module.

160
00:12:44,164 --> 00:12:48,113
And once this node module is installed,
so as you can see,

161
00:12:48,113 --> 00:12:51,680
this is version number
3.4.1 that I'm using.

162
00:12:51,680 --> 00:12:54,620
As this mixin is installed,
I need to go in and

163
00:12:54,620 --> 00:12:58,760
enable this mixin usage within my models.

164
00:12:58,760 --> 00:13:03,970
To do that lets go to the code,
in the code let me first open

165
00:13:03,970 --> 00:13:09,736
that model conflict adjacent,
which is in the server folder.

166
00:13:09,736 --> 00:13:14,716
So in the model confi.json if you
go up here you will see these

167
00:13:14,716 --> 00:13:16,900
mixins defined here.

168
00:13:16,900 --> 00:13:20,090
So we have the mixins from loopback
common and loopback server.

169
00:13:20,090 --> 00:13:24,750
Now I'm going to add in the new
mixin I have just added here.

170
00:13:24,750 --> 00:13:31,360
So, this mixin is in ../node_modules.

171
00:13:31,360 --> 00:13:33,940
So this is in the node
modules folder right there.

172
00:13:35,894 --> 00:13:45,894
/loopback-ds-timestamp-mixin.

173
00:13:51,953 --> 00:13:58,762
This allows my Loopback server to make
use of this mixin within my server.

174
00:13:58,762 --> 00:14:01,971
Now not only this, I need to specify for

175
00:14:01,971 --> 00:14:06,810
all the models where I want
this mixin to be included.

176
00:14:06,810 --> 00:14:11,014
So, I will go now to that common folder,
and

177
00:14:11,014 --> 00:14:16,812
in the common folder we will
edit that comment.json file and

178
00:14:16,812 --> 00:14:21,940
in the comment.json file,
we will add in the mixin.

179
00:14:21,940 --> 00:14:26,539
So after the properties,
I will add in the mixin as

180
00:14:26,539 --> 00:14:30,490
sync within quotes, you'll say mixins.

181
00:14:33,167 --> 00:14:38,563
:, And, there,

182
00:14:38,563 --> 00:14:43,657
and inside the mixins we'll see

183
00:14:43,657 --> 00:14:50,350
TimeStamp, true.

184
00:14:51,863 --> 00:14:55,920
This same thing I need to paste
into some of the other models so

185
00:14:55,920 --> 00:14:57,445
let me just copy this.

186
00:14:57,445 --> 00:15:03,200
So we'll say mixins TimeStamp
true in comments.json.

187
00:15:03,200 --> 00:15:07,420
Then we will go to dishes.json and

188
00:15:07,420 --> 00:15:11,680
then we'll add this
also to dishes.json and

189
00:15:11,680 --> 00:15:16,449
then they will go to favorite.json, and

190
00:15:16,449 --> 00:15:21,390
then add into favorite.json.

191
00:15:21,390 --> 00:15:26,395
We'll add it to readers.json, And

192
00:15:26,395 --> 00:15:30,129
we'll add it to promotions.json.

193
00:15:32,495 --> 00:15:36,076
And save all the changes.

194
00:15:36,076 --> 00:15:41,583
Now we will configure a few
more access control list, so

195
00:15:41,583 --> 00:15:50,020
going to the terminal let me type lb acl
to configure some access control lists.

196
00:15:50,020 --> 00:15:53,990
So, here for the dishes,

197
00:15:53,990 --> 00:15:59,750
I will specify for all methods and
properties the right

198
00:16:01,070 --> 00:16:09,686
Can only be done by the user
with the role admin.

199
00:16:11,368 --> 00:16:17,240
So that is the first access
control that I configure for

200
00:16:17,240 --> 00:16:21,589
dishes, then I do the same for leaders.

201
00:16:22,940 --> 00:16:28,673
All methods and properties, write, other,

202
00:16:28,673 --> 00:16:33,819
the admin role explicitly grant access.

203
00:16:33,819 --> 00:16:39,254
Then for the promotions also,

204
00:16:39,254 --> 00:16:43,853
the same, all methods and

205
00:16:43,853 --> 00:16:49,923
properties, write, and other,

206
00:16:52,023 --> 00:16:57,256
Role explicitly grant access,
because you only want the admin

207
00:16:57,256 --> 00:17:03,800
to be able to make any changes to
the dishes, promotions, or leaders.

208
00:17:03,800 --> 00:17:09,230
Now for the comments, the comments
can be posted by any registered user.

209
00:17:09,230 --> 00:17:15,476
So for the comments,
let me define the ACL for the comments.

210
00:17:15,476 --> 00:17:20,610
We'll say comment, all methods and

211
00:17:20,610 --> 00:17:25,909
properties, all types, all users,

212
00:17:25,909 --> 00:17:29,560
explicitly deny access.

213
00:17:29,560 --> 00:17:36,430
Because we don't want them to
be able to do all the things.

214
00:17:36,430 --> 00:17:44,200
Now, let's open only some of
the operations to be enabled by users.

215
00:17:44,200 --> 00:17:47,749
So for comments, all methods and

216
00:17:47,749 --> 00:17:52,330
properties, we'll say read operation.

217
00:17:52,330 --> 00:17:58,910
Read operation for the comments
will be only an authenticated user.

218
00:17:58,910 --> 00:18:03,995
So only users that are logged in
will be able to read the comments.

219
00:18:03,995 --> 00:18:09,951
Now similarly, let me define for the,

220
00:18:13,120 --> 00:18:17,201
Comments, a single method, and

221
00:18:17,201 --> 00:18:21,587
create, who can create comments?

222
00:18:23,341 --> 00:18:29,583
Any authenticated user, any
authenticated user can create comments.

223
00:18:29,583 --> 00:18:34,204
So we'll explicitly grant them the access,
now,

224
00:18:34,204 --> 00:18:39,529
the last one for comments, We'll say for

225
00:18:39,529 --> 00:18:43,720
the comment, all methods and properties.

226
00:18:43,720 --> 00:18:48,850
Write, meaning that,
who is allowed to write the comment?

227
00:18:48,850 --> 00:18:55,080
Only the user owning that object
can make changes to the comment.

228
00:18:55,080 --> 00:19:00,080
So only the user that has submitted the
comment can make changes to the comment,

229
00:19:00,080 --> 00:19:04,271
either edit or delete the comment,
so that's what we will allow.

230
00:19:04,271 --> 00:19:09,699
Same set of operations,
let's apply them to the favorites.

231
00:19:09,699 --> 00:19:14,703
So for favorites, since we haven't added

232
00:19:14,703 --> 00:19:20,680
any of the access controls,
so for the favorites,

233
00:19:20,680 --> 00:19:25,545
all methods and properties, all types,

234
00:19:25,545 --> 00:19:30,699
all users, explicitly deny access there.

235
00:19:30,699 --> 00:19:34,734
For favorites, we will define for

236
00:19:34,734 --> 00:19:39,047
all methods and properties, read,

237
00:19:39,047 --> 00:19:44,480
only for the user owning that object.

238
00:19:44,480 --> 00:19:46,910
So only the user can read his or

239
00:19:46,910 --> 00:19:51,186
her own favorites, no other user should
be able to see his or her own favorites.

240
00:19:51,186 --> 00:19:56,616
Now the third one is
creation of favorites,

241
00:19:56,616 --> 00:20:02,192
so for creation of favorites,
we will say for

242
00:20:02,192 --> 00:20:07,035
favorite, a single method, create,

243
00:20:07,035 --> 00:20:13,510
any authenticated user
can create a comment.

244
00:20:13,510 --> 00:20:19,980
And last, for a favorite, all methods and

245
00:20:19,980 --> 00:20:26,280
properties, the write operation,
only for the user owning that object.

246
00:20:26,280 --> 00:20:30,621
So only the user that has input
the favorite for himself or

247
00:20:30,621 --> 00:20:35,061
herself can edit or
delete those favorites, that's it.

248
00:20:35,061 --> 00:20:39,288
So we have now set up all
of the access controls and

249
00:20:39,288 --> 00:20:43,530
all the various relations for
our application.

250
00:20:45,200 --> 00:20:49,083
Let's now start the Rest API
of the server, and

251
00:20:49,083 --> 00:20:52,102
then explore some of these details.

252
00:20:52,102 --> 00:20:57,142
Now, going to our application
in the browser, so

253
00:20:57,142 --> 00:21:02,908
we'll go to that LoopBack Explorer
in the browser here.

254
00:21:02,908 --> 00:21:08,437
In the LoopBack Explorer, what I'm going
to do first is to log in as the admin.

255
00:21:08,437 --> 00:21:15,960
So we will login as the admin by
going to the /Customers/login.

256
00:21:15,960 --> 00:21:23,684
And then here we will type in username,

257
00:21:27,188 --> 00:21:33,581
Admin, Password,

258
00:21:36,667 --> 00:21:41,781
Password, And log ourselves in,

259
00:21:41,781 --> 00:21:46,655
and once we have logged in,
I'm just going to copy this ID,

260
00:21:46,655 --> 00:21:49,910
because this is the access token.

261
00:21:49,910 --> 00:21:55,074
And I'm going to go in here, and
then set the access token with

262
00:21:55,074 --> 00:22:00,461
this new access token that I
have just obtained, after that,

263
00:22:02,944 --> 00:22:07,223
Let me post a few dishes to my server, so

264
00:22:07,223 --> 00:22:10,983
now that I am logged in as an admin,

265
00:22:10,983 --> 00:22:16,250
I should be able to post
dishes to my server.

266
00:22:16,250 --> 00:22:21,320
So I'll open post, and recall that
I had given you the DB.json file,

267
00:22:21,320 --> 00:22:26,930
so we can just copy and
paste the dishes into the data field here.

268
00:22:26,930 --> 00:22:32,107
This is my first dish,
and the moment I post,

269
00:22:32,107 --> 00:22:37,842
it will come back with
the dish information here.

270
00:22:37,842 --> 00:22:42,680
And note that it automatically adds in
the createdAt and updatedAt fields here.

271
00:22:58,252 --> 00:23:04,798
This is my second dish, so finally,
I have completed inputting all the dishes,

272
00:23:04,798 --> 00:23:10,770
all the leaders, and all the promotions,
and we are all done.

273
00:23:10,770 --> 00:23:13,400
So let's now go ahead and

274
00:23:13,400 --> 00:23:19,100
then register a new
customer into the system.

275
00:23:19,100 --> 00:23:23,910
So to do that, let me remove this

276
00:23:23,910 --> 00:23:28,970
access token, and then we'll register
a new customer into the system.

277
00:23:28,970 --> 00:23:35,640
So to do that, we will go to post
customers, and then for the data,

278
00:23:35,640 --> 00:23:42,560
let me just click on this example
value here, and then post it in here.

279
00:23:42,560 --> 00:23:46,226
And for the data, the first name,

280
00:23:46,226 --> 00:23:50,788
let me register myself with my first name.

281
00:23:50,788 --> 00:23:54,848
So this would be a non-admin

282
00:23:54,848 --> 00:23:59,760
user that I am registering, and,

283
00:24:04,270 --> 00:24:12,430
And this email verified I should removed
this from here, But I should include the,

284
00:24:16,215 --> 00:24:22,380
password field in here, So
that I can register the user.

285
00:24:22,380 --> 00:24:26,387
So I'm registering a new
ordinary user in here.

286
00:24:26,387 --> 00:24:32,422
So when I register it comes back to
confirm that this new registered

287
00:24:32,422 --> 00:24:37,880
user is added in, and
also gives me an ID for that user.

288
00:24:37,880 --> 00:24:43,475
Now let me log in as this registered
user and then post a couple of comments.

289
00:24:45,510 --> 00:24:50,757
So to log in as the registered user,
I will go down to the customer login,

290
00:24:50,757 --> 00:24:56,822
and for the username,
I'll type in my name and

291
00:24:56,822 --> 00:25:03,440
the password and then when I login,
it'll come back and give me an ID.

292
00:25:03,440 --> 00:25:09,281
Now note that this particular user
is an ordinary user not an admin,

293
00:25:09,281 --> 00:25:15,130
so this user can only post comments and
edit his or her own comments.

294
00:25:15,130 --> 00:25:19,030
And or post-favorites and
edit his or her own favorites.

295
00:25:19,030 --> 00:25:23,806
So, when you post the user, you will see
that it is the userId returned here,

296
00:25:23,806 --> 00:25:25,511
shis is the ID for the user.

297
00:25:25,511 --> 00:25:29,921
So, we would need this
whenever we post comments or

298
00:25:29,921 --> 00:25:32,800
whenever we post our favorites.

299
00:25:34,030 --> 00:25:38,540
So, to post a comment for this user, so

300
00:25:38,540 --> 00:25:42,330
to post a comment, let me post a couple
of comments on behalf of this user.

301
00:25:42,330 --> 00:25:47,400
So, we'll go to Comments, and
then we'll post some comments here.

302
00:25:47,400 --> 00:25:50,334
So, to post a comment as you notice,

303
00:25:50,334 --> 00:25:54,134
we need to supply three
pieces of information.

304
00:25:54,134 --> 00:25:59,770
We need to supply the customerId,
the dishesId, the rating and the comment.

305
00:25:59,770 --> 00:26:03,180
So for the customerId,
we just logged in, so

306
00:26:03,180 --> 00:26:07,060
if you go to the customers and
down here at the bottom.

307
00:26:07,060 --> 00:26:11,939
We will get the customerId here, so
let me copy the userId from here.

308
00:26:11,939 --> 00:26:18,752
And then go up into the comments
posting section and

309
00:26:18,752 --> 00:26:23,673
then paste the customerId in there.

310
00:26:23,673 --> 00:26:25,830
Let me now go and find a dish.

311
00:26:27,470 --> 00:26:34,723
So if you go to, Dishes and
then you do a get on the dishes,

312
00:26:34,723 --> 00:26:39,538
you'll get all the dishes
that are in the system.

313
00:26:39,538 --> 00:26:45,235
So from this let me select the,

314
00:26:47,635 --> 00:26:51,970
First dish, so
I'll just take the ID of the dish.

315
00:26:51,970 --> 00:26:55,087
Then to post the comments,
we'll just go to Comments,

316
00:26:56,610 --> 00:26:59,960
Post where we are composing our comment.

317
00:26:59,960 --> 00:27:03,230
And then I'll paste
the dish's ID in there.

318
00:27:03,230 --> 00:27:08,440
And for the rating,
I will change this rating to 4 and,

319
00:27:14,109 --> 00:27:20,377
Just post, This comment,

320
00:27:20,377 --> 00:27:24,900
so I've posted one comment for
this particular dish.

321
00:27:24,900 --> 00:27:28,030
Let me post one more comment for
another dish.

322
00:27:30,614 --> 00:27:32,710
So let me get hold of this second dish.

323
00:27:38,222 --> 00:27:44,548
So here's the ID of the second dish so,
again, going back to Comments.

324
00:27:49,110 --> 00:27:51,699
Let me change the dishId here.

325
00:27:51,699 --> 00:27:56,874
And then, Let me change the comments so

326
00:27:56,874 --> 00:28:01,976
that you see that this is a different
comment for a different dish.

327
00:28:01,976 --> 00:28:07,182
When I post the comment, you would see
that it'll come back with this reply here,

328
00:28:07,182 --> 00:28:11,422
with the rating and the specific dish and
the customerId and so on.

329
00:28:11,422 --> 00:28:14,572
So I have now posted two comments for
two different dishes here.

330
00:28:16,113 --> 00:28:23,040
Let me,
Make these two as my favorite dishes.

331
00:28:23,040 --> 00:28:25,750
So to do that, let me go to Favorites.

332
00:28:35,658 --> 00:28:38,350
Going down to favorites.

333
00:28:38,350 --> 00:28:40,310
Let me post a couple of favorites.

334
00:28:40,310 --> 00:28:46,327
So to post a favorite again,
I go into the post favorites here.

335
00:28:46,327 --> 00:28:52,140
Then I click on this and
then I'll paste in the one dishId,

336
00:28:52,140 --> 00:28:58,073
for the customerId,
let me just go up to the customer here,

337
00:28:58,073 --> 00:29:01,295
where I logged in the customer.

338
00:29:01,295 --> 00:29:04,402
So we'll get the userId from there.

339
00:29:04,402 --> 00:29:08,379
And this is one customerId, so
let me go to the Favorites.

340
00:29:11,044 --> 00:29:16,398
And then, In the Favorites

341
00:29:16,398 --> 00:29:22,245
let me test the customerId here, and
then post this into my favorites.

342
00:29:22,245 --> 00:29:26,831
So when I post that into my favorites
it replies back with this so

343
00:29:26,831 --> 00:29:32,997
you can see that the dishId and customerId
and the favoriteId have been posted here.

344
00:29:32,997 --> 00:29:36,067
Let me post one more dish to my
favorites so let me go to the,

345
00:29:40,771 --> 00:29:43,675
Dishes here I have another dish.

346
00:29:43,675 --> 00:29:49,734
So let me copy the, ID of the dish and
go to my favorites and

347
00:29:49,734 --> 00:29:57,140
then replace the dishe's ID with
that new dish, and then post that.

348
00:29:57,140 --> 00:30:01,004
So now I should have two
favorites in my group.

349
00:30:03,416 --> 00:30:08,097
Wonderful, so
now we have a couple of favorites,

350
00:30:08,097 --> 00:30:13,454
a bunch of dishes,
a couple of comments, all posted in.

351
00:30:13,454 --> 00:30:15,168
Now how is this useful?

352
00:30:15,168 --> 00:30:22,554
Let me, Close all this and
then will show you how this can be useful.

353
00:30:22,554 --> 00:30:27,238
So, now when I do get dishes,
so for example,

354
00:30:27,238 --> 00:30:31,096
when I want to forget a specific dish.

355
00:30:34,617 --> 00:30:41,440
Let me get this first dish here,
let me copy the ID of the dish and,

356
00:30:47,206 --> 00:30:50,900
I can retrieve that specific dish.

357
00:30:50,900 --> 00:30:55,089
So here you see that I'm
going to /dishes/Id so

358
00:30:55,089 --> 00:30:59,489
here, when I want to
retrieve that specific dish.

359
00:30:59,489 --> 00:31:04,263
I can put the dishId there and
then try it out and

360
00:31:04,263 --> 00:31:07,595
then here I get the dishId here.

361
00:31:07,595 --> 00:31:12,850
Now, if I want to include the comments
from the dish, then in the filter,

362
00:31:15,876 --> 00:31:20,814
I should type in include in quotes and

363
00:31:20,814 --> 00:31:25,286
then within brackets I specify,

364
00:31:27,990 --> 00:31:29,850
The name of,

365
00:31:33,392 --> 00:31:38,793
The relation that I want
to include into that,

366
00:31:38,793 --> 00:31:42,540
so when I say include comments.

367
00:31:42,540 --> 00:31:47,490
So this is like the Mongols populate,
it acts similarly.

368
00:31:47,490 --> 00:31:54,666
So when I say include comments and
then retrieve the dish,

369
00:31:54,666 --> 00:31:59,651
notice that when the dish is retrieved,

370
00:32:01,219 --> 00:32:05,350
Here, you get the dish but
also you get an array Of comments here.

371
00:32:06,520 --> 00:32:11,395
So that is how you can fill in
the details from the relation

372
00:32:11,395 --> 00:32:15,160
that you have with another element here.

373
00:32:15,160 --> 00:32:18,830
Now, similarly,
going to comments, for example.

374
00:32:18,830 --> 00:32:24,365
In the comments also, when I retrieve the
comments, I can specify within the filter.

375
00:32:24,365 --> 00:32:29,137
So when I just say GET comments,
it'll bring

376
00:32:29,137 --> 00:32:34,460
back all the comments
that have been posted.

377
00:32:34,460 --> 00:32:40,780
Now, if I want to fill in the dishId and
customerId information, I can say filter.

378
00:32:40,780 --> 00:32:45,675
In the filter, I can say include.

379
00:32:45,675 --> 00:32:50,682
And then within brackets,
I can say dishes because

380
00:32:50,682 --> 00:32:57,436
that's the relationship that I have
defined within the comments and

381
00:32:57,436 --> 00:33:01,761
the dishes, and then dishes and customer.

382
00:33:01,761 --> 00:33:06,628
And when I retrieve this information, you
will see that in the returned comments,

383
00:33:06,628 --> 00:33:10,311
for each comment, you will notice
that the comment is given and

384
00:33:10,311 --> 00:33:14,066
then the corresponding dish
information is also filled in, and

385
00:33:14,066 --> 00:33:18,990
the corresponding customer information is
also filled in for each of the comment.

386
00:33:18,990 --> 00:33:25,040
So that way, we can now populate the two
relationships that we have defined from

387
00:33:25,040 --> 00:33:30,030
the dishes automatically in here,
as shown here.

388
00:33:30,030 --> 00:33:33,662
Similarly, for the favorites,

389
00:33:33,662 --> 00:33:37,943
when we are retrieving the favorites,

390
00:33:37,943 --> 00:33:42,886
we'll say favorites and
a specific ID here.

391
00:33:42,886 --> 00:33:46,084
And then we can then filter, so

392
00:33:46,084 --> 00:33:50,910
that specific favorite can be retrieved.

393
00:33:50,910 --> 00:33:55,067
You can't retrieve all the favorites
because they could be favorites from other

394
00:33:55,067 --> 00:33:58,992
users also in that list, so we won't be
able to do a GET on all the favorites.

395
00:33:58,992 --> 00:34:03,950
But instead, when we retrieve this,
you can then ask for

396
00:34:03,950 --> 00:34:09,650
the information to be filled in
here based on the relationships.

397
00:34:09,650 --> 00:34:14,403
So we can say, in quote, ("include": and
written square brackets.

398
00:34:14,403 --> 00:34:21,605
We can say ["dishes", "customer"]),

399
00:34:25,296 --> 00:34:29,382
And retrieve the information.

400
00:34:29,382 --> 00:34:32,828
So you would notice that here,
in the Response Body,

401
00:34:32,828 --> 00:34:36,583
you get the favorite, but
also the corresponding dish and

402
00:34:36,583 --> 00:34:40,700
the corresponding customer's
information filled in here.

403
00:34:40,700 --> 00:34:45,720
So that's how we can retrieve
additional information

404
00:34:45,720 --> 00:34:50,410
from our server site as required.

405
00:34:50,410 --> 00:34:53,194
With this, we complete this exercise.

406
00:34:53,194 --> 00:34:59,730
In this exercise, I have demonstrated to
you how we can define various relations

407
00:34:59,730 --> 00:35:04,920
and then be able to retrieve information,
and also use this include

408
00:35:04,920 --> 00:35:10,250
filter in order to include the additional
information of that relationship into

409
00:35:10,250 --> 00:35:16,210
the document when you retrieve
the document or a set of documents.

410
00:35:16,210 --> 00:35:18,880
With this, we complete this exercise.

411
00:35:18,880 --> 00:35:23,400
So in these three exercises that we
have done so far with LoopBack, I have

412
00:35:23,400 --> 00:35:29,397
illustrated various aspects of how you can
configure and build your LoopBack server.

413
00:35:29,397 --> 00:35:32,720
With this, we complete this exercise.

414
00:35:32,720 --> 00:35:33,974
This is a good time for

415
00:35:33,974 --> 00:35:37,542
you to do a GET comment with
the message LoopBack relations.

416
00:35:37,542 --> 00:35:43,900
[MUSIC]