1
00:00:04,880 --> 00:00:09,680
In this video, let's implement a route to update a post.

2
00:00:10,160 --> 00:00:15,890
The step we are going to do to update a post, we're going to be a little bit difficult than the one

3
00:00:15,890 --> 00:00:17,420
that we have done so far.

4
00:00:17,570 --> 00:00:21,020
But I'm going to move step by step and explain everything I write.

5
00:00:21,020 --> 00:00:27,920
So let's go so back to the route and this is a route that we are going to implement the update.

6
00:00:27,920 --> 00:00:35,990
So let me go ahead and test inside Postman Update and I made a request and I see I update post route

7
00:00:35,990 --> 00:00:37,610
meaning it's working.

8
00:00:37,610 --> 00:00:41,270
So let's go ahead and implement the logic.

9
00:00:41,540 --> 00:00:47,630
So what we are going to do is that again, if we are dealing database, the updating is going to be

10
00:00:47,630 --> 00:00:48,650
pretty easy.

11
00:00:48,650 --> 00:00:55,250
But writing onto a file, the implementation going to be difficult than doing with a normal database.

12
00:00:55,250 --> 00:01:00,110
But I'm going to help you to reinforce your knowledge when it comes to JavaScript.

13
00:01:00,110 --> 00:01:02,960
So now let's remove this one from here.

14
00:01:02,990 --> 00:01:07,550
The first step is we need the ID and indeed these are ID.

15
00:01:07,820 --> 00:01:10,400
I know now we know how to get the ID.

16
00:01:10,430 --> 00:01:17,390
The next step is you want to get the properties that we want to update and we can get that one from

17
00:01:17,390 --> 00:01:24,980
registered so counts here and we can structure that from red dot body.

18
00:01:25,370 --> 00:01:25,820
All right.

19
00:01:25,820 --> 00:01:28,850
And we want the new URL.

20
00:01:28,880 --> 00:01:35,510
We want the title here and we want the description about the posts as that.

21
00:01:35,870 --> 00:01:41,670
So if you are not familiar with JavaScript about this syntax, we treated this one in details and the

22
00:01:41,690 --> 00:01:42,860
JavaScript section.

23
00:01:42,860 --> 00:01:44,840
But let me explain what is going on here.

24
00:01:45,200 --> 00:01:51,470
If you console.log read Dot Body, I'm going to have all the properties that you pass in onto that.

25
00:01:51,470 --> 00:01:54,770
So let's go ahead and then console.log the red dot body.

26
00:01:57,150 --> 00:02:01,410
So let me get back to the postman and now for the updates.

27
00:02:01,470 --> 00:02:08,759
I'm going to provide the values you want to update, so click on body and then click on batting and

28
00:02:08,759 --> 00:02:12,420
then Non and click on raw and then JSON data.

29
00:02:12,420 --> 00:02:17,830
So we're going to pass in the JSON data, meaning the properties that we want to update.

30
00:02:17,850 --> 00:02:24,540
So first is we want a title and then title here, let me say updated title.

31
00:02:25,700 --> 00:02:26,150
All right.

32
00:02:26,150 --> 00:02:28,400
Just to show that it has been updated.

33
00:02:29,880 --> 00:02:35,550
And then the description and let me say this, I'm going to be description.

34
00:02:36,510 --> 00:02:42,480
And then the you are all going to be the you are is going to be.

35
00:02:43,530 --> 00:02:48,470
ATP Cologne image dot com.

36
00:02:48,480 --> 00:02:49,260
Anything.

37
00:02:49,830 --> 00:02:51,960
All right, so this is what I want.

38
00:02:51,990 --> 00:02:57,870
When I hit send, let me check the terminal, and you can see that I have the object.

39
00:02:58,260 --> 00:03:01,520
If I want a title, I'm going to be Red Dead Body.

40
00:03:01,530 --> 00:03:07,050
The title Red Dead Body the description and record body the URL.

41
00:03:07,140 --> 00:03:10,320
Now I get the properties that I want updates.

42
00:03:10,350 --> 00:03:14,340
The next step is let's find the post that you want to update.

43
00:03:14,340 --> 00:03:16,710
So here we go find.

44
00:03:18,670 --> 00:03:21,700
The Post to update.

45
00:03:22,000 --> 00:03:25,840
And here we are going to use a JavaScript method called Fine.

46
00:03:25,840 --> 00:03:26,680
So here we go.

47
00:03:26,680 --> 00:03:33,700
Const found post is equal to the post data dot fine.

48
00:03:33,880 --> 00:03:39,790
And the time you pass in a callback function and then this callback function you have access to the

49
00:03:39,790 --> 00:03:41,860
post and now here we go.

50
00:03:41,860 --> 00:03:50,260
So you're are going to return the post ID post ID if it is equal to the ID in the params.

