1
00:00:02,180 --> 00:00:04,080
So, we're almost done.

2
00:00:04,080 --> 00:00:06,950
Let's now make sure we can delete posts.

3
00:00:06,950 --> 00:00:10,410
And that again starts in the post item EJS file,

4
00:00:10,410 --> 00:00:11,763
where I have this button.

5
00:00:12,880 --> 00:00:16,700
Now, there are multiple ways of deleting the post.

6
00:00:16,700 --> 00:00:19,360
Instead of a button, we could use a link,

7
00:00:19,360 --> 00:00:22,160
and then simply add a route for a get request

8
00:00:22,160 --> 00:00:24,750
to the specified path we added to the link,

9
00:00:24,750 --> 00:00:27,223
and then perform our deletion logic.

10
00:00:28,300 --> 00:00:32,450
Alternatively, you could argue that it technically

11
00:00:32,450 --> 00:00:36,880
makes more sense to send a post request to the server

12
00:00:36,880 --> 00:00:38,520
when you want to delete something,

13
00:00:38,520 --> 00:00:41,260
and you can't do this with a link.

14
00:00:41,260 --> 00:00:43,830
That's why I added a button for this.

15
00:00:43,830 --> 00:00:48,700
And then, you can wrap this button into a form, like that,

16
00:00:48,700 --> 00:00:51,650
to make sure that a request is sent

17
00:00:51,650 --> 00:00:53,460
when the button is clicked.

18
00:00:53,460 --> 00:00:56,373
And add an action with the desired path,

19
00:00:56,373 --> 00:00:59,845
and the method of post to this form.

20
00:00:59,845 --> 00:01:02,090
And the desired path could now

21
00:01:02,090 --> 00:01:06,850
be /posts, slash the ID of the post,

22
00:01:06,850 --> 00:01:11,470
which we can inject with help of EJS here, post.id.

23
00:01:11,470 --> 00:01:14,260
As we also did it down there, for example.

24
00:01:14,260 --> 00:01:16,520
/delete.

25
00:01:16,520 --> 00:01:19,860
That could be our path for deleting a post.

26
00:01:19,860 --> 00:01:21,670
And that's one way of ensuring

27
00:01:21,670 --> 00:01:24,230
that a post request is sent to this path

28
00:01:24,230 --> 00:01:26,323
when the delete post button is clicked.

29
00:01:27,210 --> 00:01:29,650
Since this button does not have a type,

30
00:01:29,650 --> 00:01:31,790
it will automatically submit that form,

31
00:01:31,790 --> 00:01:33,440
as you learned before in the course,

32
00:01:33,440 --> 00:01:36,623
and therefore this post request will be sent to this path.

33
00:01:37,890 --> 00:01:39,605
And now in block JS,

34
00:01:39,605 --> 00:01:44,360
we just also have to add a route that handles requests

35
00:01:44,360 --> 00:01:47,713
to this path, post requests to this path.

36
00:01:48,560 --> 00:01:53,390
So here, we can then actually add router.post

37
00:01:54,370 --> 00:01:59,370
and handle the requests to /posts/:id/delete.

38
00:02:02,620 --> 00:02:05,703
And then execute our standard function here.

39
00:02:06,610 --> 00:02:08,580
And in there, in this function,

40
00:02:08,580 --> 00:02:12,053
we now want to delete a post from the database.

41
00:02:13,090 --> 00:02:16,170
We can already convert it into an async function

42
00:02:16,170 --> 00:02:19,350
since we'll again, use async await, because we'll again

43
00:02:19,350 --> 00:02:21,980
send a query to the database.

44
00:02:21,980 --> 00:02:24,090
It will be a pretty short query.

45
00:02:24,090 --> 00:02:26,450
So I'll just write it in line here,

46
00:02:26,450 --> 00:02:28,710
not stored in an extra constant,

47
00:02:28,710 --> 00:02:30,970
which you of course could also do.

48
00:02:30,970 --> 00:02:34,430
And the query we want to send is of course, a query,

49
00:02:34,430 --> 00:02:39,120
a command, we also saw in the last course section already.

50
00:02:39,120 --> 00:02:43,940
We can delete items by using DELETE FROM.

51
00:02:43,940 --> 00:02:48,060
Then the table from where we want to delete something.

52
00:02:48,060 --> 00:02:51,480
And then WHERE, and then id = ?

53
00:02:52,900 --> 00:02:55,140
This is how we can delete all the posts

54
00:02:55,140 --> 00:02:56,500
from the posts table

55
00:02:56,500 --> 00:02:59,090
that match this condition.

56
00:02:59,090 --> 00:02:59,923
And of course,

57
00:02:59,923 --> 00:03:03,270
since we used the ID, and we'll just provide one ID here,

58
00:03:03,270 --> 00:03:05,823
we'll just delete one single post.

59
00:03:06,880 --> 00:03:09,510
So that's then this second parameter value,

60
00:03:09,510 --> 00:03:12,010
which we pass to the query method.

61
00:03:12,010 --> 00:03:15,060
It's this array with all the values that will be used

62
00:03:15,060 --> 00:03:17,940
as replacements for those question marks,

63
00:03:17,940 --> 00:03:21,010
and here that's req.params.id.

64
00:03:21,010 --> 00:03:23,653
That will then delete this post.

65
00:03:24,930 --> 00:03:26,940
Now we cannot wait this operation,

66
00:03:26,940 --> 00:03:29,510
because of course it's an asynchronous one.

67
00:03:29,510 --> 00:03:33,150
And then once this is done, we can, again, redirect,

68
00:03:33,150 --> 00:03:36,693
maybe back to the all posts page like this.

69
00:03:37,820 --> 00:03:39,980
And that is all we have to do.

70
00:03:39,980 --> 00:03:43,593
That's all we have to do in order to delete a post.

71
00:03:45,320 --> 00:03:48,510
Now again, if we save all the files

72
00:03:50,530 --> 00:03:54,650
and I reload this page here, I want to delete a post.

73
00:03:54,650 --> 00:03:57,330
And for this, I'll actually create a new post first.

74
00:03:57,330 --> 00:04:00,440
Test for deletion.

75
00:04:00,440 --> 00:04:03,460
Delete this! Please.

76
00:04:03,460 --> 00:04:05,080
Add to this post.

77
00:04:05,080 --> 00:04:08,270
And now if I click delete post here,

78
00:04:08,270 --> 00:04:11,200
you see, this page is reloaded and the post is gone.

79
00:04:11,200 --> 00:04:13,820
And if I reload it again, it's still gone.

80
00:04:13,820 --> 00:04:15,730
And if I go back, it's still gone.

81
00:04:15,730 --> 00:04:16,733
It's not there.

82
00:04:17,610 --> 00:04:20,570
So deleting, all of it works without errors,

83
00:04:20,570 --> 00:04:22,543
and that's all we have to do here.

