1
00:00:01,693 --> 00:00:04,860
Now, this was our first API,

2
00:00:04,860 --> 00:00:07,810
our first basic API that we built.

3
00:00:07,810 --> 00:00:10,620
Now, we will move on to a different example,

4
00:00:10,620 --> 00:00:12,810
a different API which we'll build,

5
00:00:12,810 --> 00:00:15,110
which will be a little bit more complex

6
00:00:15,110 --> 00:00:19,163
and where we will see more than just GET routes in action.

7
00:00:20,040 --> 00:00:21,470
Here, we are going to build

8
00:00:21,470 --> 00:00:24,410
a Simple Todo Management Web API,

9
00:00:24,410 --> 00:00:26,650
and therefore in the end Web App

10
00:00:26,650 --> 00:00:28,660
because I will later also provide you

11
00:00:28,660 --> 00:00:30,350
with a decoupled frontend,

12
00:00:30,350 --> 00:00:33,180
but we're going to start with the API.

13
00:00:33,180 --> 00:00:36,110
And the goal of this API which we build

14
00:00:36,110 --> 00:00:40,263
is that we allow users of this API to manage todos.

15
00:00:41,210 --> 00:00:43,450
For this, we'll have a GET route to /todos

16
00:00:44,780 --> 00:00:47,210
which should retrieve and return a list

17
00:00:47,210 --> 00:00:50,390
of all the todos that are stored in the database.

18
00:00:50,390 --> 00:00:52,260
And for this API here,

19
00:00:52,260 --> 00:00:56,960
we can say that a todo is simply a document, an object,

20
00:00:56,960 --> 00:01:00,423
which contains the todo text and a unique id.

21
00:01:01,410 --> 00:01:05,650
We'll have a POST /todos route, an endpoint therefore,

22
00:01:05,650 --> 00:01:08,090
which will accept incoming data,

23
00:01:08,090 --> 00:01:10,620
so where the request should contain some data

24
00:01:10,620 --> 00:01:13,530
in the request body in the JSON format,

25
00:01:13,530 --> 00:01:14,800
which we will parse

26
00:01:14,800 --> 00:01:18,270
and then store as a new todo in the database.

27
00:01:18,270 --> 00:01:21,120
And, of course, you wanna return a success response,

28
00:01:21,120 --> 00:01:25,890
maybe with that newly created todo in the response body.

29
00:01:25,890 --> 00:01:28,100
And then I got two more routes,

30
00:01:28,100 --> 00:01:31,870
for example, a PATCH route where we target a specific todo

31
00:01:31,870 --> 00:01:36,750
with /todos and then slash the id of a specific todo

32
00:01:36,750 --> 00:01:39,333
to update the text of an existing todo,

33
00:01:40,470 --> 00:01:45,470
and we'll have a DELETE /todos/ some specific id route

34
00:01:46,010 --> 00:01:48,610
which allows us to delete a todo,

35
00:01:48,610 --> 00:01:51,860
so that we have all these CRUD operations at this example

36
00:01:51,860 --> 00:01:54,010
of this basic todos app here

37
00:01:54,010 --> 00:01:57,240
or of this basic todos API here.

38
00:01:57,240 --> 00:02:00,720
Now, that's the next API we'll build, and as I mentioned,

39
00:02:00,720 --> 00:02:04,190
later I will also provide you with a fitting frontend,

40
00:02:04,190 --> 00:02:08,460
some JavaScript code, browser-side JavaScript code,

41
00:02:08,460 --> 00:02:10,610
that will talk to this API,

42
00:02:10,610 --> 00:02:12,070
but that will be the next step.

43
00:02:12,070 --> 00:02:14,570
First of all, we're going to build it.

44
00:02:14,570 --> 00:02:16,550
Now, to build this API,

45
00:02:16,550 --> 00:02:21,550
I got a brand new project here which you also find attached

46
00:02:21,600 --> 00:02:24,740
where I got an empty models and controllers

47
00:02:24,740 --> 00:02:26,130
and routes folder.

48
00:02:26,130 --> 00:02:29,420
You'll have to create those folders on your own.

49
00:02:29,420 --> 00:02:30,300
In the data folder,

