1
00:00:02,070 --> 00:00:03,890
So did you succeed?

2
00:00:03,890 --> 00:00:06,983
Let's implement, updating and deleting together.

3
00:00:08,000 --> 00:00:09,040
Now for that,

4
00:00:09,040 --> 00:00:11,980
I'll start with updating and therefore

5
00:00:11,980 --> 00:00:13,403
this posts edit route.

6
00:00:14,650 --> 00:00:16,280
What we're doing in there in the end,

7
00:00:16,280 --> 00:00:20,260
is we are calling this updateOne method

8
00:00:20,260 --> 00:00:22,450
and we're selecting a post by postId,

9
00:00:22,450 --> 00:00:25,653
and then we're setting the updated title and content.

10
00:00:26,790 --> 00:00:30,140
So I will grab this part here of the code

11
00:00:30,140 --> 00:00:31,400
and go to my model.

12
00:00:31,400 --> 00:00:35,210
And now in there we could add a new method,

13
00:00:35,210 --> 00:00:38,793
an async method called update, for example.

14
00:00:39,970 --> 00:00:44,950
But actually we could also reuse the safe method here,

15
00:00:44,950 --> 00:00:48,010
and in there, we can use the fact

16
00:00:48,010 --> 00:00:51,470
that the ID won't always be set.

17
00:00:51,470 --> 00:00:54,930
And check if this thought ID is truthy,

18
00:00:54,930 --> 00:00:56,580
so it's not undefined.

19
00:00:56,580 --> 00:00:58,870
It is set instead.

20
00:00:58,870 --> 00:01:01,780
Then this means that we created this post

21
00:01:01,780 --> 00:01:04,019
with an existing ID.

22
00:01:04,019 --> 00:01:08,870
And when we call save on a post that already has an ID,

23
00:01:08,870 --> 00:01:11,550
we're definitely not creating a new post

24
00:01:11,550 --> 00:01:15,070
because when we do that, we don't have an ID yet.

25
00:01:15,070 --> 00:01:18,790
We only have IDs on posts that were already stored

26
00:01:18,790 --> 00:01:20,610
in the database before.

27
00:01:20,610 --> 00:01:23,480
So when we call, save on such an existing post,

28
00:01:23,480 --> 00:01:26,430
we must be doing that because we wanna update

29
00:01:26,430 --> 00:01:28,713
that post instead of creating it.

30
00:01:29,680 --> 00:01:33,170
So therefore if we have an ID here in the save method,

31
00:01:33,170 --> 00:01:38,090
I'll execute this update code and only else.

32
00:01:38,090 --> 00:01:42,220
If we don't have an ID, I know that I'm creating a post.

33
00:01:42,220 --> 00:01:44,203
So then I'll do it like that.

34
00:01:45,700 --> 00:01:47,750
Now either way we'll get a result.

35
00:01:47,750 --> 00:01:51,830
So I will actually create result as a standalone variable

36
00:01:51,830 --> 00:01:55,390
and set result equal here in the update case,

37
00:01:55,390 --> 00:01:59,160
and set it equal here, not as a new constant,

38
00:01:59,160 --> 00:02:02,000
but instead as a value for this variable.

39
00:02:02,000 --> 00:02:03,610
So that either way in the end,

40
00:02:03,610 --> 00:02:05,853
we can return the result if we need that.

41
00:02:07,900 --> 00:02:11,880
So that's how we can utilize this ID for updating.

42
00:02:11,880 --> 00:02:16,340
And that also means that here for finding the correct post,

43
00:02:16,340 --> 00:02:18,230
we can use this ID,

44
00:02:18,230 --> 00:02:20,640
assuming that the ID that is stored

45
00:02:20,640 --> 00:02:23,380
in this post object based on this class,

46
00:02:23,380 --> 00:02:24,893
has the right format.

47
00:02:25,830 --> 00:02:27,490
And to ensure this,

48
00:02:27,490 --> 00:02:30,783
we should go back to the post constructor.

49
00:02:31,650 --> 00:02:34,440
Instead of assigning the ID like that here,

50
00:02:34,440 --> 00:02:37,600
we should check if ID is set.

51
00:02:37,600 --> 00:02:38,870
If we're not getting an ID,

52
00:02:38,870 --> 00:02:41,030
I don't wanna do anything.

53
00:02:41,030 --> 00:02:42,750
But if I do get an ID,

54
00:02:42,750 --> 00:02:46,500
then we probably get it as a raw string

55
00:02:46,500 --> 00:02:51,450
and we wanna transform it to this ObjectId thing.

56
00:02:51,450 --> 00:02:52,283
So then here,

57
00:02:52,283 --> 00:02:56,060
I wanna set this ID equal to new ObjectId

