1
00:00:02,469 --> 00:00:03,380
Now let's make sure

2
00:00:03,380 --> 00:00:05,420
we can also submit this post,

3
00:00:05,420 --> 00:00:09,830
and then actually store the updated data in the database.

4
00:00:09,830 --> 00:00:12,710
And, for this, back in the update-post template,

5
00:00:12,710 --> 00:00:15,050
we need to work on that form here,

6
00:00:15,050 --> 00:00:17,360
on the action and the method.

7
00:00:17,360 --> 00:00:19,890
Now the method should be POST because we're sending

8
00:00:19,890 --> 00:00:23,670
user generated data that should be stored in the database,

9
00:00:23,670 --> 00:00:25,890
and whenever you're storing something,

10
00:00:25,890 --> 00:00:27,740
somewhere on the server side,

11
00:00:27,740 --> 00:00:30,113
you typically wanna use a post request here.

12
00:00:31,480 --> 00:00:35,470
But what should be our action, our URL?

13
00:00:35,470 --> 00:00:38,560
Well, here, we have different options,

14
00:00:38,560 --> 00:00:41,670
but I would say one approach that makes sense

15
00:00:41,670 --> 00:00:46,670
is to send the request to posts/, then the post id

16
00:00:47,800 --> 00:00:49,580
that's being edited,

17
00:00:49,580 --> 00:00:54,373
so post.id injected with ejs/edit again.

18
00:00:55,602 --> 00:00:58,970
So the same path that we had for loading this page,

19
00:00:58,970 --> 00:01:02,380
but now reached with help of a post request,

20
00:01:02,380 --> 00:01:04,950
which will, therefore, trigger a different route

21
00:01:04,950 --> 00:01:08,610
because post and get requests don't trigger the same routes

22
00:01:08,610 --> 00:01:11,103
because they are different types of requests.

23
00:01:12,320 --> 00:01:14,600
With that, back in blog.js,

24
00:01:14,600 --> 00:01:17,680
we just have to add a fitting route for this.

25
00:01:17,680 --> 00:01:20,090
Now it's a post request, which you wanna handle,

26
00:01:20,090 --> 00:01:22,130
so we use the post method, here,

27
00:01:22,130 --> 00:01:27,130
and then it's /posts/placeholder/edit.

28
00:01:29,530 --> 00:01:31,890
So the same path as we added here,

29
00:01:31,890 --> 00:01:34,140
but here we had it for get requests,

30
00:01:34,140 --> 00:01:37,300
now we have it for post requests.

31
00:01:37,300 --> 00:01:40,213
Then, of course, we have our function as always.

32
00:01:41,450 --> 00:01:44,080
And, in there, we now wanna really update

33
00:01:44,080 --> 00:01:46,870
the data in the database.

34
00:01:46,870 --> 00:01:49,970
For this, again, I'll write my query

35
00:01:52,480 --> 00:01:56,210
for readability reasons in a separate constant.

36
00:01:56,210 --> 00:01:59,470
And you might remember how updating works

37
00:01:59,470 --> 00:02:01,430
from the last core section.

38
00:02:01,430 --> 00:02:05,013
The SQL statement we need, here, is UPDATE,

39
00:02:05,930 --> 00:02:09,900
then the name of the table, where we wanna change things

40
00:02:09,900 --> 00:02:14,300
like posts, and then the SET keyword,

41
00:02:14,300 --> 00:02:17,890
and then we simply tell SQL which fields should be set

42
00:02:17,890 --> 00:02:20,340
to a new value, and all the fields,

43
00:02:20,340 --> 00:02:22,040
where we don't say anything about,

44
00:02:22,040 --> 00:02:24,363
will keep their existing values.

45
00:02:25,400 --> 00:02:27,890
So, here, we can then set the title, for example,

46
00:02:27,890 --> 00:02:30,970
to a new value, and I'll use a question mark

47
00:02:30,970 --> 00:02:33,600
as a placeholder, which will be replaced

48
00:02:33,600 --> 00:02:35,443
with a concrete value later.

49
00:02:36,850 --> 00:02:38,490
Then, separated by a comma,

50
00:02:38,490 --> 00:02:41,410
I'll set the summary to a new value,

51
00:02:41,410 --> 00:02:43,270
and, again, separated by a comma,

52
00:02:43,270 --> 00:02:46,060
I'll set the body to a new value.

53
00:02:46,060 --> 00:02:48,840
These are the three fields we wanna overwrite

54
00:02:48,840 --> 00:02:50,890
because these are the three pieces of data

55
00:02:50,890 --> 00:02:54,783
which we fetch with the form in update-post.ejs.

56
00:02:57,160 --> 00:03:00,370
So that's the first step, but not everything.

57
00:03:00,370 --> 00:03:03,700
We also need to add a WHERE clause to make it clear

58
00:03:03,700 --> 00:03:07,440
which posts or post should be edited.

59
00:03:07,440 --> 00:03:09,200
And, here, it should be a single post,