50
00:02:30,300 --> 00:02:33,700
I got the database.js file where we connect to a database

51
00:02:33,700 --> 00:02:37,690
now to the second-api database on the database server,

52
00:02:37,690 --> 00:02:40,090
which you therefore, of course, need to start.

53
00:02:40,090 --> 00:02:43,450
You need to have MongoDB installed and started.

54
00:02:43,450 --> 00:02:46,260
And then app.js has just got some basic setup.

55
00:02:46,260 --> 00:02:49,540
I got no routes yet because we'll add those together,

56
00:02:49,540 --> 00:02:52,290
and I also got no model and nothing like this.

57
00:02:52,290 --> 00:02:55,510
We're all going to add this together.

58
00:02:55,510 --> 00:02:57,580
Now, if you download this snapshot,

59
00:02:57,580 --> 00:02:59,480
you will have to run npm install

60
00:02:59,480 --> 00:03:02,500
so that you get this node_modules folder.

61
00:03:02,500 --> 00:03:04,450
And then you could run npm start,

62
00:03:04,450 --> 00:03:06,840
but, first of all, I'm going to write some code here

63
00:03:06,840 --> 00:03:10,553
and add some logic to this web API before I'll start it.

64
00:03:11,520 --> 00:03:16,180
And I'll start by adding a model, the todo.model.js file,

65
00:03:16,180 --> 00:03:19,020
where I wanna add a Todo class

66
00:03:20,130 --> 00:03:22,573
which helps us store and create todos.

67
00:03:23,810 --> 00:03:26,430
For this here, I will add a constructor

68
00:03:26,430 --> 00:03:28,900
which is this method which is called automatically

69
00:03:28,900 --> 00:03:32,470
when a new instance of this class is created.

70
00:03:32,470 --> 00:03:35,480
And here I expect to get the todo text

71
00:03:35,480 --> 00:03:37,010
so that in the constructor,

72
00:03:37,010 --> 00:03:42,010
I can add it as a text property to the to-be created object.

73
00:03:42,330 --> 00:03:45,540
And, of course, we can already also export this Todo class

74
00:03:45,540 --> 00:03:48,123
so that later we can use it outside of this file.

75
00:03:49,550 --> 00:03:52,550
Now, eventually, I want to be able to save my todos

76
00:03:52,550 --> 00:03:57,550
which exist as objects in my running web application here

77
00:03:57,570 --> 00:03:58,940
to the database.

78
00:03:58,940 --> 00:04:00,870
And for this, I'll add a save method

79
00:04:00,870 --> 00:04:04,290
as we did it before in the course for other examples

80
00:04:04,290 --> 00:04:06,490
like the online shop as well.

81
00:04:06,490 --> 00:04:08,150
And in this save method,

82
00:04:08,150 --> 00:04:10,970
I, of course, wanna reach out to the database

83
00:04:10,970 --> 00:04:14,330
so I will actually require my database file

84
00:04:14,330 --> 00:04:16,089
from the data folder here

85
00:04:16,089 --> 00:04:19,300
and import this db object

86
00:04:19,300 --> 00:04:23,000
so that here I can then run get.Db,

87
00:04:23,000 --> 00:04:27,780
reach out to a collection, let's say the todos collection,

88
00:04:27,780 --> 00:04:32,780
and then there, I want to insert one new todo.

89
00:04:34,890 --> 00:04:37,270
Now, for this todo which I wanna insert,

90
00:04:37,270 --> 00:04:39,810
I only wanna add a text property

91
00:04:39,810 --> 00:04:42,220
to this document that should be inserted,

92
00:04:42,220 --> 00:04:44,633
and I'll set this equal to this.text.

93
00:04:46,040 --> 00:04:47,770
Now, this will return a promise,

94
00:04:47,770 --> 00:04:50,370
and hence we could turn this into a async function

95
00:04:50,370 --> 00:04:51,820
and await this,

96
00:04:51,820 --> 00:04:55,360
but here I'll just return this so that I return this promise

97
00:04:55,360 --> 00:04:57,570
which is returned by this line of code

