1
00:00:01,130 --> 00:00:05,930
In this section, we are going to work on the post API.

2
00:00:06,080 --> 00:00:13,880
The most difficult part of the entire application is complete and that is the user API as compared to

3
00:00:13,880 --> 00:00:15,680
post and comment.

4
00:00:15,680 --> 00:00:21,710
So in this video we are going to create a controller to be able to create a post.

5
00:00:21,710 --> 00:00:28,100
So let's get back to the controllers and post controller before we can create a post.

6
00:00:28,100 --> 00:00:30,230
We need two modules.

7
00:00:30,230 --> 00:00:38,990
First one is the post itself and then the user module because we want to assist with a user to a post.

8
00:00:38,990 --> 00:00:41,060
So let me receive this file.

9
00:00:41,060 --> 00:00:48,230
Likewise, the user module also and let me get back to the post controller and let me require those

10
00:00:48,230 --> 00:00:49,520
two modules.

11
00:00:49,520 --> 00:00:54,050
The first one is a user and then the other one is post.

12
00:00:57,030 --> 00:00:57,910
Next ace.

13
00:00:57,960 --> 00:01:00,120
Let's look at the failures of the posts.

14
00:01:00,120 --> 00:01:03,540
So if you get back to the post model, we need the title.

15
00:01:03,540 --> 00:01:05,310
So let me place it here.

16
00:01:07,910 --> 00:01:11,750
And let me distract from the request body.

17
00:01:11,780 --> 00:01:17,900
So it's going to be as cost equal to rec dot body.

18
00:01:19,420 --> 00:01:22,030
We need the title of the post.

19
00:01:22,660 --> 00:01:25,000
Then the description.

20
00:01:26,370 --> 00:01:28,260
Then the category.

21
00:01:31,350 --> 00:01:33,030
Then the image.

22
00:01:36,150 --> 00:01:38,340
And then the user.

23
00:01:39,390 --> 00:01:39,960
Oh, right.

24
00:01:39,960 --> 00:01:41,820
So let me collapse this one.

25
00:01:42,000 --> 00:01:45,240
So first of all, let's look at the step.

26
00:01:45,450 --> 00:01:49,620
Step number one is we want to find the user.

27
00:01:49,650 --> 00:01:53,280
So inside the trial, our other comment here.

28
00:01:53,550 --> 00:01:56,280
So how can we get the user?

29
00:01:56,430 --> 00:02:01,560
Because of that, we need to put this controller behind authentication.

30
00:02:01,710 --> 00:02:07,380
So let's get back to the post route, which is this one.

31
00:02:07,380 --> 00:02:15,780
And here we are going to require the middleware to check if a user is logged in, which is called protected.

32
00:02:16,050 --> 00:02:26,790
So inside the post route, let me require the file as protected unless I do it manually as const protected

33
00:02:27,240 --> 00:02:35,400
is equal to ..4..4 slash and inside metaverse and protected.

34
00:02:35,940 --> 00:02:42,960
So I'm going to pass this one as a moderate to this, meaning that unless you are logged in before you

35
00:02:42,960 --> 00:02:44,460
can create a post.

36
00:02:44,640 --> 00:02:51,650
So let's make a request to the endpoint to make sure that everything is fine inside the post collection.

37
00:02:51,660 --> 00:02:59,490
This is the endpoint when I hit send, you can see that not authorized meaning unless I look in.

38
00:02:59,490 --> 00:03:02,910
So let me look in from here as that.

39
00:03:02,910 --> 00:03:08,640
Now I have successfully logged in, so let's hit send and let's look at the response.

40
00:03:09,150 --> 00:03:17,370
Well, we got a beautiful error, so let me check the terminal and we are doing something wrong about

41
00:03:17,370 --> 00:03:18,940
red dot body.

42
00:03:18,960 --> 00:03:24,330
So let me get back to the controller and instead of boy is buddy.

43
00:03:24,360 --> 00:03:26,670
All right, so let me hit send again.

44
00:03:28,140 --> 00:03:31,140
And it has been called successfully.

45
00:03:31,320 --> 00:03:34,800
So let's look at the road map now that we are locked in.

46
00:03:34,830 --> 00:03:38,160
We have access to the user as cost.

47
00:03:38,190 --> 00:03:43,560
User ID is equal to rec dot session.

48
00:03:46,330 --> 00:03:47,980
Dot user oath.

49
00:03:49,270 --> 00:03:54,490
So if you console console.log user ID, you're going to see the logged in user.

50
00:03:55,390 --> 00:03:56,680
Moment of truth.

51
00:03:58,820 --> 00:04:00,380
Let me check the terminal.

52
00:04:01,230 --> 00:04:08,820
And I have the log in user ID here so I can go ahead and find this user inside our DB.

53
00:04:09,120 --> 00:04:10,740
So let's go ahead and do that.

54
00:04:10,740 --> 00:04:12,660
Let me collapse this once.

