1
00:00:02,440 --> 00:00:05,570
So, let's make sure posts are deletable.

2
00:00:05,570 --> 00:00:09,090
For this we got the delete button here

3
00:00:09,090 --> 00:00:11,370
in the post item EJS file.

4
00:00:11,370 --> 00:00:13,330
And it's a button, not a link.

5
00:00:13,330 --> 00:00:15,400
We could convert it into a link

6
00:00:15,400 --> 00:00:17,120
and point at some path

7
00:00:17,120 --> 00:00:19,230
where we then handle GIT requests

8
00:00:19,230 --> 00:00:21,900
to that path to delete that post.

9
00:00:21,900 --> 00:00:25,100
That would work and you could implement it like this.

10
00:00:25,100 --> 00:00:29,710
But deleting data actually impacts the database, of course.

11
00:00:29,710 --> 00:00:31,600
It's a little bit like storing data,

12
00:00:31,600 --> 00:00:32,850
of course the opposite

13
00:00:32,850 --> 00:00:35,810
but we change something in the database.

14
00:00:35,810 --> 00:00:36,880
Hence in my opinion,

15
00:00:36,880 --> 00:00:41,280
a post request is a little bit better to use here.

16
00:00:41,280 --> 00:00:43,330
Again, you could delete the post

17
00:00:43,330 --> 00:00:45,920
both with a GIT and a post request.

18
00:00:45,920 --> 00:00:48,870
This does not impact what you can do on the server.

19
00:00:48,870 --> 00:00:52,470
It's just about which message you wanna send.

20
00:00:52,470 --> 00:00:56,057
Post typically implies that things change on the server.

21
00:00:56,057 --> 00:01:00,010
GIT implies that you just, well get data.

22
00:01:00,010 --> 00:01:03,200
That's why I would like to send a post request here,

23
00:01:03,200 --> 00:01:04,709
but how can we do that?

24
00:01:04,709 --> 00:01:07,163
We can't add a link to the button like this.

25
00:01:08,230 --> 00:01:09,790
Well, we can wrap the button

26
00:01:09,790 --> 00:01:11,070
in a form as we did it

27
00:01:11,070 --> 00:01:13,440
before in the course as well.

28
00:01:13,440 --> 00:01:15,660
Of course here, the users shouldn't enter

29
00:01:15,660 --> 00:01:17,040
anything into fields

30
00:01:17,040 --> 00:01:19,090
but by wrapping the button of form,

31
00:01:19,090 --> 00:01:21,610
now we can attach a link to the button

32
00:01:21,610 --> 00:01:24,050
by adding an action to the form

33
00:01:24,050 --> 00:01:26,550
because the button will submit that form.

34
00:01:26,550 --> 00:01:29,290
And here we could point at posts.

35
00:01:29,290 --> 00:01:30,830
Then the idea of that post,

36
00:01:30,830 --> 00:01:33,033
which is post._id,

37
00:01:33,033 --> 00:01:35,450
as used down there,

38
00:01:35,450 --> 00:01:37,873
/delete for example.

39
00:01:39,340 --> 00:01:41,760
Now this kind of request would be sent.

40
00:01:41,760 --> 00:01:43,600
We can set method to post

41
00:01:43,600 --> 00:01:46,583
to ensure that a post request will be sent.

42
00:01:49,890 --> 00:01:51,550
And that is how we could ensure

43
00:01:51,550 --> 00:01:53,380
that whenever the button is clicked here,

44
00:01:53,380 --> 00:01:56,450
we do a send such a post request to this path.

45
00:01:56,450 --> 00:01:58,900
And hence, now we can register a post route

46
00:01:58,900 --> 00:02:01,740
for this is path in our blog JS file

47
00:02:01,740 --> 00:02:05,079
to then implement the deletion logic there.

48
00:02:05,079 --> 00:02:06,800
And that's what I'll do.

49
00:02:06,800 --> 00:02:10,310
In blog JS, at the bottom let's say,

50
00:02:10,310 --> 00:02:15,310
we can register a new route for /posts,

51
00:02:15,397 --> 00:02:16,810
/:id,

52
00:02:16,810 --> 00:02:18,400
/delete

53
00:02:19,352 --> 00:02:21,960
and then execute our deletion logic

54
00:02:21,960 --> 00:02:24,163
in this route handling function here.

55
00:02:26,090 --> 00:02:27,920
Of course, since we'll talk to a database,

56
00:02:27,920 --> 00:02:29,610
we can already add async,

57
00:02:29,610 --> 00:02:33,000
since we'll perform some async operations.

58
00:02:33,000 --> 00:02:35,240
And then as a first step, we can again,

59
00:02:35,240 --> 00:02:37,030
extract our post ID,

60
00:02:37,030 --> 00:02:39,660
since we'll need that for identifying the post

61
00:02:39,660 --> 00:02:41,720
that should be deleted.

62
00:02:41,720 --> 00:02:45,300
So we can get our post ID like this here

63
00:02:47,110 --> 00:02:50,063
and now we can get hold of our database here.

64
00:02:51,600 --> 00:02:55,480
Then they're reach out to the posts collection

65
00:02:55,480 --> 00:02:58,240
and then run the delete one method

66
00:02:58,240 --> 00:03:00,723
on that collection to delete one post.

67
00:03:01,600 --> 00:03:04,740
And just like update one or find one we can

68
00:03:04,740 --> 00:03:07,610
and we should now pass in a parameter value

69
00:03:07,610 --> 00:03:10,580
that defines how the posts that should be deleted,

70
00:03:10,580 --> 00:03:12,540
should be identified.

71
00:03:12,540 --> 00:03:14,220
And here I wanna look for the post

72
00:03:14,220 --> 00:03:17,953
where _id is equal to the postId I created here.

73
00:03:19,070 --> 00:03:20,080
And that should be all.

74
00:03:20,080 --> 00:03:22,653
That should delete the post with that ID.

75
00:03:23,530 --> 00:03:25,810
So we can await this.

76
00:03:25,810 --> 00:03:28,060
We also would be getting back a result here

77
00:03:28,060 --> 00:03:31,120
with more information about the deletion process.

78
00:03:31,120 --> 00:03:32,710
If we're interested in that.

79
00:03:32,710 --> 00:03:35,250
You can log into the console if you wanna look into it.

80
00:03:35,250 --> 00:03:36,850
Here, I'll ignore it.

81
00:03:36,850 --> 00:03:39,410
And instead I'll just redirect the user

82
00:03:39,410 --> 00:03:41,500
back to /posts here,

83
00:03:41,500 --> 00:03:44,303
once the deletion was executed.

84
00:03:45,830 --> 00:03:47,750
That should be all.

85
00:03:47,750 --> 00:03:51,010
If I now save that and I reload here

86
00:03:51,010 --> 00:03:52,980
and I click delete post,

87
00:03:52,980 --> 00:03:56,620
it's deleted and hence I see the no posts message.

88
00:03:56,620 --> 00:04:00,180
And of course, now we can, again, add more posts,

89
00:04:00,180 --> 00:04:01,920
a new post,

90
00:04:01,920 --> 00:04:05,403
this works for real,

91
00:04:06,790 --> 00:04:09,520
and maybe attach that to manual

92
00:04:09,520 --> 00:04:10,640
and we see it here.

93
00:04:10,640 --> 00:04:12,204
But deleting, as you saw,

94
00:04:12,204 --> 00:04:13,793
also works.