98
00:04:57,570 --> 00:04:59,393
as part of this save method.

99
00:05:00,890 --> 00:05:03,380
Of course, this is actually only the correct code

100
00:05:03,380 --> 00:05:05,240
if we're creating a new todo,

101
00:05:05,240 --> 00:05:09,540
so I also want to have code that updates an existing todo,

102
00:05:09,540 --> 00:05:13,293
just as we also had it before in other parts of this course.

103
00:05:14,230 --> 00:05:15,700
For this in a constructor,

104
00:05:15,700 --> 00:05:19,170
I will actually accept an id as a second parameter,

105
00:05:19,170 --> 00:05:21,580
though this might not always be provided.

106
00:05:21,580 --> 00:05:25,280
If we create a todo for the first time, we won't have an id,

107
00:05:25,280 --> 00:05:28,690
hence no value for this parameter will be provided,

108
00:05:28,690 --> 00:05:31,700
and therefore id will be undefined,

109
00:05:31,700 --> 00:05:35,980
but I will still store it, and it might be defined.

110
00:05:35,980 --> 00:05:37,910
And because it might be defined

111
00:05:37,910 --> 00:05:40,010
but isn't guaranteed to be defined,

112
00:05:40,010 --> 00:05:43,890
here in the save method, I'll check if this id is truthy,

113
00:05:43,890 --> 00:05:46,040
which means it does exist,

114
00:05:46,040 --> 00:05:48,510
which means I wanna update an existing todo

115
00:05:48,510 --> 00:05:52,170
because I have an id already and I'm calling save

116
00:05:52,170 --> 00:05:54,533
so I must be updating an existing todo.

117
00:05:55,680 --> 00:05:58,070
Else, if we have no id and we call save,

118
00:05:58,070 --> 00:05:59,620
we are creating a new todo,

119
00:05:59,620 --> 00:06:02,620
and that's therefore when I wanna execute this line of code.

120
00:06:03,840 --> 00:06:06,220
Here in this first branch of the if block,

121
00:06:06,220 --> 00:06:08,993
I will instead return db.getDb,

122
00:06:09,870 --> 00:06:13,060
reaching out to the todos collection as always,

123
00:06:13,060 --> 00:06:16,313
and then here we call updateOne to update a todo.

124
00:06:18,340 --> 00:06:20,520
Now, for this, I need the id of the todo,

125
00:06:20,520 --> 00:06:23,802
and I'll transform this to a mongodb.ObjectId

126
00:06:23,802 --> 00:06:26,260
so that we'll never run into problems there,

127
00:06:26,260 --> 00:06:31,260
and hence I'll require mongodb here in this file.

128
00:06:32,450 --> 00:06:33,960
And then in the save method,

129
00:06:33,960 --> 00:06:38,150
we can get our todoId as an object id

130
00:06:38,150 --> 00:06:41,720
by using new mongodb.ObjectId

131
00:06:41,720 --> 00:06:43,170
and passing this id,

132
00:06:43,170 --> 00:06:46,830
which we know exists in this part here, in this if branch,

133
00:06:46,830 --> 00:06:49,110
to object id.

134
00:06:49,110 --> 00:06:51,480
So then I get the todo as an object id,

135
00:06:51,480 --> 00:06:55,610
and hence here for updateOne for the first-parameter value,

136
00:06:55,610 --> 00:06:58,670
I described my filtering condition,

137
00:06:58,670 --> 00:07:02,970
and I'm looking for a document where _id in the database,

138
00:07:02,970 --> 00:07:07,630
because that's the default id field MongoDB will add

139
00:07:07,630 --> 00:07:11,160
to every document that's inserted in the database,

140
00:07:11,160 --> 00:07:14,467
so where _id is equal to todoId.

141
00:07:16,310 --> 00:07:20,140
And then with the second-parameter value here for updateOne,

142
00:07:20,140 --> 00:07:22,080
we describe the update,

143
00:07:22,080 --> 00:07:27,080
and here I'll just add this special $set key

144
00:07:27,370 --> 00:07:30,090
where we then pass an object that describes

145
00:07:30,090 --> 00:07:33,030
which keys or which fields in the document

