1
00:00:00,260 --> 00:00:00,620
All right.

2
00:00:00,620 --> 00:00:06,410
So now that we have all the functions here for our order controller, basically just dummy responses,

3
00:00:06,410 --> 00:00:11,120
we're going to start to work on these in the first one I want to do is to create a new order.

4
00:00:11,120 --> 00:00:16,820
So let's get rid of this and we're going to have to get a lot of stuff from the body because that's

5
00:00:16,820 --> 00:00:19,390
what's going to come in through the front end.

6
00:00:19,400 --> 00:00:25,700
So first off, we're going to get the order items, which will be an array of orders, shipping address,

7
00:00:25,880 --> 00:00:27,170
payment method.

8
00:00:27,170 --> 00:00:32,030
We want the items price tax, price, shipping price and total price.

9
00:00:32,030 --> 00:00:36,320
So we're going to get that stuff from the body of the Http request.

10
00:00:36,350 --> 00:00:43,460
Then we want to check to see if there is an order items array, but it's empty and if it is, then we

11
00:00:43,460 --> 00:00:45,770
want to respond with a 400.

12
00:00:45,770 --> 00:00:54,260
So let's say res dot status and we're going to do a 400 and let's say dot Json.

13
00:00:55,510 --> 00:00:59,440
Actually, no, we don't need to do that because we have our error handler.

14
00:00:59,440 --> 00:01:04,840
So just we'll set the status and then we'll throw a new error and say no order items.

15
00:01:05,140 --> 00:01:05,650
Okay.

16
00:01:05,680 --> 00:01:09,580
Else So if there are order items, then we want to proceed.

17
00:01:09,700 --> 00:01:14,740
So we're going to create a new actually I'll go ahead and put this in here.

18
00:01:14,740 --> 00:01:22,750
But order items, we don't want to just put that directly in because if we look at our model, our order

19
00:01:22,750 --> 00:01:29,380
model, we have in order items, this stuff is going to come through right through the front end.

20
00:01:29,380 --> 00:01:35,110
But this isn't the product, which is just the ID of the item for each item.

21
00:01:35,110 --> 00:01:38,980
So we have to add this onto order item.

22
00:01:38,980 --> 00:01:43,450
So what we can do is set this to an object, right?

23
00:01:43,840 --> 00:01:45,580
Actually, no, we don't want to set it to an object.

24
00:01:45,610 --> 00:01:47,170
We want to map through this.

25
00:01:47,170 --> 00:01:51,340
So order items and then we're going to map through that.

26
00:01:51,490 --> 00:01:58,220
And let's say for each order, we'll call it X and we want to return an object from this.

27
00:01:58,220 --> 00:02:03,890
So we're going to use parentheses and then our curly braces and we're going to spread across whatever

28
00:02:03,890 --> 00:02:09,169
that is in that order, which will be this stuff right here, these four fields.

29
00:02:09,169 --> 00:02:11,150
We need to add this product.

30
00:02:11,150 --> 00:02:13,130
So let's say product.

31
00:02:13,130 --> 00:02:18,470
And all that's going to be is the object ID, which we can get from X dot underscore ID.

32
00:02:19,920 --> 00:02:20,280
Okay.

33
00:02:20,280 --> 00:02:27,480
And then we're going to set the ID field, underscore ID to undefined because we don't need that in

34
00:02:27,480 --> 00:02:28,140
here.

35
00:02:28,140 --> 00:02:28,550
Right?

36
00:02:28,570 --> 00:02:34,200
We just have the product, which will be the ID, There's no underscore ID field in here, so we're

37
00:02:34,200 --> 00:02:35,910
setting that to undefined.

38
00:02:36,450 --> 00:02:36,720
Okay.

39
00:02:36,720 --> 00:02:41,880
So just a little bit of preparation for the order items before we actually submit to the database.

40
00:02:41,910 --> 00:02:46,080
Now, this will create the order and just put it in this variable.

41
00:02:46,080 --> 00:02:47,850
It's not going to get saved yet.

42
00:02:47,850 --> 00:02:49,650
So we want to do that.

43
00:02:49,690 --> 00:02:51,600
Actually, it already got added here.

44
00:02:51,900 --> 00:03:00,600
So yeah, we want to just await on order dot save, put the the order into this variable and then respond

45
00:03:00,600 --> 00:03:04,770
with a 201 and pass in that new created order.

46
00:03:05,330 --> 00:03:07,340
And that should do it for that route.

47
00:03:07,520 --> 00:03:10,760
Now let's do a few of these other ones.

48
00:03:10,760 --> 00:03:17,390
So to get my orders, that's actually going to be pretty easy so we can get rid of that and we'll say

49
00:03:17,390 --> 00:03:24,830
const orders and we're going to set that to await and then order find.

50
00:03:24,830 --> 00:03:29,690
And we just want to match the user to whatever user is logged in, okay?

51
00:03:29,690 --> 00:03:31,250
And then we're just going to respond.

52
00:03:31,250 --> 00:03:40,220
So res dot status and respond with a 200 and then dot Jason and pass in the orders.

53
00:03:42,240 --> 00:03:43,020
Okay.

54
00:03:43,020 --> 00:03:47,300
And then the other one I want to do is get order by ID.

55
00:03:47,310 --> 00:03:54,510
We're not going to do these update ones just yet, so we're not going to do the admin stuff, but for

56
00:03:54,510 --> 00:03:57,300
getting the order by ID, let's do that.

57
00:03:57,600 --> 00:04:08,340
So we'll say const order and we want to await on order find by ID pass in the id which is in the URL.

58
00:04:08,340 --> 00:04:10,950
So this here we can get with that.

59
00:04:11,160 --> 00:04:19,019
And then I also want to add the user, the user name and email to the order and that's not stored in

60
00:04:19,019 --> 00:04:20,160
the order collection.

61
00:04:20,160 --> 00:04:28,680
So what we can do is we can say dot populate and we're going to say we want to populate from the user

62
00:04:28,680 --> 00:04:34,650
collection and we want to populate the name and email fields.

63
00:04:35,100 --> 00:04:35,700
Okay.

64
00:04:35,700 --> 00:04:40,440
So again, this is going to find the order by ID, everything that's in the orders collection for that

65
00:04:40,440 --> 00:04:41,740
particular order.

66
00:04:41,740 --> 00:04:46,000
And then it's also going to populate the user's name and email.

67
00:04:46,580 --> 00:04:48,530
So then we'll check for the order.

68
00:04:48,530 --> 00:04:54,170
And if we have an order, then we're going to respond with that order.

69
00:04:54,200 --> 00:04:55,100
Else.

70
00:04:56,490 --> 00:04:57,590
Uh, else.

71
00:04:57,600 --> 00:05:00,810
Then let's set a status.

72
00:05:00,810 --> 00:05:06,240
So raise dot status and we're going to do a 404.

73
00:05:07,440 --> 00:05:07,770
Okay.

74
00:05:07,770 --> 00:05:11,070
And then we'll throw an error of order not found.

75
00:05:11,670 --> 00:05:12,510
And that's it.

76
00:05:13,410 --> 00:05:15,300
So we'll go ahead and save that.

77
00:05:15,300 --> 00:05:22,620
And now what we'll do is on the front end, we'll consume these these end points and we'll be able to

78
00:05:22,620 --> 00:05:23,790
create a new order.

