1
00:00:00,260 --> 00:00:06,290
Okay, So now we're going to be moving to the back end because we need to work with orders and we don't

2
00:00:06,290 --> 00:00:08,740
have we don't even have an orders controller yet.

3
00:00:08,750 --> 00:00:16,370
So what I want to do is the same thing we did with users where we just added all of these methods and

4
00:00:16,370 --> 00:00:23,180
we added all the routes and just just responded with a string for all of them just to just to get them

5
00:00:23,180 --> 00:00:23,750
there.

6
00:00:23,750 --> 00:00:25,940
And then we'll start to work one by one.

7
00:00:25,940 --> 00:00:34,610
So let's create a new file in the back End Routes folder and we're going to call this order Routes Dot.

8
00:00:34,610 --> 00:00:37,430
JS Okay, so that will be our routes.

9
00:00:37,430 --> 00:00:40,700
And then in controllers, let's create an order.

10
00:00:41,610 --> 00:00:42,730
Controller.

11
00:00:43,080 --> 00:00:46,770
JS And we'll start off with the controller.

12
00:00:46,770 --> 00:00:55,320
So I'm going to import actually let's copy from the product controller, just the first function here

13
00:00:55,320 --> 00:00:56,910
and the imports.

14
00:00:57,820 --> 00:01:01,870
And because we want the async handler now, we want the order.

15
00:01:03,460 --> 00:01:04,390
Order model.

16
00:01:04,390 --> 00:01:09,460
So let's say models slash order model.

17
00:01:09,460 --> 00:01:13,780
And for this first one here, this is going to be to create a new order.

18
00:01:13,780 --> 00:01:17,650
So let's change this to create new order.

19
00:01:17,650 --> 00:01:27,700
And the route is going to be a post request to API slash orders and this is going to be private.

20
00:01:28,600 --> 00:01:33,760
So this is the route that we're going to hit when we go through the you know, we go through here and

21
00:01:33,760 --> 00:01:36,250
we go through checkout and we place an order.

22
00:01:36,250 --> 00:01:37,630
It's going to create one.

23
00:01:37,630 --> 00:01:43,690
So let's change the name of this function to, let's say, create order.

24
00:01:45,710 --> 00:01:47,480
And or let's call it.

25
00:01:48,760 --> 00:01:50,050
We'll call it ad.

26
00:01:51,190 --> 00:01:53,230
Order items.

27
00:01:53,870 --> 00:01:55,520
Because it can be more than one.

28
00:01:55,520 --> 00:02:03,530
So we'll say add order items and let's see, we'll get rid of this and we'll just we're just going to

29
00:02:03,530 --> 00:02:04,670
respond.

30
00:02:05,210 --> 00:02:08,780
So res dot send we're just going to respond with.

31
00:02:09,419 --> 00:02:10,410
Add.

32
00:02:11,910 --> 00:02:13,830
Order items for now.

33
00:02:14,110 --> 00:02:15,900
Okay, so that's the first one.

34
00:02:16,200 --> 00:02:19,200
So let's copy that.

35
00:02:20,380 --> 00:02:27,580
And the next one is going to be to get the logged in users orders, because on the profile we want to

36
00:02:27,580 --> 00:02:30,340
show the orders that the user made.

37
00:02:30,340 --> 00:02:32,680
So we'll say get logged in.

38
00:02:33,270 --> 00:02:34,470
Users orders.

39
00:02:34,470 --> 00:02:35,790
And that's going to be.

40
00:02:36,720 --> 00:02:38,090
Get request.

41
00:02:38,160 --> 00:02:39,900
So we'll say get.

42
00:02:39,900 --> 00:02:48,690
And it's going to be API slash orders, slash my orders and we'll be able to get the ID through the

43
00:02:48,690 --> 00:02:50,760
token that's in the cookie.

44
00:02:51,060 --> 00:03:01,410
So let's call this one, get my orders and we'll just respond for now with get my orders.

45
00:03:02,380 --> 00:03:09,250
Okay, Then we're going to get we're going to have the get order by ID, So let's paste that in and

46
00:03:09,250 --> 00:03:11,170
let's say this is going to get.

47
00:03:11,770 --> 00:03:15,490
Order by D and that's going to be a get request.

48
00:03:16,140 --> 00:03:21,510
Two API orders slash and then whatever the ID, it's going to be private.