58
00:02:57,260 --> 00:03:00,530
just as I'm doing it here in blog.js,

59
00:03:00,530 --> 00:03:04,913
when I transform the post ID I'm getting into an ObjectId.

60
00:03:07,038 --> 00:03:09,660
Therefore, just as in blog.js,

61
00:03:09,660 --> 00:03:13,270
we wanna get the ObjectId like this

62
00:03:13,270 --> 00:03:17,580
by importing mongodb, and using the ObjectId like that.

63
00:03:17,580 --> 00:03:20,820
And therefore in the post.js file, at the very top,

64
00:03:20,820 --> 00:03:25,820
I will import mongodb by requiring mongodb.

65
00:03:25,870 --> 00:03:28,450
And then once we imported all those things,

66
00:03:28,450 --> 00:03:31,950
I will get my ObjectId Constructor,

67
00:03:31,950 --> 00:03:34,990
my ObjectId class by accessing it

68
00:03:34,990 --> 00:03:38,820
on the imported mongodb package like that.

69
00:03:38,820 --> 00:03:42,560
And then here we can indeed create a new ObjectId

70
00:03:42,560 --> 00:03:46,280
and pass the ID, which we got as a parameter value

71
00:03:46,280 --> 00:03:47,763
into this constructor.

72
00:03:49,040 --> 00:03:52,260
And with that, we'll set the ID only if we get one

73
00:03:52,260 --> 00:03:54,490
and we therefore only have one down there

74
00:03:54,490 --> 00:03:58,133
if we did set one when we created the post before.

75
00:04:00,210 --> 00:04:02,820
This also means that the transformation

76
00:04:02,820 --> 00:04:06,780
from string to ObjectId, now happens in the model

77
00:04:06,780 --> 00:04:08,990
and hence back in blog.js,

78
00:04:08,990 --> 00:04:13,990
we can actually go to this update post route.

79
00:04:14,860 --> 00:04:19,800
So to this route and get rid of this line of code here.

80
00:04:19,800 --> 00:04:23,670
And instead after checking that the input is correct

81
00:04:23,670 --> 00:04:25,850
before we redirect,

82
00:04:25,850 --> 00:04:29,760
we now create our post based on that model.

83
00:04:29,760 --> 00:04:34,710
So we create a new post here based on this post class,

84
00:04:34,710 --> 00:04:38,094
but now we create it by passing in the enteredTitle

85
00:04:38,094 --> 00:04:39,860
and the enteredContent.

86
00:04:39,860 --> 00:04:43,500
And we now also do pass in an ID here

87
00:04:43,500 --> 00:04:46,220
because here we wanna create a post object

88
00:04:46,220 --> 00:04:50,023
for a post that already exists in the database.

89
00:04:50,980 --> 00:04:54,510
And the ID here is req.params.id

90
00:04:54,510 --> 00:04:56,880
because we're using the ID, which we extract

91
00:04:56,880 --> 00:04:58,633
from the URL path.

92
00:05:00,090 --> 00:05:04,670
And then again, we can call post.save here

93
00:05:04,670 --> 00:05:07,910
to save that and await this here.

94
00:05:07,910 --> 00:05:11,110
Since save actually is a async function,

95
00:05:11,110 --> 00:05:14,420
it will therefore automatically return a promise.

96
00:05:14,420 --> 00:05:19,420
All functions decorated with async, do return promises.

97
00:05:19,760 --> 00:05:22,330
So therefore we can await this here to wait

98
00:05:22,330 --> 00:05:25,003
until it was saved before we redirect.

99
00:05:26,870 --> 00:05:29,530
And actually that's the wrong place I just noticed.

100
00:05:29,530 --> 00:05:31,670
I, of course, wanna do that down here.

101
00:05:31,670 --> 00:05:35,440
Here, I was in the place where I was checking for errors.

102
00:05:35,440 --> 00:05:37,883
I wanna do that after I checked this.

103
00:05:39,480 --> 00:05:42,890
Now with that, I also implemented the updating logic

104
00:05:42,890 --> 00:05:44,410
with help of a model.

105
00:05:44,410 --> 00:05:47,670
And hence, if we now try that again,

106
00:05:47,670 --> 00:05:50,650
and we go to the admin page and I try updating

107
00:05:50,650 --> 00:05:55,650
this first post, this crashes, and let's see why.

108
00:05:55,680 --> 00:05:59,950
If I expand this, enteredTitle is not the defined,

109
00:05:59,950 --> 00:06:01,950
that's of course, my fault.

110
00:06:01,950 --> 00:06:04,800
I have an error in that save logic.

111
00:06:04,800 --> 00:06:07,630
When I set the data for updating the post,

112
00:06:07,630 --> 00:06:11,010
this should not be enteredTitle and enteredContent.

