1
00:00:02,070 --> 00:00:05,340
Now that we tested our first two endpoints,

2
00:00:05,340 --> 00:00:07,800
it's time to go back to the Todo's controller

3
00:00:07,800 --> 00:00:09,600
and add the logic for the updateTodo

4
00:00:10,737 --> 00:00:12,833
and deleteTodo actions here.

5
00:00:13,790 --> 00:00:17,570
In updateTodo, I expect to get a Todo ID

6
00:00:17,570 --> 00:00:22,147
and it will be part of the params here of my path

7
00:00:23,540 --> 00:00:25,520
because that's the idea.

8
00:00:25,520 --> 00:00:28,540
If you have a look back at the slide I showed you earlier,

9
00:00:28,540 --> 00:00:31,570
the idea for the patch and the delete routes is

10
00:00:31,570 --> 00:00:34,380
that the ID of the Todo with which we're working

11
00:00:34,380 --> 00:00:37,920
is part of the path of the incoming request.

12
00:00:37,920 --> 00:00:39,670
And you learned that you can extract

13
00:00:39,670 --> 00:00:42,930
such dynamic path parameters with req.params.

14
00:00:42,930 --> 00:00:44,763
That's a built in Express feature.

15
00:00:45,620 --> 00:00:48,400
And then, of course, we also need the new text,

16
00:00:48,400 --> 00:00:53,200
the newTodoText, which we get by accessing request body

17
00:00:53,200 --> 00:00:56,494
and then maybe, again, text or newText.

18
00:00:56,494 --> 00:00:57,680
That will be up to us

19
00:00:57,680 --> 00:01:00,180
because we are the developer of the API.

20
00:01:00,180 --> 00:01:02,010
And in the end, we just have to make sure

21
00:01:02,010 --> 00:01:04,440
that it's then used appropriately,

22
00:01:04,440 --> 00:01:07,420
either because we are the one using it anyways,

23
00:01:07,420 --> 00:01:10,010
and then of course we know which data it wants,

24
00:01:10,010 --> 00:01:12,010
or if we expose it to the public,

25
00:01:12,010 --> 00:01:13,780
if we sell it to customers,

26
00:01:13,780 --> 00:01:16,130
by providing proper documentation

27
00:01:16,130 --> 00:01:18,910
that explains how to use this API.

28
00:01:18,910 --> 00:01:21,170
So here I expect to get a new text field

29
00:01:21,170 --> 00:01:22,913
on the incoming request body.

30
00:01:24,230 --> 00:01:26,350
Now with that, we can then create the Todo

31
00:01:26,350 --> 00:01:29,872
that should be updated by using new Todo

32
00:01:29,872 --> 00:01:34,510
and to new Todo, we then pass our new Todo text

33
00:01:34,510 --> 00:01:36,530
and Todo ID.

34
00:01:36,530 --> 00:01:39,490
That's how we configured our model to work.

35
00:01:39,490 --> 00:01:41,220
Now that we got an ID here,

36
00:01:41,220 --> 00:01:44,240
if we now call save in the Todo model,

37
00:01:44,240 --> 00:01:47,430
we will update the existing Todo instead

38
00:01:47,430 --> 00:01:48,650
of creating a new one.

39
00:01:48,650 --> 00:01:50,583
That's the logic we wrote before.

40
00:01:51,520 --> 00:01:55,530
Hence here we can now call Todo save.

41
00:01:55,530 --> 00:01:58,490
And, of course, this returns a promise.

42
00:01:58,490 --> 00:02:02,860
So again, we want to await this down here.

43
00:02:02,860 --> 00:02:04,670
And since it could fail of course,

44
00:02:04,670 --> 00:02:07,950
as always, we also want to wrap this with,

45
00:02:07,950 --> 00:02:12,950
try-catch here and catch any errors that we might be getting

46
00:02:13,370 --> 00:02:17,253
so that ultimately here we can return next error.

47
00:02:18,720 --> 00:02:20,870
But if we make it past this try-catch block,

48
00:02:20,870 --> 00:02:22,340
we know that it worked.

49
00:02:22,340 --> 00:02:26,890
And then we can send back a confirmation response where we

50
00:02:26,890 --> 00:02:31,830
just have a message of Todo updated.

51
00:02:31,830 --> 00:02:34,800
And maybe we also include the updated Todo,

52
00:02:34,800 --> 00:02:37,540
which is that Todo I created here.

