1
00:00:00,180 --> 00:00:05,070
In this video we are going to implement deleting of a post.

2
00:00:05,340 --> 00:00:08,670
The first step is let's look at the root for that.

3
00:00:08,670 --> 00:00:09,960
That is the endpoint.

4
00:00:09,960 --> 00:00:18,840
If you go to root post and post dot G's, let's scroll down and let's have a look for the delete, which

5
00:00:18,840 --> 00:00:19,620
is this.

6
00:00:19,710 --> 00:00:27,420
So it means going to be API where you want post and then the ID, the next one going to be the controller

7
00:00:27,420 --> 00:00:29,010
for the real action.

8
00:00:29,010 --> 00:00:37,020
So post and let's have a look and this is a controller for deleting or what we need is to pass in the

9
00:00:37,020 --> 00:00:40,050
ID of the post that we want to delete.

10
00:00:40,170 --> 00:00:43,980
So let's get back to the client side and implement that.

11
00:00:43,980 --> 00:00:45,330
So here we go.

12
00:00:45,360 --> 00:00:49,710
That is inside views, post and post details.

13
00:00:50,530 --> 00:00:54,790
And we are targeting this form to be able to delete.

14
00:00:55,000 --> 00:01:01,900
And for client side form, it doesn't support what is called delete and put method unless you use the

15
00:01:01,900 --> 00:01:04,500
package core method override.

16
00:01:04,510 --> 00:01:06,940
So let's provide the attribute.

17
00:01:06,940 --> 00:01:08,980
The first one going to be the action.

18
00:01:10,590 --> 00:01:18,960
And this action will point to the endpoint, which is API forward slash vision, one forward slash post

19
00:01:18,960 --> 00:01:21,030
and then the ID of the post.

20
00:01:21,150 --> 00:01:25,830
And lucky for us, we have the post here so you can have the ID.

21
00:01:25,860 --> 00:01:35,790
So I'm going to use GCM and the ID of the post is going to be equal to post DOT and that's called ID

22
00:01:35,790 --> 00:01:38,730
and I'm going to use the method overwrite.

23
00:01:38,730 --> 00:01:45,930
So question mark and that's called method and is equal to delete.

24
00:01:47,190 --> 00:01:55,200
And provide the method code post overwrite the post to use delete method.

25
00:01:55,470 --> 00:01:56,870
And that is it.

26
00:01:56,880 --> 00:01:58,470
We are done with the deleting.

27
00:01:58,470 --> 00:02:02,430
So now let's try out and see if it's going to work.

28
00:02:02,820 --> 00:02:09,419
Now let's refresh the entire application and I want to delete one of these.

29
00:02:09,419 --> 00:02:12,780
So I'll click on this one and click on delete.

30
00:02:13,320 --> 00:02:19,710
And I got not authorized simply means that I'm not allowed to delete this post.

31
00:02:19,710 --> 00:02:22,800
So again, let me refresh it and let me look back.

32
00:02:22,800 --> 00:02:27,170
I think I have it look in and indeed I'm not looking.

33
00:02:27,180 --> 00:02:35,370
You can see that we are handling all the error use cases, so let me provide ban, add gmail or email.

34
00:02:36,630 --> 00:02:38,730
At gmail.com.

35
00:02:39,490 --> 00:02:41,710
One, two, three, four, five, six.

36
00:02:41,980 --> 00:02:42,790
Look in.

37
00:02:43,830 --> 00:02:46,230
And let me go to homepage.

38
00:02:47,440 --> 00:02:49,300
And delete one of these.

39
00:02:51,980 --> 00:02:55,130
And you can see delete was successful.

40
00:02:55,380 --> 00:03:01,880
Okay, so when I go back to the home page and let's go to home and let's have a look at the remaining

41
00:03:01,880 --> 00:03:06,380
posts and you can see that one is gone forever and that is it.

42
00:03:06,830 --> 00:03:07,640
All right.

43
00:03:07,640 --> 00:03:12,420
So let's go ahead and then redirects after successfully deleting.

44
00:03:12,440 --> 00:03:16,460
So here that is inside the controller of posts.

45
00:03:16,730 --> 00:03:18,980
And this is a message that was sent.

46
00:03:18,980 --> 00:03:29,660
So let me remove this one and make use of redirect going to be red dot redirect and go to homepage and

47
00:03:29,660 --> 00:03:30,950
simple as that.

48
00:03:31,130 --> 00:03:39,710
In case there's an error, let's go ahead and also send the error as red dot render and the template

49
00:03:39,710 --> 00:03:41,990
is post and.

50
00:03:42,770 --> 00:03:43,800
Post detailed.

51
00:03:44,420 --> 00:03:45,020
All right.

52
00:03:45,020 --> 00:03:47,330
And I'm going to pass in the error.

53
00:03:52,780 --> 00:03:55,060
Let me copy the same error response.

54
00:03:55,060 --> 00:03:59,050
And down here let me paste as that.

55
00:03:59,050 --> 00:04:05,380
And for the error message, I want the one from the server that is error dot message.

56
00:04:06,610 --> 00:04:07,720
All right, cool.

57
00:04:07,810 --> 00:04:12,990
Next is let's try and delete a post created by a different user.

58
00:04:13,000 --> 00:04:15,390
So when I go to continue.

59
00:04:16,399 --> 00:04:18,320
And I click on this.

60
00:04:18,980 --> 00:04:25,790
We got this error simply because we are not passing in the post inside the delete controller.

61
00:04:26,420 --> 00:04:31,670
Remember for the post details template it accept what is called the post.

62
00:04:31,670 --> 00:04:36,080
So let's provide the post also inside here as that.

63
00:04:36,840 --> 00:04:44,340
Let's also provide the post here as empty that is inside the catch block or even here we can give it

64
00:04:44,340 --> 00:04:45,450
as empty post.

65
00:04:45,450 --> 00:04:49,710
But for the meantime, let's maintain this one to make the delete work.

66
00:04:49,890 --> 00:04:52,650
All right, so let's refresh again.

67
00:04:53,470 --> 00:04:55,150
And click on delete.

68
00:04:56,000 --> 00:05:00,350
And let's refresh and let's see where we can go back home.

69
00:05:00,350 --> 00:05:03,950
And let's see if indeed the post has been deleted.

70
00:05:04,520 --> 00:05:10,680
Well, we couldn't delete this post because this post belongs to a different user.

71
00:05:10,700 --> 00:05:13,550
But this is a post that belongs to us.

72
00:05:13,560 --> 00:05:17,360
So when I click on this one and I click on delete.

73
00:05:18,520 --> 00:05:21,150
You can see that I'm able to delete that.

74
00:05:21,160 --> 00:05:28,720
So let's find a way to display an error in case a user is trying to delete a post that does not belong

75
00:05:28,720 --> 00:05:29,920
to him or her.

76
00:05:30,220 --> 00:05:36,100
And for this functionality we have already implemented in the API development.

77
00:05:36,100 --> 00:05:39,550
But for the client side, this is what we are going to do.

78
00:05:39,640 --> 00:05:47,770
We are going to show these two icons to the owner of the posts and hide from not authorized user.

79
00:05:47,860 --> 00:05:55,060
Or we can display the error message that you are not authorized to delete this post.

80
00:05:55,060 --> 00:06:01,240
But I think hiding these icons is the best way to go for the client side.

81
00:06:01,240 --> 00:06:03,850
So let's get into that in the next video.