49
00:03:21,720 --> 00:03:23,740
All of these are going to be private.

50
00:03:23,760 --> 00:03:30,030
And then let's change the name of the function to get order by ID.

51
00:03:32,100 --> 00:03:34,530
And then we'll just have a string here.

52
00:03:35,360 --> 00:03:37,190
That says Get order by D.

53
00:03:38,190 --> 00:03:38,670
All right.

54
00:03:38,670 --> 00:03:44,670
So the next one is going to be update order to paid because when the order is created, it's not going

55
00:03:44,670 --> 00:03:46,950
to be the paid value.

56
00:03:46,980 --> 00:03:48,510
If we look at the model.

57
00:03:50,000 --> 00:03:53,420
You can see that we have a is paid.

58
00:03:53,420 --> 00:03:56,030
So that's false by default.

59
00:03:56,030 --> 00:04:01,700
But when we go through when we place an order, we're going to want to at some point mark it as paid.

60
00:04:01,700 --> 00:04:04,910
So that's what this is going to do, let's say update.

61
00:04:05,650 --> 00:04:08,410
Order to paid and it's going to be a.

62
00:04:09,780 --> 00:04:10,500
Let's see.

63
00:04:10,500 --> 00:04:14,220
It's going to be it's actually going to be a get request.

64
00:04:15,750 --> 00:04:22,530
And it's going to be two API orders and then the ID, so colon ID and then slash pay.

65
00:04:23,200 --> 00:04:24,550
It's going to be private.

66
00:04:24,550 --> 00:04:27,850
And let's make the function name.

67
00:04:28,400 --> 00:04:29,840
Call it update.

68
00:04:31,420 --> 00:04:34,240
Update order to paid.

69
00:04:35,520 --> 00:04:38,760
And then in here we'll just say update order to paid.

70
00:04:39,310 --> 00:04:42,420
And then we also want want to update to delivered.

71
00:04:42,430 --> 00:04:47,250
So basically we're going to be able to as an admin, mark it as shipped and delivered.

72
00:04:47,260 --> 00:04:57,100
So let's copy that and we'll say update to delivered and it's going to be get request to API orders.

73
00:04:57,830 --> 00:04:58,880
Deliver.

74
00:05:00,090 --> 00:05:06,180
Private and we'll say update order to delivered.

75
00:05:07,290 --> 00:05:09,990
And yeah, we'll just change this.

76
00:05:09,990 --> 00:05:11,100
Delivered.

77
00:05:12,280 --> 00:05:12,640
Okay.

78
00:05:12,640 --> 00:05:15,970
And then the last one we're going to have is just to get all orders.

79
00:05:16,650 --> 00:05:19,170
So let's say description.

80
00:05:19,990 --> 00:05:21,790
Get all orders.

81
00:05:21,820 --> 00:05:29,140
Now this is actually going to be admin and so is so is to update to delivered.

82
00:05:30,040 --> 00:05:32,410
So we'll say private slash admin.

83
00:05:32,980 --> 00:05:38,170
And then for get all orders, that's just going to be a get request to API orders.

84
00:05:38,230 --> 00:05:40,540
That's also going to be admin.

85
00:05:41,210 --> 00:05:44,720
Because we don't want regular users to see other people's orders.

86
00:05:44,810 --> 00:05:47,780
So let's say get orders.

87
00:05:48,780 --> 00:05:52,650
It should be uppercase, and we'll just.

88
00:05:53,500 --> 00:05:54,880
Have this say.

89
00:05:55,610 --> 00:05:58,100
Get all orders.

90
00:05:59,190 --> 00:06:01,830
All right, Now we just want to export all this.

91
00:06:02,820 --> 00:06:05,640
So add order that that.

92
00:06:06,460 --> 00:06:07,060
Oops.

93
00:06:08,090 --> 00:06:11,870
That, that and get orders.

94
00:06:12,230 --> 00:06:12,860
Okay.

95
00:06:12,980 --> 00:06:15,200
So that should do it for the functions.

96
00:06:15,200 --> 00:06:23,180
Now let's go into order routes and now we want to let's see, how should we do this?

97
00:06:23,180 --> 00:06:25,670
Let's copy.

98
00:06:28,620 --> 00:06:32,580
We'll copy the user routes because there's a few things we need.

99
00:06:32,820 --> 00:06:36,660
That's also in there, like the the router.

100
00:06:37,770 --> 00:06:39,230
The the middleware.