146
00:07:33,030 --> 00:07:36,350
should be updated with which values.

147
00:07:36,350 --> 00:07:38,650
And here I wanna update the text field

148
00:07:38,650 --> 00:07:42,600
in the document that was identified with this condition

149
00:07:42,600 --> 00:07:45,563
with the text that's stored in this text.

150
00:07:46,480 --> 00:07:48,883
So that's then my updating logic here.

151
00:07:51,450 --> 00:07:54,010
With that, I got this save method.

152
00:07:54,010 --> 00:07:57,333
Of course, we will also need a method for deleting a todo,

153
00:07:58,170 --> 00:08:02,350
and therefore I'll add a delete method here at the bottom,

154
00:08:02,350 --> 00:08:05,070
though you know the position doesn't matter.

155
00:08:05,070 --> 00:08:08,230
And here we could check if we maybe don't have an id,

156
00:08:08,230 --> 00:08:10,590
which means we try to delete a todo

157
00:08:10,590 --> 00:08:13,420
which doesn't exist in the database yet

158
00:08:13,420 --> 00:08:16,230
in which case we could throw a new error,

159
00:08:16,230 --> 00:08:18,530
though that's totally optional,

160
00:08:18,530 --> 00:08:23,530
where I say Trying to delete todo without id!

161
00:08:24,320 --> 00:08:27,400
It is a case that actually shouldn't occur in our API

162
00:08:27,400 --> 00:08:29,730
since we will write code that ensures

163
00:08:29,730 --> 00:08:33,960
that we only call delete on todos that we do have,

164
00:08:33,960 --> 00:08:36,823
but nonetheless this is extra check you could add.

165
00:08:38,039 --> 00:08:40,549
More importantly, though, after this check,

166
00:08:40,549 --> 00:08:43,190
we wanna actually delete the todo,

167
00:08:43,190 --> 00:08:45,180
and therefore what I'll do here

168
00:08:45,180 --> 00:08:48,453
is I will return db.getDb,

169
00:08:49,630 --> 00:08:53,810
reaching out to the todos collection as always,

170
00:08:53,810 --> 00:08:58,100
and here we can call deleteOne to delete one document,

171
00:08:58,100 --> 00:09:00,700
and we need to provide our filtering condition here.

172
00:09:01,610 --> 00:09:03,280
And now just as before,

173
00:09:03,280 --> 00:09:06,880
for this, I wanna create a todoId as an object id,

174
00:09:06,880 --> 00:09:10,860
so I will copy this line of code and add that here.

175
00:09:10,860 --> 00:09:13,000
And then I wanna delete the todo

176
00:09:13,000 --> 00:09:17,343
where _id is equal to this.todoId, like that.

177
00:09:20,098 --> 00:09:23,550
So that's the delete method which should do the trick.

178
00:09:23,550 --> 00:09:28,550
Now, we also wanna add static methods for fetching todos

179
00:09:29,050 --> 00:09:32,153
or one static method for fetching all the todos.

180
00:09:33,280 --> 00:09:35,920
For this, I'll add a static method getTodos

181
00:09:37,220 --> 00:09:41,330
or getAllTodos, however you wanna name it.

182
00:09:41,330 --> 00:09:45,553
And here I return db.getDb collection todos,

183
00:09:46,750 --> 00:09:49,730
and we simply find all todos here,

184
00:09:49,730 --> 00:09:53,210
but, actually, I wanna do a bit more than that.

185
00:09:53,210 --> 00:09:55,740
I also wanna get them as an array,

186
00:09:55,740 --> 00:09:57,930
as we did it before in the course as well,

187
00:09:57,930 --> 00:10:01,240
but I also wanna transform all these todo documents

188
00:10:01,240 --> 00:10:03,050
which we get from the collection

189
00:10:03,050 --> 00:10:07,380
into objects that are instantiated like this,

190
00:10:07,380 --> 00:10:09,943
so that are instantiated with help of this class.

191
00:10:11,250 --> 00:10:14,980
Therefore, I will actually turn this into a async method

192
00:10:14,980 --> 00:10:18,530
so that I can get my todoDocuments here