60
00:03:09,200 --> 00:03:11,970
which you wanna identify with help of the id,

61
00:03:11,970 --> 00:03:13,930
and, again, I'll use a placeholder, here,

62
00:03:13,930 --> 00:03:16,813
to inject the concrete value, later,

63
00:03:18,130 --> 00:03:19,930
and later is now.

64
00:03:19,930 --> 00:03:24,730
We, again, use db.query and pass our query to it,

65
00:03:24,730 --> 00:03:26,570
and then the second parameter value,

66
00:03:26,570 --> 00:03:30,100
which holds all the concrete values that will be used

67
00:03:30,100 --> 00:03:33,143
as replacements for those question marks.

68
00:03:34,770 --> 00:03:37,280
And, now, here we need more than one value.

69
00:03:37,280 --> 00:03:39,840
Before we always dealt with one value,

70
00:03:39,840 --> 00:03:41,640
like the request.params.id,

71
00:03:41,640 --> 00:03:44,240
now we have four question marks,

72
00:03:44,240 --> 00:03:47,230
and, hence, we need four values here,

73
00:03:47,230 --> 00:03:49,720
and the order needs to be the same.

74
00:03:49,720 --> 00:03:52,900
If you have title, here, as a first question mark,

75
00:03:52,900 --> 00:03:54,660
the title that you wanna inject

76
00:03:54,660 --> 00:03:57,173
has to be the first value in this array.

77
00:03:58,440 --> 00:04:00,730
Now, of course, the data is all coming from

78
00:04:00,730 --> 00:04:04,580
the request body here because we have a form submission,

79
00:04:04,580 --> 00:04:07,823
and, hence, the title is request.body.title.

80
00:04:09,577 --> 00:04:12,770
.title because in the update-post form

81
00:04:12,770 --> 00:04:16,050
this input, here, has a name of title,

82
00:04:16,050 --> 00:04:18,730
and it's these names which are available

83
00:04:18,730 --> 00:04:21,353
as properties on req body.

84
00:04:23,850 --> 00:04:26,230
So, here, we have request.body.title,

85
00:04:26,230 --> 00:04:30,880
then request.body.summary,

86
00:04:30,880 --> 00:04:35,880
and then request.body.content.

87
00:04:36,790 --> 00:04:40,700
Content because, again, that's the name value

88
00:04:40,700 --> 00:04:42,913
I have here on this text area.

89
00:04:45,310 --> 00:04:47,790
Now the fourth value, which we have to pass in,

90
00:04:47,790 --> 00:04:51,150
is the value for this question mark in the WHERE clause,

91
00:04:51,150 --> 00:04:54,510
and that's actually the id, which is part of the path,

92
00:04:54,510 --> 00:04:58,423
so that's actually retrieved with req.params.id.

93
00:04:59,510 --> 00:05:03,220
So what we also did before, for example, here,

94
00:05:03,220 --> 00:05:06,123
when we fetched the data for the edit page.

95
00:05:08,170 --> 00:05:11,240
Now I'll reformat this to make it a bit more readable,

96
00:05:11,240 --> 00:05:13,863
and that's now how I wanna execute my query.

97
00:05:15,360 --> 00:05:19,570
Again, let's add async await to await this query,

98
00:05:19,570 --> 00:05:22,190
and then, once it's done, I will, again,

99
00:05:22,190 --> 00:05:26,890
call redirect to redirect back to /posts,

100
00:05:26,890 --> 00:05:28,970
so to the all posts page.

101
00:05:28,970 --> 00:05:30,300
That is what should happen

102
00:05:30,300 --> 00:05:33,433
after saving the updated post data finished.

103
00:05:35,320 --> 00:05:39,200
And, hence, now with that, if we save all of that

104
00:05:39,200 --> 00:05:41,640
this should be working.

105
00:05:41,640 --> 00:05:45,030
If we go back and reload this Update post page,

106
00:05:45,030 --> 00:05:49,810
and I now add a new text, here, to this post content,

107
00:05:49,810 --> 00:05:54,810
like, It was updated!, and maybe also add a couple

108
00:05:54,830 --> 00:05:58,040
of extra exclamation marks, here, to the title.

109
00:05:58,040 --> 00:05:59,813
If I click Update post,

110
00:06:00,710 --> 00:06:03,650
we see those exclamation marks here, so that worked.

111
00:06:03,650 --> 00:06:05,270
And if I click View post,

112
00:06:05,270 --> 00:06:07,673
I also see my extra paragraph here.

113
00:06:09,090 --> 00:06:11,320
So updating also works,

114
00:06:11,320 --> 00:06:13,650
we also got no error, here, in the terminal,

115
00:06:13,650 --> 00:06:15,830
which is a good sign, and, therefore,

116
00:06:15,830 --> 00:06:19,900
the only remaining step now is to make posts deletable.

117
00:06:19,900 --> 00:06:22,650
Again, try this on your own first if you want to,

118
00:06:22,650 --> 00:06:25,713
we'll implement this feature together in the next lecture.