101
00:06:39,240 --> 00:06:40,860
We're going to want that now.

102
00:06:40,890 --> 00:06:45,710
We want to bring in our controller stuff from the order controller.

103
00:06:45,720 --> 00:06:49,500
And what I'm going to do is just copy these.

104
00:06:50,720 --> 00:06:54,890
And put those in here because those are the ones we want to bring in.

105
00:06:54,890 --> 00:06:57,530
And then let's see for the routes.

106
00:06:58,600 --> 00:06:59,890
We're going to have.

107
00:07:01,020 --> 00:07:01,560
See.

108
00:07:01,560 --> 00:07:06,600
So for the index, the post request is going to be to add an order item.

109
00:07:06,600 --> 00:07:09,960
So let's change this to add.

110
00:07:10,550 --> 00:07:15,380
Order items, and we're going to want that to be just for registered users.

111
00:07:15,380 --> 00:07:18,470
So we're going to add the protect middleware in front of it.

112
00:07:19,220 --> 00:07:19,670
Okay.

113
00:07:19,670 --> 00:07:21,050
Now the.

114
00:07:22,230 --> 00:07:27,120
Um, get request is going to be to get all orders, which is an admin function.

115
00:07:27,120 --> 00:07:30,270
So we already have right here protect and admin.

116
00:07:30,270 --> 00:07:33,690
We just want to change the name of the function to get orders.

117
00:07:35,150 --> 00:07:40,430
Okay, Now, next we're going to do actually, you know what?

118
00:07:40,430 --> 00:07:44,570
Let's just get rid of the rest of these and then we'll just type out the rest that we need.

119
00:07:44,930 --> 00:07:46,670
So for the.

120
00:07:47,320 --> 00:07:50,140
To get my orders.

121
00:07:50,290 --> 00:07:53,440
We're going to have the route B slash mine.

122
00:07:55,200 --> 00:08:00,420
And yeah, we need protect middleware and then call get my orders.

123
00:08:00,970 --> 00:08:01,260
Okay.

124
00:08:01,290 --> 00:08:07,110
Next we're going to do for the ID If it's a get request, we want to get the order by ID.

125
00:08:08,280 --> 00:08:10,500
So that should actually be.

126
00:08:11,980 --> 00:08:13,140
I believe that should be for.

127
00:08:13,180 --> 00:08:14,440
Yeah, that should be admin.

128
00:08:14,440 --> 00:08:19,210
So let's do protect and admin middleware.

129
00:08:20,820 --> 00:08:25,490
And yeah, get order by D, then we're going to have our pay.

130
00:08:25,500 --> 00:08:32,460
So it's going to be the ID and then pay and it's going to be a put request and protect and then update

131
00:08:32,460 --> 00:08:33,690
order to paid.

132
00:08:34,350 --> 00:08:42,870
And then finally we have the deliver which is going to be protect and admin and update order to delivered.

133
00:08:44,550 --> 00:08:44,970
All right.

134
00:08:44,970 --> 00:08:49,020
So now we just want to check and see if these actually work.

135
00:08:49,020 --> 00:08:51,540
But before we do that, we have to go to our server.js.

136
00:08:51,540 --> 00:08:57,390
And just like we did with the other routes, we have to bring in the order routes.

137
00:08:58,070 --> 00:09:01,310
So autoroutes change this.

138
00:09:02,160 --> 00:09:03,690
To order.

139
00:09:03,690 --> 00:09:06,600
And we also want to add right here.

140
00:09:08,650 --> 00:09:09,940
Bring that down.

141
00:09:10,910 --> 00:09:14,510
Change that to order routes and API.

142
00:09:15,430 --> 00:09:16,420
Orders.

143
00:09:17,020 --> 00:09:18,370
Okay, so let's try this out.

144
00:09:18,370 --> 00:09:20,140
We're going to open up, Postman.

145
00:09:22,650 --> 00:09:25,230
And I'm just going to add these as we go, right?

146
00:09:25,230 --> 00:09:28,200
So I'm going to create a new a new collection here.

147
00:09:29,610 --> 00:09:30,840
Let's see, how do we do this?

148
00:09:30,840 --> 00:09:32,640
Plus new collection.

149
00:09:32,790 --> 00:09:35,460
And let's call this orders.

150
00:09:37,670 --> 00:09:38,240
Okay.