53
00:02:37,540 --> 00:02:40,530
It's the same Todo because that already has the new text

54
00:02:40,530 --> 00:02:41,863
and the Todo ID.

55
00:02:43,370 --> 00:02:45,790
So that should do the trick for updating.

56
00:02:45,790 --> 00:02:49,330
Now for deleting, it's quite similar.

57
00:02:49,330 --> 00:02:52,173
I will, actually, copy the code from updating.

58
00:02:53,490 --> 00:02:56,230
But here, of course, when we delete the Todo,

59
00:02:56,230 --> 00:03:00,830
we don't get a Todo ID text as part of our body.

60
00:03:00,830 --> 00:03:02,320
So I'll delete that.

61
00:03:02,320 --> 00:03:04,430
And, therefore, I don't have a Todo text

62
00:03:04,430 --> 00:03:05,960
for initializing a Todo.

63
00:03:05,960 --> 00:03:09,460
I'll set it to null therefore, because for deleting

64
00:03:09,460 --> 00:03:11,840
in the Todo model I, in the end, only care

65
00:03:11,840 --> 00:03:15,550
about the ID anyways and I need that ID for deleting.

66
00:03:15,550 --> 00:03:19,230
So just passing in an ID here is enough.

67
00:03:19,230 --> 00:03:22,313
And then here we can call Todo delete,

68
00:03:23,870 --> 00:03:27,420
which of course still returns a promise here.

69
00:03:27,420 --> 00:03:30,453
That's how we built this delete method.

70
00:03:31,310 --> 00:03:34,000
And therefore we, of course, still try-catch this.

71
00:03:34,000 --> 00:03:36,900
But then, in the end, we can say Todo deleted here

72
00:03:39,010 --> 00:03:41,500
and return nothing else.

73
00:03:41,500 --> 00:03:44,620
Now, with that, I'm getting an error down here as I see,

74
00:03:44,620 --> 00:03:45,623
let's see.

75
00:03:46,480 --> 00:03:48,350
Yeah, I'm using a weight in a function

76
00:03:48,350 --> 00:03:50,230
that doesn't have asynch on it.

77
00:03:50,230 --> 00:03:54,017
Of course we should add async here in front of delete Todo,

78
00:03:54,017 --> 00:03:55,693
and now that is fixed.

79
00:03:57,210 --> 00:04:00,080
So with that, we added these two functions

80
00:04:00,080 --> 00:04:02,010
or the code for these functions.

81
00:04:02,010 --> 00:04:05,500
Now in Todo's routes, we need to add the routes.

82
00:04:05,500 --> 00:04:07,440
So we want to have a patch route

83
00:04:07,440 --> 00:04:10,430
for slash colon ID here.

84
00:04:10,430 --> 00:04:13,480
Slash Todos is automatically added in front of that

85
00:04:13,480 --> 00:04:16,480
but now we need that dynamic path parameter.

86
00:04:16,480 --> 00:04:19,040
And then here we point at the Todo's controller

87
00:04:19,040 --> 00:04:20,753
and there add update Todo.

88
00:04:22,060 --> 00:04:24,770
And then below that we have a last route,

89
00:04:24,770 --> 00:04:29,080
which is our delete route for slash colon ID here

90
00:04:29,080 --> 00:04:31,670
with that dynamic parameter and we point

91
00:04:31,670 --> 00:04:33,503
at delete Todo here.

92
00:04:34,720 --> 00:04:36,973
So these are the finished routes now.

93
00:04:38,170 --> 00:04:41,560
And with that, we should be able to also update

94
00:04:41,560 --> 00:04:42,973
and delete Todos.

95
00:04:44,430 --> 00:04:47,280
To test this, I will again use postman.

96
00:04:47,280 --> 00:04:49,380
And I'll, first of all, add a second Todo,

97
00:04:50,270 --> 00:04:52,093
which is test all routes.

98
00:04:53,210 --> 00:04:56,250
So I'm using the post request again in this first tab

99
00:04:56,250 --> 00:04:58,580
with my updated text here in the body,

100
00:04:58,580 --> 00:04:59,663
and I'll click send.

101
00:05:00,580 --> 00:05:01,790
This worked.

102
00:05:01,790 --> 00:05:05,950
If I now get all Todos, I should have two Todos, and I do.

103
00:05:05,950 --> 00:05:08,100
And now I can add a new request,