193
00:10:18,530 --> 00:10:20,900
by awaiting this operation,

194
00:10:20,900 --> 00:10:23,753
and then in the next step, I can transform them all.

195
00:10:24,800 --> 00:10:29,380
So here we can then return todoDocuments and call map.

196
00:10:29,380 --> 00:10:32,170
You learned that the map method is a built-in method

197
00:10:32,170 --> 00:10:34,530
that exists on JavaScript arrays

198
00:10:34,530 --> 00:10:37,833
which allows us to transform all the elements in an array.

199
00:10:39,190 --> 00:10:41,870
And for this, we get an anonymous function here

200
00:10:41,870 --> 00:10:43,220
which gets the todoDocument

201
00:10:44,460 --> 00:10:46,940
because this function will automatically execute

202
00:10:46,940 --> 00:10:49,299
for all the todoDocuments.

203
00:10:49,299 --> 00:10:51,950
And then here, in this anonymous function,

204
00:10:51,950 --> 00:10:54,490
I return my transformed object.

205
00:10:54,490 --> 00:10:58,800
And for this, I'll instantiate my class here, my Todo class,

206
00:10:58,800 --> 00:11:00,440
with new Todo,

207
00:11:00,440 --> 00:11:05,440
and I pass in these two constructor values, text and id,

208
00:11:05,880 --> 00:11:09,910
which is todoDocument._id

209
00:11:09,910 --> 00:11:13,363
and todoDocument.text,

210
00:11:14,680 --> 00:11:15,513
like this.

211
00:11:16,840 --> 00:11:20,350
So this will now return these transformed todos,

212
00:11:20,350 --> 00:11:23,233
and that's my getAllTodos method which I need here.

213
00:11:25,240 --> 00:11:28,460
With that done, the model should be finished.

214
00:11:28,460 --> 00:11:30,920
We'll see if we need to fix an error later.

215
00:11:30,920 --> 00:11:32,580
It looks good though.

216
00:11:32,580 --> 00:11:34,570
Now, we can add a controller,

217
00:11:34,570 --> 00:11:38,640
the todos.controller.js file here,

218
00:11:38,640 --> 00:11:43,500
where I wanna add a function for getting all the todos

219
00:11:45,680 --> 00:11:50,200
and then also a function for adding a todo,

220
00:11:50,200 --> 00:11:52,010
so I'll add all these actions right now,

221
00:11:52,010 --> 00:11:55,800
and we're then going to populate them with more life later,

222
00:11:55,800 --> 00:11:57,693
then a function for updating a todo,

223
00:11:58,770 --> 00:12:03,770
and then also a function for deleting a todo,

224
00:12:03,950 --> 00:12:05,693
so for these CRUD operations.

225
00:12:06,930 --> 00:12:10,580
And, of course, all these functions should be exposed,

226
00:12:10,580 --> 00:12:12,750
so I will export an object here

227
00:12:12,750 --> 00:12:15,210
where I will add all these functions as keys

228
00:12:15,210 --> 00:12:17,290
and then point at all these functions

229
00:12:17,290 --> 00:12:21,800
so that they are available outside of this file here as well

230
00:12:21,800 --> 00:12:26,223
with addTodo, updateTodo, deleteTodo, and so on.

231
00:12:28,160 --> 00:12:30,150
And then, of course, all these functions

232
00:12:30,150 --> 00:12:32,690
will get request, response, and next

233
00:12:32,690 --> 00:12:34,830
because they are controller actions

234
00:12:34,830 --> 00:12:37,550
and therefore middleware functions.

235
00:12:37,550 --> 00:12:41,340
And we will need that todo.model which we just worked on

236
00:12:41,340 --> 00:12:45,523
so we can require that from /models/todo.model.

237
00:12:47,730 --> 00:12:48,563
And then with that,

238
00:12:48,563 --> 00:12:51,320
then here, for example, for getting all the todos,

239
00:12:51,320 --> 00:12:56,320
what we can do is we can turn this into a async function

240
00:12:56,800 --> 00:13:01,800
so that we can get our todos by awaiting Todo.getAllTodos.

241
00:13:04,350 --> 00:13:07,420
And we might wanna put this into a try-catch block