51
00:03:52,510 --> 00:03:56,260
Post found all the found posts and let's see.

52
00:03:56,350 --> 00:03:58,690
So now back to post, man.

53
00:03:58,720 --> 00:04:03,190
Let's make the requests and let's hit send back to my terminal.

54
00:04:03,220 --> 00:04:11,770
I see undefined meaning that the ID, which is 23, is not part of the post record or data here.

55
00:04:11,800 --> 00:04:14,320
The ID here I have one and two.

56
00:04:14,350 --> 00:04:16,240
So let me provide that.

57
00:04:16,240 --> 00:04:18,540
Here is going to be let me see two.

58
00:04:18,579 --> 00:04:23,770
Here it's saying check the terminal and I have the post here.

59
00:04:24,160 --> 00:04:27,250
So let's also check if the post is not found.

60
00:04:27,250 --> 00:04:32,950
Because if I make use of any ID here and I st I get undefined.

61
00:04:32,950 --> 00:04:35,860
But let's give a beautiful message to the user.

62
00:04:36,130 --> 00:04:39,340
So now back to the root here.

63
00:04:39,370 --> 00:04:42,040
Let's also check if there is no post.

64
00:04:42,040 --> 00:04:51,130
So down here going to be if there is no found post, then we can go ahead and then return from this.

65
00:04:51,130 --> 00:05:02,560
So return and then rest dot json and then you will see that message is equal to post not.

66
00:05:03,410 --> 00:05:04,220
Found.

67
00:05:04,250 --> 00:05:06,230
Let's save it and let's check again.

68
00:05:06,260 --> 00:05:10,820
Let me hit send and I see a message that post not found.

69
00:05:10,850 --> 00:05:16,100
If I make use of one or two when I hit st, let's check the console.

70
00:05:16,130 --> 00:05:20,070
I don't see anything here is because I'm not console.

71
00:05:20,110 --> 00:05:21,110
Again the post.

72
00:05:21,590 --> 00:05:23,780
So how can we update it?

73
00:05:23,990 --> 00:05:25,700
Let's look at the flow here.

74
00:05:25,700 --> 00:05:26,570
The steps.

75
00:05:26,810 --> 00:05:34,490
Step number one is I'm going to filter out all the posts, which is not equal to this.

76
00:05:35,030 --> 00:05:38,450
So I'm going to get a new array of posts.

77
00:05:38,810 --> 00:05:46,640
And next is I'm going to update the found post and then push it back into the newly filtered array of

78
00:05:46,640 --> 00:05:47,210
posts.

79
00:05:47,690 --> 00:05:50,260
And with that, I need to run to the file.

80
00:05:50,270 --> 00:05:51,860
So this added steps.

81
00:05:52,190 --> 00:05:56,630
So down here we can go ahead and filter those posts.

82
00:05:56,630 --> 00:05:58,020
So here we go.

83
00:05:58,040 --> 00:06:00,920
So let me comment here and say filter.

84
00:06:02,790 --> 00:06:03,960
So here we go.

85
00:06:04,020 --> 00:06:06,570
Concert theater.

86
00:06:07,610 --> 00:06:13,520
Post is equal to the post data dot filter.

87
00:06:13,790 --> 00:06:15,050
As we did before.

88
00:06:16,320 --> 00:06:19,260
If they were to then pass in a callback function.

89
00:06:19,260 --> 00:06:25,470
And for this one I'm going to use a function to make it pretty easy so I have access to the posts and

90
00:06:25,470 --> 00:06:28,980
then I role function and then the post.

91
00:06:29,940 --> 00:06:30,390
Dart.

92
00:06:30,390 --> 00:06:31,290
I'd.

93
00:06:33,950 --> 00:06:37,970
Which is equal to the ID of the post that I want to find.

94
00:06:38,450 --> 00:06:44,630
Next is if I try to console log filtered post here, you are going to see that.

95
00:06:45,610 --> 00:06:50,260
The post being found will not be part of the filtered one.

96
00:06:50,590 --> 00:06:52,180
Now back to post mine.

97
00:06:52,180 --> 00:06:57,580
When I hit st in the back to my terminal I see that I don't see the post with.

98
00:06:57,580 --> 00:06:58,540
I do too.

99
00:06:59,020 --> 00:07:00,290
So here we go.

100
00:07:00,310 --> 00:07:05,710
After that, I'm going to take the front post to update it.

101
00:07:05,710 --> 00:07:06,460
So here we go.

102
00:07:06,460 --> 00:07:11,190
So update the found post.

103
00:07:11,380 --> 00:07:12,460
So here we go.

104
00:07:12,490 --> 00:07:19,600
The found post dot title is now equal to the title from the request body.

105
00:07:20,470 --> 00:07:26,860
Then likewise the description is going to be from description from here.