151
00:09:38,240 --> 00:09:41,270
And then in orders, let's create a new.

152
00:09:42,780 --> 00:09:43,400
Let's see.

153
00:09:43,410 --> 00:09:44,980
What do we want to start with here?

154
00:09:45,000 --> 00:09:47,490
Let's create a new request.

155
00:09:50,640 --> 00:09:54,870
And this is going to be to create a new order.

156
00:09:54,870 --> 00:09:58,140
So let's say create new order.

157
00:09:59,240 --> 00:10:05,780
And it's going to be a post request and the URL is going to be our base URL.

158
00:10:06,470 --> 00:10:08,930
Slash orders.

159
00:10:10,050 --> 00:10:11,370
And that's it.

160
00:10:11,370 --> 00:10:12,750
Just slash orders.

161
00:10:13,680 --> 00:10:18,430
So let's send that and we see add order items, which is what we want.

162
00:10:18,450 --> 00:10:19,890
So let's save that.

163
00:10:21,160 --> 00:10:22,270
We know that works.

164
00:10:22,270 --> 00:10:22,870
Thatrillionoute.

165
00:10:22,900 --> 00:10:25,360
Now let's create another request.

166
00:10:27,760 --> 00:10:29,500
So ad request.

167
00:10:29,500 --> 00:10:33,700
So this one is going to be to get the logged in users orders.

168
00:10:33,700 --> 00:10:43,300
So get logged in user orders and that's going to be a get request to let's see, it's going to be our

169
00:10:43,300 --> 00:10:44,560
base URL.

170
00:10:45,400 --> 00:10:47,290
Slash orders.

171
00:10:47,470 --> 00:10:54,940
And it's going to be what is the root slash my orders.

172
00:10:58,080 --> 00:11:02,340
Okay so it says not authorizes admin, which is right.

173
00:11:02,340 --> 00:11:03,960
That's good because.

174
00:11:04,560 --> 00:11:05,760
Well wait a minute.

175
00:11:06,540 --> 00:11:07,890
To get my orders.

176
00:11:07,890 --> 00:11:09,750
That shouldn't be an admin.

177
00:11:11,330 --> 00:11:12,110
Uh, let's see.

178
00:11:12,110 --> 00:11:13,460
Let me go back.

179
00:11:15,960 --> 00:11:17,910
That should just be regular protect.

180
00:11:17,910 --> 00:11:19,680
So get my orders.

181
00:11:22,880 --> 00:11:23,240
Hmm.

182
00:11:25,740 --> 00:11:26,280
You know what?

183
00:11:26,280 --> 00:11:27,210
This.

184
00:11:27,630 --> 00:11:29,100
I'm not sure why.

185
00:11:31,960 --> 00:11:33,520
This is doing this.

186
00:11:33,520 --> 00:11:36,520
So we have router dot route slash.

187
00:11:36,940 --> 00:11:39,030
We have our post and then.

188
00:11:39,040 --> 00:11:40,420
Oh it's mine.

189
00:11:40,420 --> 00:11:45,580
That's why it's not my orders slash mine.

190
00:11:47,820 --> 00:11:48,530
There we go.

191
00:11:48,540 --> 00:11:48,930
All right.

192
00:11:48,930 --> 00:11:50,610
So we'll save that route.

193
00:11:51,440 --> 00:11:52,820
Or thatrillionequest.

194
00:11:53,330 --> 00:11:57,470
Let's make a new request and let's see this one.

195
00:11:58,940 --> 00:12:00,500
What is this one going to be?

196
00:12:02,330 --> 00:12:03,220
Let's see.

197
00:12:03,230 --> 00:12:07,760
Let's do get order by ID which.

198
00:12:10,130 --> 00:12:11,360
Is that admin?

199
00:12:12,520 --> 00:12:13,740
Get order by D.

200
00:12:13,750 --> 00:12:14,740
Yeah, that's admin.

201
00:12:14,740 --> 00:12:19,090
So in orders I'm going to create a folder.

202
00:12:20,020 --> 00:12:22,510
Just like I did with with users.

203
00:12:22,510 --> 00:12:24,490
And let's call this admin.

204
00:12:25,170 --> 00:12:28,980
Just to differentiate what routes are actually admins.

205
00:12:29,560 --> 00:12:31,930
So let's see in here.

206
00:12:31,960 --> 00:12:38,140
Admin Let's add a request and say get order by ID.

