1
00:00:00,450 --> 00:00:05,760
In this video, we are going to create a controller to update a post.

2
00:00:05,760 --> 00:00:13,140
So inside the post controller, which is this one, let's collapse the finished one and we have the

3
00:00:13,140 --> 00:00:14,580
last one here.

4
00:00:15,030 --> 00:00:20,010
The first step is that we need to find the post that we want to update.

5
00:00:20,010 --> 00:00:27,330
So I'm going to add a comment to this place as that and I'm going to structure the field.

6
00:00:27,350 --> 00:00:37,590
So I'm going to be the title, the description, the category, and that is what we need and which is

7
00:00:37,590 --> 00:00:40,470
equal to the red dot body.

8
00:00:40,920 --> 00:00:44,210
The logic would be the same for the list controller.

9
00:00:44,220 --> 00:00:50,940
First of all, we are going to find the post and I can copy this one and even this one as well, because

10
00:00:50,940 --> 00:00:56,010
you will check if the post belongs to the user who is trying to update.

11
00:00:56,010 --> 00:00:59,400
So let me pass it here as we did before.

12
00:00:59,730 --> 00:01:07,110
So the first step is that we are finding the post by ID and on that we can check if the look in user

13
00:01:07,110 --> 00:01:08,820
is the owner of the post.

14
00:01:08,940 --> 00:01:18,570
Then after that we can go ahead and then update the post here as that const post updated is equal to

15
00:01:18,570 --> 00:01:21,570
a weight and post dot.

16
00:01:21,570 --> 00:01:31,380
Find by ID and update and the id can be found on the red dot params dot ID.

17
00:01:31,410 --> 00:01:34,620
Then the properties that I want to update.

18
00:01:34,650 --> 00:01:41,340
I want the user to update the title description and then the category.

19
00:01:41,920 --> 00:01:45,250
And then the image as well as I provide the image here.

20
00:01:45,250 --> 00:01:50,950
And remember, we can have the image on red dot file dot path.

21
00:01:51,790 --> 00:01:56,860
So let's go ahead and check if a user is trying to send an empty data.

22
00:01:56,860 --> 00:02:02,310
So the same way we did for the create are going to use the same concept here.

23
00:02:02,320 --> 00:02:04,000
So I'll copy this one.

24
00:02:04,540 --> 00:02:08,380
So inside the updates, I'm going to place it here.

25
00:02:09,289 --> 00:02:12,480
So here we are checking for empty data.

26
00:02:12,500 --> 00:02:17,870
So now that we have it, let's go ahead and send back the response as data.

27
00:02:18,050 --> 00:02:21,670
And then the post are data.

28
00:02:21,710 --> 00:02:24,710
So let's go ahead and then make the requests.

29
00:02:24,920 --> 00:02:26,530
Let's find a post.

30
00:02:26,540 --> 00:02:34,580
Let's say I want to update the first one for HTML, so I'll copy that and for update our provider ID.

31
00:02:34,670 --> 00:02:42,190
And remember, we are passing an image, so we need to pass more to as a murderer to the update route.

32
00:02:42,200 --> 00:02:45,280
So here we go for the update.

33
00:02:45,290 --> 00:02:53,690
First of all, we add the protected and then the mortar package as upload dot single and we pass in

34
00:02:53,690 --> 00:02:56,450
the file and comma separated.

35
00:02:56,780 --> 00:03:00,410
So let's go ahead and make the request So under body.

36
00:03:00,440 --> 00:03:03,720
Click on form and click on files.

37
00:03:03,740 --> 00:03:04,760
The field name.

38
00:03:04,760 --> 00:03:10,010
I want to update the title only and I would change this one to CSS.

39
00:03:10,550 --> 00:03:14,060
And then for the file it's going to be the file.

40
00:03:15,850 --> 00:03:18,240
And then the fire to update.

41
00:03:18,250 --> 00:03:21,670
So I would choose this one.

42
00:03:22,890 --> 00:03:31,530
And I think the chicken is not ideal because a user can choose to update the title or description.

43
00:03:31,530 --> 00:03:33,750
So let's remove the chicken here.

44
00:03:34,850 --> 00:03:39,080
And then we need to pass additional configuration to the update.

45
00:03:39,090 --> 00:03:47,170
So here, come on, object and name to true meaning that we want to have the updated data or record.

46
00:03:47,180 --> 00:03:50,180
So now let's hit send and let's see.

47
00:03:50,180 --> 00:03:57,080
You can see that it has been updated to CCIS and you can see the image has been updated.

48
00:03:57,080 --> 00:04:03,290
If you copy the image and open a browser, you can see that we have newly updated image.

49
00:04:03,590 --> 00:04:07,970
So let's say that I want to update a post belong to somebody else.

50
00:04:07,970 --> 00:04:15,230
So let me make a request to find all posts and I want to find the main stock post.

51
00:04:15,230 --> 00:04:23,510
So down here, which is this and I'll copy the ID, so let me paste the ID here and let me hit send.

52
00:04:24,050 --> 00:04:29,840
You can see I go back and empty object, meaning that our error is not being caught.

53
00:04:29,870 --> 00:04:34,280
The problem is we need to pass in next year and that is it.

54
00:04:34,280 --> 00:04:40,760
The next one is let's change this text by saying that you are not allowed to update this post.

55
00:04:41,030 --> 00:04:46,370
So now let's make the requests and we have the response back.

56
00:04:46,610 --> 00:04:51,230
And you can see you are not allowed to update this post.

57
00:04:51,260 --> 00:04:52,130
All right, guys.

58
00:04:52,130 --> 00:04:56,150
So here ends the API for post the next section.

59
00:04:56,150 --> 00:04:58,730
We are going to work on the comments.