242
00:13:07,420 --> 00:13:09,580
because, of course, since we're interacting

243
00:13:09,580 --> 00:13:10,930
with the database here,

244
00:13:10,930 --> 00:13:13,250
this could be failing,

245
00:13:13,250 --> 00:13:14,950
so I wanna catch errors

246
00:13:14,950 --> 00:13:18,113
and return next error here if we have an error.

247
00:13:19,403 --> 00:13:22,340
Otherwise, thereafter, I wanna use my todos.

248
00:13:22,340 --> 00:13:25,930
So, as always, I'll actually turn this into a variable

249
00:13:25,930 --> 00:13:28,440
so that we have the right scope.

250
00:13:28,440 --> 00:13:29,330
And then here,

251
00:13:29,330 --> 00:13:32,390
I can use the res object to call the json method

252
00:13:32,390 --> 00:13:35,263
to send back a response in the JSON format.

253
00:13:36,560 --> 00:13:39,130
And the data which I wanna send back here

254
00:13:39,130 --> 00:13:41,020
actually is my todos array,

255
00:13:41,020 --> 00:13:44,990
but I'll actually send back an object with a todos key

256
00:13:44,990 --> 00:13:47,520
which then holds this todos array.

257
00:13:47,520 --> 00:13:48,780
We could have also send back

258
00:13:48,780 --> 00:13:52,060
the todos array itself like this.

259
00:13:52,060 --> 00:13:53,180
This would also work

260
00:13:53,180 --> 00:13:56,640
because just an array is also valid JSON.

261
00:13:56,640 --> 00:13:58,890
But here I'll wrap it into an extra object

262
00:13:58,890 --> 00:14:02,863
and send back the array under a todos key in that object.

263
00:14:04,710 --> 00:14:06,660
And that should fetch us all the todos.

264
00:14:07,640 --> 00:14:09,240
Now, for adding a todo here,

265
00:14:09,240 --> 00:14:11,320
that's the next action I wanna work on,

266
00:14:11,320 --> 00:14:13,940
I, of course, need to get my todoText

267
00:14:13,940 --> 00:14:16,973
which I wanna get from req.body.text like this,

268
00:14:17,939 --> 00:14:21,775
assuming that the incoming request body will have

269
00:14:21,775 --> 00:14:22,858
a text field.

270
00:14:23,804 --> 00:14:25,193
So we'll have to make sure

271
00:14:25,193 --> 00:14:28,092
that when we send a request to our API later

272
00:14:28,092 --> 00:14:30,588
that this POST request has a request body

273
00:14:30,588 --> 00:14:33,310
with a text field inside of it.

274
00:14:33,310 --> 00:14:34,143
And then with that,

275
00:14:34,143 --> 00:14:38,050
we can, of course, create our todo with new Todo

276
00:14:38,050 --> 00:14:40,690
and pass the todoText to it.

277
00:14:40,690 --> 00:14:42,500
The second-parameter value,

278
00:14:42,500 --> 00:14:44,480
the id which we could also pass in,

279
00:14:44,480 --> 00:14:45,820
is not passed in

280
00:14:45,820 --> 00:14:48,870
because here we are creating a todo for the first time,

281
00:14:48,870 --> 00:14:51,033
and therefore, of course, we have no id.

282
00:14:52,360 --> 00:14:54,500
But with that, we got this todo,

283
00:14:54,500 --> 00:14:56,913
and now we can call todo.save here.

284
00:14:57,930 --> 00:14:59,690
This, of course, returns a promise,

285
00:14:59,690 --> 00:15:00,830
and therefore, as always,

286
00:15:00,830 --> 00:15:04,730
I'll turn this into async function and await this,

287
00:15:04,730 --> 00:15:08,110
and as always, wrap this into try-catch

288
00:15:08,110 --> 00:15:11,340
to catch any potential errors we might be facing

289
00:15:11,340 --> 00:15:14,070
so that we can actually let

290
00:15:14,070 --> 00:15:17,370
Express.js' default error-handler handle this

291
00:15:17,370 --> 00:15:19,763
if something goes wrong, like this.