207
00:12:39,990 --> 00:12:43,140
It's going to be a get request to base URL.

208
00:12:43,810 --> 00:12:47,770
Slash orders slash and then the ID.

209
00:12:48,730 --> 00:12:52,270
Now, I don't have an ID right now, so I'm just going to put in.

210
00:12:53,330 --> 00:12:53,840
Um.

211
00:12:56,840 --> 00:12:57,820
Let's see.

212
00:12:57,830 --> 00:13:00,140
Yeah, we'll just put a one for now.

213
00:13:00,140 --> 00:13:01,880
And if I send that.

214
00:13:05,650 --> 00:13:05,980
Hmm.

215
00:13:09,140 --> 00:13:10,580
Unauthorized is admin.

216
00:13:12,970 --> 00:13:14,470
Oh, yeah, I guess that makes sense.

217
00:13:14,470 --> 00:13:16,210
We're not logged in as admin.

218
00:13:17,100 --> 00:13:21,060
We are logged in, though I should have mentioned that in the beginning that you need to be logged in

219
00:13:21,060 --> 00:13:22,500
for most of these.

220
00:13:22,650 --> 00:13:25,350
So what I'll do is log in as admin.

221
00:13:25,350 --> 00:13:27,030
So over here I'm going to go.

222
00:13:27,060 --> 00:13:28,260
I'm going to log out.

223
00:13:29,140 --> 00:13:31,480
And then I'm going to auth user.

224
00:13:32,030 --> 00:13:34,310
And change this.

225
00:13:36,000 --> 00:13:37,860
To admin.

226
00:13:39,900 --> 00:13:45,390
So now if I go back to get order by ID and we look at the body that works.

227
00:13:46,020 --> 00:13:47,520
So we'll save that.

228
00:13:48,690 --> 00:13:49,890
Okay, so let's see.

229
00:13:49,890 --> 00:13:50,880
What else do we have?

230
00:13:50,910 --> 00:13:55,230
For orders, we have update order to paid which.

231
00:13:56,910 --> 00:13:59,400
Which is not admin?

232
00:13:59,400 --> 00:14:01,830
I don't believe so.

233
00:14:01,830 --> 00:14:03,690
Let's say add request.

234
00:14:05,710 --> 00:14:09,310
And that's going to be a put put request.

235
00:14:10,200 --> 00:14:15,630
We'll call it update order to paid.

236
00:14:17,590 --> 00:14:27,700
And it's going to be, let's see, base URL slash orders, slash the ID and then slash pay.

237
00:14:28,180 --> 00:14:32,740
We'll put one for the ID, send update or it's a paid good.

238
00:14:32,740 --> 00:14:34,480
We'll save that request.

239
00:14:34,570 --> 00:14:37,420
Then we're going to do update to delivered.

240
00:14:38,010 --> 00:14:41,370
Which is an admin function.

241
00:14:41,640 --> 00:14:45,330
So let's add a request here.

242
00:14:45,930 --> 00:14:48,120
We'll say update to.

243
00:14:49,100 --> 00:14:50,180
Delivered.

244
00:14:51,000 --> 00:14:52,440
Put request.

245
00:14:53,550 --> 00:14:56,310
Base URL slash orders.

246
00:14:56,310 --> 00:15:00,840
Slash id slash deliver.

247
00:15:02,870 --> 00:15:03,770
Send that.

248
00:15:03,770 --> 00:15:04,680
That works.

249
00:15:04,700 --> 00:15:05,690
Save.

250
00:15:06,920 --> 00:15:10,490
And then we have get orders, which is admin.

251
00:15:10,490 --> 00:15:12,440
So let's say add request.

252
00:15:13,200 --> 00:15:16,140
Say get all orders.

253
00:15:16,770 --> 00:15:19,470
Get requests to the base URL.

254
00:15:20,240 --> 00:15:22,130
Slash orders.

255
00:15:22,750 --> 00:15:25,240
Send that and we'll save.

256
00:15:25,480 --> 00:15:28,390
Okay, so now we have all the controller methods.

257
00:15:28,420 --> 00:15:30,760
Our controller functions hook to the roots.

258
00:15:30,760 --> 00:15:34,900
And then in Postman, we have all of our routes all set up.

259
00:15:35,690 --> 00:15:42,020
Okay, so now what we'll do in the next video is make it so that we can actually create a new order.