113
00:06:11,010 --> 00:06:13,790
instead we're inside of the post class.

114
00:06:13,790 --> 00:06:18,790
So this is this.title and this here is this.content,

115
00:06:20,400 --> 00:06:23,960
referring to the title and content that we do set up

116
00:06:23,960 --> 00:06:26,913
when we instantiate this post class.

117
00:06:28,010 --> 00:06:30,510
Now with that, if we saved that,

118
00:06:30,510 --> 00:06:35,510
if I do click save again here, this was updated like this.

119
00:06:37,270 --> 00:06:38,980
So that worked.

120
00:06:38,980 --> 00:06:42,060
Let's now implement the deletion logic as well.

121
00:06:42,060 --> 00:06:46,360
And for this, I will go to the delete route, this one.

122
00:06:46,360 --> 00:06:48,520
And it's now this line of code that we

123
00:06:48,520 --> 00:06:50,383
wanna outsource in the end.

124
00:06:51,530 --> 00:06:55,330
In post.js, I will add a new method

125
00:06:55,330 --> 00:06:57,760
because now it's not the save method,

126
00:06:57,760 --> 00:06:59,630
instead it's a new async method,

127
00:06:59,630 --> 00:07:03,440
which could be called delete because that's what we'll do.

128
00:07:03,440 --> 00:07:06,113
And in there, I wanna execute this logic.

129
00:07:07,450 --> 00:07:11,560
Now for the ID, I will use this.id,

130
00:07:11,560 --> 00:07:15,600
assuming that we are calling delete on a post object

131
00:07:15,600 --> 00:07:19,403
that did get an ID when it was instantiated.

132
00:07:20,510 --> 00:07:24,773
And therefore, we might wanna check if not this thought ID,

133
00:07:25,700 --> 00:07:30,500
if this is not set, in that case we know we have no ID,

134
00:07:30,500 --> 00:07:32,600
so deleting won't work.

135
00:07:32,600 --> 00:07:35,910
And in that case, we could throw a custom error

136
00:07:35,910 --> 00:07:39,680
with the throw keyword, which I also mentioned before,

137
00:07:39,680 --> 00:07:43,500
or we just return and don't do anything.

138
00:07:43,500 --> 00:07:46,060
Either way, we wanna make sure that this line

139
00:07:46,060 --> 00:07:48,880
of code doesn't execute because this relies

140
00:07:48,880 --> 00:07:50,450
on the existence of an ID.

141
00:07:50,450 --> 00:07:53,203
And therefore it won't work if we don't have one.

142
00:07:55,740 --> 00:07:58,520
But with that, we should now be able to call delete

143
00:07:58,520 --> 00:08:00,440
to delete a post.

144
00:08:00,440 --> 00:08:02,610
We also get back a result here,

145
00:08:02,610 --> 00:08:06,610
which we can return in case we should be interested

146
00:08:06,610 --> 00:08:09,980
in that in the place where we called the delete method

147
00:08:11,300 --> 00:08:13,900
and with all of that, back in blog.js,

148
00:08:13,900 --> 00:08:16,660
here in the delete route,

149
00:08:16,660 --> 00:08:20,763
I will just create my post by instantiating it.

150
00:08:21,720 --> 00:08:26,720
And now here, I'm not really interested in the title

151
00:08:27,050 --> 00:08:29,250
and the content.

152
00:08:29,250 --> 00:08:32,693
Instead here, I'm really just interested in the ID.

153
00:08:33,900 --> 00:08:37,730
What we can do in this case is we can pass in empty strings

154
00:08:37,730 --> 00:08:42,730
for title and content and just pass in req.params.id.

155
00:08:45,320 --> 00:08:50,320
Alternatively, we pass null as values for title and content

156
00:08:51,940 --> 00:08:55,620
to make it clear that here I don't have any title

157
00:08:55,620 --> 00:08:58,950
or content because I'm not creating this instance

158
00:08:58,950 --> 00:09:03,140
of the post class to work on that existing post.

159
00:09:03,140 --> 00:09:07,123
Instead, I'm creating it like this, just to delete it.

160
00:09:08,120 --> 00:09:12,510
So here we can then call post.delete and await that.

161
00:09:12,510 --> 00:09:13,963
And that should do the trick.

162
00:09:15,730 --> 00:09:18,890
If we now save all of that,

163
00:09:18,890 --> 00:09:21,160
and I reload here and I delete,

164
00:09:21,160 --> 00:09:25,520
let's say the last post here, that seems to work.

165
00:09:25,520 --> 00:09:27,690
If I reload, it's still gone.

166
00:09:27,690 --> 00:09:32,580
I also have no error here. So that did the trick.

167
00:09:32,580 --> 00:09:35,803
And that is how we can delete a post as well.

