1
00:00:00,810 --> 00:00:04,890
In this video, let's implement delete functionality.

2
00:00:05,130 --> 00:00:07,980
Let's change the comments to delete posts.

3
00:00:08,250 --> 00:00:11,250
Let's try the endpoint and make sure that it's working.

4
00:00:11,250 --> 00:00:12,570
And this is the delete.

5
00:00:12,600 --> 00:00:15,690
Let me hit send and now I have the root.

6
00:00:15,690 --> 00:00:17,490
Been working perfectly.

7
00:00:17,520 --> 00:00:19,470
So what are the steps?

8
00:00:19,800 --> 00:00:25,500
Well, the steps are going to be pretty simple as the way we create a post.

9
00:00:25,530 --> 00:00:31,550
So first is we need to find the ID of the post that we want to delete.

10
00:00:31,560 --> 00:00:33,480
So here we go.

11
00:00:33,510 --> 00:00:39,660
So first is find the OR, get the ID, get the ID.

12
00:00:39,660 --> 00:00:47,040
And as we did before, the ID can be found under rec dot params dot ID.

13
00:00:47,610 --> 00:00:52,740
Then after that we are going to find the post by ID inside the post data.

14
00:00:52,800 --> 00:01:01,080
So now inside the root here, let's filter out all the posts with a specific id so accounts.

15
00:01:01,080 --> 00:01:04,050
And now call this one as filtered post.

16
00:01:05,720 --> 00:01:08,600
And is equal to the posts array.

17
00:01:08,600 --> 00:01:15,440
On that we have a method called floater and for this function we pass in a callback function.

18
00:01:15,440 --> 00:01:17,300
Let me use normal function here.

19
00:01:17,570 --> 00:01:22,400
So inside a function body we can go ahead and implement the logic.

20
00:01:22,400 --> 00:01:29,510
So here we can retain a post dot id maybe guessing where is the ID?

21
00:01:29,540 --> 00:01:36,680
If you can still log the posts inside the callback function, we're going to have the individual post,

22
00:01:36,680 --> 00:01:41,060
so let's check it out and see back to Postman Hit St.

23
00:01:41,060 --> 00:01:45,980
And now we see that these are the individual posts and we have an ID.

24
00:01:46,280 --> 00:01:52,700
So back to the root here for the returning if the ID is.

25
00:01:53,870 --> 00:01:58,220
Equal to the ID in the params, then it means that it's been found.

26
00:01:58,220 --> 00:02:07,820
So let's go ahead and then console that log the filtered posts favorited posts as that.

27
00:02:07,820 --> 00:02:09,740
This one down here.

28
00:02:10,100 --> 00:02:13,260
Oh, it must be in the root server.

29
00:02:13,790 --> 00:02:15,140
I always make mistake.

30
00:02:15,200 --> 00:02:20,570
So now let's go ahead and make the request and see hit st.

31
00:02:20,570 --> 00:02:25,820
And now we see empty array because the ID is not found.

32
00:02:25,820 --> 00:02:28,520
So let's use ID that exists.

33
00:02:28,520 --> 00:02:29,720
Let's see one.

34
00:02:29,720 --> 00:02:32,960
Let me cancel the request and send again.

35
00:02:32,960 --> 00:02:40,730
Now indeed I have the post that has been fetched, but what you want to do is that you want to display

36
00:02:40,730 --> 00:02:43,310
all posts, which is not this.

37
00:02:43,310 --> 00:02:45,350
So there is implementation.

38
00:02:45,530 --> 00:02:50,750
So here instead of this I'm going to make use of it's not ID.

39
00:02:51,370 --> 00:02:58,260
Meaning that you won't find all posts which is not equal to the ID in the params.

40
00:02:58,270 --> 00:03:04,030
So now with this one, if we send again now I'm going to see array of posts.

41
00:03:04,360 --> 00:03:11,860
You can see that we have all the posts with exception of the post with ID of one meaning that we have

42
00:03:11,860 --> 00:03:13,150
filtered out that.

43
00:03:13,150 --> 00:03:20,680
So now that we get this one, we can go ahead and then re update the file, meaning rewrite the content

44
00:03:20,680 --> 00:03:21,640
onto the file.

45
00:03:21,760 --> 00:03:23,410
So here we go.

46
00:03:23,530 --> 00:03:29,050
As we did for creating, we can copy the same functionality and bring it here.

47
00:03:29,230 --> 00:03:37,060
So let's look at create post here, which is this and we want this function that saves a content onto

48
00:03:37,060 --> 00:03:37,540
a file.

49
00:03:37,540 --> 00:03:44,020
So let me collapse this one and then copy this one and now I'm back to delete down here.

50
00:03:44,020 --> 00:03:51,610
Let me paste it now instead of post data here we are passing in the filtered post and that is it.

51
00:03:51,610 --> 00:03:52,840
And that is it.

52
00:03:52,840 --> 00:03:55,150
So here is our.

53
00:03:56,100 --> 00:03:58,140
I start to two.

54
00:03:58,470 --> 00:04:00,230
So now we are done with it.

55
00:04:00,240 --> 00:04:08,400
So let me try again and hit St and Nancy that post created but instead is post deleted.

56
00:04:08,400 --> 00:04:12,930
So here post deleted the message.

57
00:04:13,860 --> 00:04:19,050
But if you check the file, we see that we don't have that post.

58
00:04:19,050 --> 00:04:20,279
Now we have two posts.

59
00:04:20,490 --> 00:04:25,680
Let's say I want to delete this post, I'll provide the of 2022.

60
00:04:25,680 --> 00:04:30,240
So here we go and provide 2020 to 2020.

61
00:04:30,240 --> 00:04:31,860
Sorry 2020.

62
00:04:32,160 --> 00:04:35,010
So hit st and now post deleted.

63
00:04:35,070 --> 00:04:36,060
You see that.

64
00:04:36,060 --> 00:04:40,170
Let me check the file and now we have one post.

65
00:04:40,170 --> 00:04:44,640
And if I make a request to fetch a post, I have one post.

66
00:04:44,640 --> 00:04:46,090
So this is how we can work.

67
00:04:46,110 --> 00:04:47,370
Delete a post.

68
00:04:47,610 --> 00:04:51,680
Let me add more post in the mix by creating this now post credit.

69
00:04:51,690 --> 00:04:58,110
If I check the file now I have to post the last CRUD operation is what we call update.

70
00:04:58,110 --> 00:05:02,790
So let's see how we can update the post inside our route.