106
00:07:26,890 --> 00:07:29,230
The one that we have structured up here.

107
00:07:29,650 --> 00:07:39,700
And then after that, I also want to update the URL, which is post dot URL, which is equal to you

108
00:07:39,700 --> 00:07:41,160
are from the body.

109
00:07:41,170 --> 00:07:42,760
And now here we go.

110
00:07:43,820 --> 00:07:50,930
The next step is I need to push this newly updated post into this.

111
00:07:51,410 --> 00:07:53,360
So here we go.

112
00:07:53,810 --> 00:08:02,180
We're going to be push the updated post into the filtered.

113
00:08:03,330 --> 00:08:05,670
Posts are re.

114
00:08:06,660 --> 00:08:07,920
So here you go.

115
00:08:08,310 --> 00:08:12,780
Now let's take the future post docs.

116
00:08:13,320 --> 00:08:20,430
Let me use Onshape throughout to be at the beginning of the array and I'll pass in the found posts.

117
00:08:20,940 --> 00:08:29,070
Now if I try to console.log the future post, I'll be able to see the newly updated repos in the array.

118
00:08:29,340 --> 00:08:32,340
So let's go ahead and do that before we continue.

119
00:08:32,370 --> 00:08:34,270
Now we will take post here.

120
00:08:37,090 --> 00:08:37,870
All right.

121
00:08:37,870 --> 00:08:39,909
So back to post man.

122
00:08:39,909 --> 00:08:42,730
And next is let me check the payload.

123
00:08:42,730 --> 00:08:44,380
Click here or expand it.

124
00:08:44,380 --> 00:08:49,030
And now I see updated title and the updated description.

125
00:08:49,060 --> 00:08:56,260
Let me hit send and check the terminal and if will see that I have it been added into the array.

126
00:08:56,290 --> 00:09:03,400
I have the title here updated in the description, but if I check the file, I don't see that because

127
00:09:03,400 --> 00:09:05,680
I'm not writing onto the file.

128
00:09:05,740 --> 00:09:07,150
So here we go.

129
00:09:07,150 --> 00:09:09,040
So let me remove the console.

130
00:09:09,040 --> 00:09:10,750
Look here and next page.

131
00:09:10,750 --> 00:09:12,250
Right to the file.

132
00:09:12,940 --> 00:09:14,050
Right to the file.

133
00:09:14,050 --> 00:09:19,870
So the same logic here, we can create a function to get this one line that we are just repeating ourself.

134
00:09:20,080 --> 00:09:26,500
We need the same function here, the same thing we did for the creating and deleting so we can create

135
00:09:26,500 --> 00:09:28,240
a function to get this one done.

136
00:09:28,240 --> 00:09:30,580
But it's working or better.

137
00:09:30,580 --> 00:09:32,230
So we have the function here.

138
00:09:32,230 --> 00:09:32,650
Here.

139
00:09:32,650 --> 00:09:34,060
Why are we using it?

140
00:09:34,090 --> 00:09:36,730
This function, we have it here.

141
00:09:36,730 --> 00:09:39,760
But let's make it pretty simple by copy and paste, guys.

142
00:09:39,760 --> 00:09:41,420
All right, let's play it simple.

143
00:09:41,420 --> 00:09:41,690
Right?

144
00:09:41,710 --> 00:09:47,950
So let me copy this function here and now inside the update where the update handler.

145
00:09:48,930 --> 00:09:51,990
And here is my update and let me pass it here.

146
00:09:52,200 --> 00:09:53,460
So here is one.

147
00:09:53,470 --> 00:09:54,660
Ah, the error.

148
00:09:55,570 --> 00:10:00,450
Okay, so now instead of post here, I'm going to pass in the filtered posts.

149
00:10:00,570 --> 00:10:01,800
So here we go.

150
00:10:01,830 --> 00:10:06,360
And next is post updated successfully.

151
00:10:06,360 --> 00:10:13,320
So now back to postman and let me hit send and I see post updated successfully.

152
00:10:13,350 --> 00:10:19,150
Let me check the file and I have it been updated.

153
00:10:19,170 --> 00:10:24,450
Guys, let me create more post double clicking, clicking, clicking, clicking, clicking.

154
00:10:25,110 --> 00:10:26,370
And let me check.

155
00:10:26,730 --> 00:10:31,620
And you see that I have a bunch of post here and I can delete any of this one.

156
00:10:31,620 --> 00:10:37,560
So let me delete post number four and let me hit send post delete deleted.

157
00:10:37,560 --> 00:10:41,660
I can delete number two and I can delete number one.

158
00:10:41,670 --> 00:10:43,610
I can delete number six.

159
00:10:43,620 --> 00:10:47,640
And now let me check what has slipped and now I have to post left.