55
00:04:13,460 --> 00:04:15,200
With exception of the posts.

56
00:04:15,320 --> 00:04:16,640
So here we go.

57
00:04:17,000 --> 00:04:29,060
Cost user found is equal to await the user model dot find by ID and then the ID is the log in user.

58
00:04:30,440 --> 00:04:34,340
Now we have gotten the log in user.

59
00:04:34,370 --> 00:04:39,110
The next step is that you go ahead and then create the post.

60
00:04:39,110 --> 00:04:40,550
And here we go.

61
00:04:40,580 --> 00:04:43,820
Const post created.

62
00:04:47,600 --> 00:04:52,490
Is equal to await the post model dot grid.

63
00:04:54,540 --> 00:04:57,210
And here I'll pass in the title.

64
00:04:59,240 --> 00:05:08,360
The description, the category, the user, and then the image.

65
00:05:08,600 --> 00:05:09,380
All right.

66
00:05:09,380 --> 00:05:20,180
For the user, we have it inside the session so I can assign the value as req dot session dot user off

67
00:05:20,960 --> 00:05:25,490
or I can use user found underscore ID.

68
00:05:26,480 --> 00:05:33,260
Because now I have the user in the DB and the ID of this user is equally the same as the one we store

69
00:05:33,260 --> 00:05:34,550
inside the session.

70
00:05:34,550 --> 00:05:41,750
So we can maintain this one or we can use the user found the underscore ID.

71
00:05:42,620 --> 00:05:43,220
All right.

72
00:05:43,220 --> 00:05:45,020
So for the image.

73
00:05:45,020 --> 00:05:50,750
For the meantime, let's remove the image here and let's create a post without an image.

74
00:05:50,750 --> 00:05:55,460
So let's also remove from here and we will come back to it as you move on.

75
00:05:55,910 --> 00:06:02,990
The next step is that we need to push the post created into the array of the log in user.

76
00:06:03,020 --> 00:06:13,220
So I'm going to add a comment here and say that push the post created into the array.

77
00:06:15,090 --> 00:06:17,850
Of U.S. Post.

78
00:06:20,240 --> 00:06:25,550
It's going to be, as a user found on that we have a field called post.

79
00:06:25,670 --> 00:06:30,950
If you doubt, let's look at the user model and this is what I'm referring to.

80
00:06:31,040 --> 00:06:39,020
We are pushing the ID of the post inside this property code post, but not the entire post.

81
00:06:39,020 --> 00:06:42,320
And this is what we call by reference.

82
00:06:42,320 --> 00:06:51,650
So here we are going to use push and we are going to push the idea of the post credit going to be post

83
00:06:51,650 --> 00:07:01,100
credit dot underscore ID and we can even post the entire post and mongoose will grab the ID and save

84
00:07:01,100 --> 00:07:03,500
it into this field of posts.

85
00:07:03,500 --> 00:07:08,510
But let's make it more descriptive by providing underscore ID.

86
00:07:09,110 --> 00:07:16,460
Then the last step is we need to save the user going to be as real, save the user because we've made

87
00:07:16,460 --> 00:07:23,540
changes to the user model going to be as await the user found dot save.

88
00:07:24,760 --> 00:07:31,290
To be able to create a post unless we remove the validation from the image for the meantime.

89
00:07:31,300 --> 00:07:33,670
So let's get back to the post model.

90
00:07:34,740 --> 00:07:42,750
Which is this and let's remove the required here for now and let's go ahead and then create a post.

91
00:07:43,170 --> 00:07:48,990
Remember, for the value of the category unless we use one of these.

92
00:07:48,990 --> 00:07:56,850
So let's get back to the endpoint where we can create and let's click on body and let's pass in the

93
00:07:56,850 --> 00:07:57,690
payload.

94
00:07:58,020 --> 00:08:03,570
First is the title of the post, and I'll call this one as Node.js.

95
00:08:03,690 --> 00:08:13,500
And then the description is going to be the best post and then the category.

96
00:08:15,290 --> 00:08:18,320
Is going to be JavaScript.

97
00:08:18,620 --> 00:08:24,440
So let's check the category where yes, indeed, we have JavaScript here.

98
00:08:24,890 --> 00:08:31,100
So that's what we need for now for the user is going to add it automatically because we are using the

99
00:08:31,100 --> 00:08:33,980
log in user or the user found ID here.

100
00:08:34,010 --> 00:08:35,659
So moment of truth.

101
00:08:35,690 --> 00:08:40,880
Let me send back the post created and then let me change this one to data.

102
00:08:41,700 --> 00:08:43,830
So now let me hit send.

103
00:08:45,120 --> 00:08:53,790
And we have the post being created and we have a field code user, meaning the author of this particular

104
00:08:53,790 --> 00:08:54,540
post.

105
00:08:54,630 --> 00:08:58,110
So what about creating a post with an image?

106
00:08:58,140 --> 00:09:00,510
Let's get into that in the next video.