292
00:15:21,530 --> 00:15:23,090
Now, if nothing goes wrong,

293
00:15:23,090 --> 00:15:25,380
if we make it past this try-catch block,

294
00:15:25,380 --> 00:15:27,590
then we did create a todo,

295
00:15:27,590 --> 00:15:30,400
and then I'll send back a JSON response

296
00:15:30,400 --> 00:15:33,410
because we always send back JSON responses

297
00:15:33,410 --> 00:15:34,990
when we build such an API.

298
00:15:34,990 --> 00:15:39,110
We never send back HTML, and we never redirect

299
00:15:39,110 --> 00:15:42,010
because we don't care about what the client does.

300
00:15:42,010 --> 00:15:44,240
We just exchange data, as you learned.

301
00:15:44,240 --> 00:15:49,010
And here in this object, I'll just add a message of saying

302
00:15:49,010 --> 00:15:52,950
Added todo successfully, or anything like this.

303
00:15:52,950 --> 00:15:57,480
And I'll add a second key-value pair to this response data,

304
00:15:57,480 --> 00:15:59,520
and that's the createdTodo

305
00:15:59,520 --> 00:16:03,540
where I pass back that todo here which was created.

306
00:16:03,540 --> 00:16:06,690
Or, actually, I'll not pass it back like this.

307
00:16:06,690 --> 00:16:10,740
Instead, on that todo, I wanna set the id field

308
00:16:10,740 --> 00:16:15,740
and set this equal to the id of the todo that was inserted.

309
00:16:16,570 --> 00:16:18,480
And for this, it is very convenient

310
00:16:18,480 --> 00:16:20,880
that in the todo.model in the save method,

311
00:16:20,880 --> 00:16:24,820
we return the promise that is yielded by insertOne,

312
00:16:24,820 --> 00:16:28,230
and it turns out that the promise yielded by insertOne

313
00:16:28,230 --> 00:16:30,500
eventually results to an object

314
00:16:30,500 --> 00:16:33,500
which holds the id that was inserted.

315
00:16:33,500 --> 00:16:36,480
So therefore, here in todos.controller,

316
00:16:36,480 --> 00:16:38,180
I get the result here

317
00:16:39,702 --> 00:16:41,540
of that operation,

318
00:16:41,540 --> 00:16:46,540
and then I can add a variable, insertedId for example,

319
00:16:46,890 --> 00:16:48,948
which I can get from that result.

320
00:16:48,948 --> 00:16:50,150
So insertedId is then equal to result.insertedId.

321
00:16:53,110 --> 00:16:55,760
This field will exist on this result object

322
00:16:55,760 --> 00:16:59,410
because this result object is generated by MongoDB,

323
00:16:59,410 --> 00:17:01,210
by this MongoDB package,

324
00:17:01,210 --> 00:17:05,290
and it will hold the id of the todo that was created.

325
00:17:05,290 --> 00:17:09,960
And then I'll set todo.id equal to insertedId.toString

326
00:17:09,960 --> 00:17:12,579
to force a conversion to a regular string

327
00:17:12,579 --> 00:17:14,583
instead of having this object id.

328
00:17:15,579 --> 00:17:18,010
And therefore now, this todo which I created here,

329
00:17:18,010 --> 00:17:20,210
which doesn't have an id initially,

330
00:17:20,210 --> 00:17:21,589
will now have an id

331
00:17:21,589 --> 00:17:25,680
because I set that id after I inserted it into the database,

332
00:17:25,680 --> 00:17:30,140
and therefore once I have that automatically created id,

333
00:17:30,140 --> 00:17:32,560
and therefore now it's this updated todo

334
00:17:32,560 --> 00:17:35,593
which now contains an id which I will return here.

335
00:17:38,060 --> 00:17:40,720
Now, let's ignore update and delete for now.

336
00:17:40,720 --> 00:17:43,610
These are two important controller actions already

337
00:17:43,610 --> 00:17:46,283
which allow us to create new todos and to get todos,

338
00:17:47,170 --> 00:17:50,750
and therefore now we wanna add routes for those actions

339
00:17:50,750 --> 00:17:54,313
and then also add one important middleware to app.js.