104
00:05:08,100 --> 00:05:09,800
where I send a patch request

105
00:05:09,800 --> 00:05:13,920
to local host three thousand slash Todos.

106
00:05:13,920 --> 00:05:18,080
And here I want to update a Todo. Therefore, actually in

107
00:05:18,080 --> 00:05:21,510
the path, we need to include the ID of the Todo that should

108
00:05:21,510 --> 00:05:25,410
be updated because that is how we configured our route.

109
00:05:25,410 --> 00:05:28,660
And, therefore, I'll grab one of these IDs.

110
00:05:28,660 --> 00:05:31,260
Let's say the first one, doesn't matter.

111
00:05:31,260 --> 00:05:34,140
By the way I just see I do have an error here, of course.

112
00:05:34,140 --> 00:05:35,720
Didn't notice this before.

113
00:05:35,720 --> 00:05:37,963
Text and ID is swapped here.

114
00:05:38,860 --> 00:05:41,040
Yeah, that's not the idea.

115
00:05:41,040 --> 00:05:42,280
We'll have to fix that

116
00:05:43,670 --> 00:05:45,780
because in Todo model and get all Todos,

117
00:05:45,780 --> 00:05:49,030
I'm of course passing the data into the constructor

118
00:05:49,030 --> 00:05:50,730
in the wrong order.

119
00:05:50,730 --> 00:05:53,780
So, thankfully, it's not stored incorrectly in the database.

120
00:05:53,780 --> 00:05:56,570
I did just map it incorrectly here.

121
00:05:56,570 --> 00:06:00,210
But if we fix this here and I send my get request again,

122
00:06:00,210 --> 00:06:01,543
now this is correct.

123
00:06:02,860 --> 00:06:06,950
So, now again, let's pick that ID here off that first Todo

124
00:06:06,950 --> 00:06:08,800
and then we can add this here in the path

125
00:06:08,800 --> 00:06:10,023
of the patch request.

126
00:06:10,960 --> 00:06:13,950
And then the patch request needs a body because

127
00:06:13,950 --> 00:06:16,780
in the Todo's controller for updating a Todo,

128
00:06:16,780 --> 00:06:20,240
I'm extracting the new text from the body.

129
00:06:20,240 --> 00:06:23,380
So we have to make sure that this field is present

130
00:06:23,380 --> 00:06:24,850
in the request.

131
00:06:24,850 --> 00:06:27,020
And then for here in the body, I'll again

132
00:06:27,020 --> 00:06:32,020
choose raw and Jcon, and add my basic Jcon body here.

133
00:06:33,980 --> 00:06:37,040
Where I do now add this new text field between double

134
00:06:37,040 --> 00:06:40,040
quotes and then I add my new text,

135
00:06:40,040 --> 00:06:42,320
which could just say updated.

136
00:06:42,320 --> 00:06:45,570
If I click send, this seems to work.

137
00:06:45,570 --> 00:06:48,030
I got my updated Todo here.

138
00:06:48,030 --> 00:06:50,590
And if I get all Todos again,

139
00:06:50,590 --> 00:06:52,783
I see it was updated here as well.

140
00:06:54,360 --> 00:06:56,750
Now let's also see if deleting works.

141
00:06:56,750 --> 00:06:59,580
For this one we'll again, pick this ID maybe, again,

142
00:06:59,580 --> 00:07:04,580
off this first Todo and here I will then delete.

143
00:07:04,830 --> 00:07:07,510
Actually, I'll pick the ID off the second Todo,

144
00:07:07,510 --> 00:07:09,030
though it doesn't matter.

145
00:07:09,030 --> 00:07:13,470
But then here, for delete, I send a delete request here

146
00:07:13,470 --> 00:07:18,470
to local host three thousand slash Todos slash this ID.

147
00:07:19,800 --> 00:07:23,970
Nothing else, no bodies required, no special headers.

148
00:07:23,970 --> 00:07:28,320
And hence, if I now click send, I get Todo deleted here.

149
00:07:28,320 --> 00:07:32,543
And if I get all my Todo's again, it indeed is gone.

150
00:07:33,780 --> 00:07:36,560
So now all these current operations work

151
00:07:36,560 --> 00:07:39,060
with help of our web API here.

152
00:07:39,060 --> 00:07:40,620
And because it is an API,

153
00:07:40,620 --> 00:07:42,803
it's all about exchanging data here.

