﻿1
00:00:01,180 --> 00:00:03,630
‫Let's now, really quickly, create the model

2
00:00:03,630 --> 00:00:06,700
‫for our bookings so that then, in the next video,

3
00:00:06,700 --> 00:00:09,873
‫we can actually start creating some real bookings.

4
00:00:11,590 --> 00:00:13,490
‫So, let's close up some of these

5
00:00:13,490 --> 00:00:18,073
‫files here, also these folders.

6
00:00:21,965 --> 00:00:26,070
‫Here is the models folder and so booking model.js.

7
00:00:30,170 --> 00:00:32,700
‫And so this is, of course, gonna be very similar

8
00:00:32,700 --> 00:00:34,860
‫to what we already did before,

9
00:00:34,860 --> 00:00:36,510
‫so I will do this pretty quickly.

10
00:00:40,290 --> 00:00:43,463
‫So as always, we need mongoose,

11
00:00:48,362 --> 00:00:52,043
‫and we create our booking schema mongoose.Schema,

12
00:00:57,560 --> 00:00:59,890
‫and now, remember how we said before

13
00:00:59,890 --> 00:01:01,850
‫that we were going to use paragraph referencing

14
00:01:01,850 --> 00:01:05,350
‫here on the bookings, so basically keeping a reference

15
00:01:05,350 --> 00:01:08,853
‫to the tour and also to the user who booked the tour.

16
00:01:10,030 --> 00:01:11,743
‫So that is very easy.

17
00:01:15,624 --> 00:01:17,660
‫We did this many times before.

18
00:01:17,660 --> 00:01:21,457
‫So mongoose.Schema.ObjectId, right?

19
00:01:24,700 --> 00:01:28,310
‫And then the reference, and this one

20
00:01:28,310 --> 00:01:31,670
‫is going to point to the tour model.

21
00:01:31,670 --> 00:01:34,423
‫Then let's also say these are required,

22
00:01:39,200 --> 00:01:43,960
‫Booking must belong to a Tour.

23
00:01:47,278 --> 00:01:49,200
‫Now let's just grab this

24
00:01:53,490 --> 00:01:58,060
‫and create the same thing for a user, okay?

25
00:01:58,060 --> 00:02:00,420
‫So that's what we just said we were gonna do

26
00:02:00,420 --> 00:02:02,360
‫keeping a reference of both the tour

27
00:02:02,360 --> 00:02:05,853
‫that's being purchased and the user who does the purchase.

28
00:02:07,260 --> 00:02:09,470
‫Then we also want to know the price

29
00:02:09,470 --> 00:02:11,840
‫at which the purchase actually happened,

30
00:02:11,840 --> 00:02:14,200
‫and so that's because the price might change

31
00:02:14,200 --> 00:02:17,250
‫in the future, and so then we would no longer know

32
00:02:17,250 --> 00:02:19,910
‫how much a certain user paid for a tour.

33
00:02:19,910 --> 00:02:23,160
‫And so it's important to also have this here

34
00:02:23,160 --> 00:02:26,763
‫in the booking, so this is going to be a number,

35
00:02:28,560 --> 00:02:30,533
‫and we also require this,

36
00:02:35,560 --> 00:02:38,443
‫Booking must have a price.

37
00:02:45,100 --> 00:02:48,667
‫Next up, let's also create our time stamp createdAt,

38
00:02:52,340 --> 00:02:53,343
‫which is a date,

39
00:02:54,300 --> 00:02:56,410
‫and then simply add default

40
00:02:56,410 --> 00:02:58,323
‫so that we don't have to do anything.

41
00:03:01,070 --> 00:03:04,483
‫Finally, I also want to create a paid property,

42
00:03:05,850 --> 00:03:09,810
‫and this one will be automatically set to true,

43
00:03:09,810 --> 00:03:12,050
‫but this is just in case that, for example,

44
00:03:12,050 --> 00:03:14,360
‫an administrator wants to create a booking

45
00:03:14,360 --> 00:03:16,250
‫outside of Stripe.

46
00:03:16,250 --> 00:03:19,550
‫So, for example, if a customer doesn't have a credit card

47
00:03:19,550 --> 00:03:22,940
‫and wants to pay directly, like in a store with cash,

48
00:03:22,940 --> 00:03:24,330
‫or something like that.

49
00:03:24,330 --> 00:03:27,050
‫And in this case, an administrator might then use

50
00:03:27,050 --> 00:03:29,740
‫our bookings API in order to basically

51
00:03:29,740 --> 00:03:33,430
‫manually create a tour, and so that might then be paid

52
00:03:33,430 --> 00:03:34,683
‫or not yet paid.

53
00:03:37,140 --> 00:03:39,163
‫So just a very small detail here,

54
00:03:41,180 --> 00:03:44,210
‫by the thought of that, and thought that it might be

55
00:03:44,210 --> 00:03:46,670
‫interesting to include that here as well.

56
00:03:46,670 --> 00:03:49,070
‫But of course, by default, it will be true

57
00:03:49,070 --> 00:03:50,920
‫so that we don't have to do anything.

58
00:03:52,790 --> 00:03:56,663
‫Now finally, let's then create a model out of this,

59
00:03:59,440 --> 00:04:03,793
‫so booking is equal to mongoose.model,

60
00:04:10,510 --> 00:04:13,444
‫and then, of course, our bookingSchema

61
00:04:13,444 --> 00:04:15,183
‫and then exporting the whole thing,

62
00:04:19,850 --> 00:04:21,770
‫is equal to Booking.

63
00:04:21,770 --> 00:04:23,680
‫Now what we also want to do here

64
00:04:23,680 --> 00:04:27,850
‫is to populate the tour and the user automatically

65
00:04:27,850 --> 00:04:30,890
‫whenever there is a query, all right?

66
00:04:30,890 --> 00:04:33,220
‫So remember how we used to do that

67
00:04:33,220 --> 00:04:34,723
‫using query middleware.

68
00:04:35,770 --> 00:04:39,210
‫So, right on the Schema.pre,

69
00:04:39,210 --> 00:04:42,470
‫and then here or as usual regular expression

70
00:04:43,990 --> 00:04:46,183
‫and whatever starts with find.

71
00:04:50,640 --> 00:04:53,363
‫So here next,

72
00:04:55,240 --> 00:05:00,240
‫and then this.populate,

73
00:05:00,970 --> 00:05:02,760
‫and let's do it actually automatically

74
00:05:02,760 --> 00:05:05,450
‫for both the user and the tours.

75
00:05:05,450 --> 00:05:07,810
‫And in this case that's absolutely no problem

76
00:05:07,810 --> 00:05:10,220
‫for performance, because there won't be

77
00:05:10,220 --> 00:05:14,170
‫many calls to the bookings, because only guides and admins

78
00:05:14,170 --> 00:05:17,600
‫will actually be allowed to do them, all right?

79
00:05:17,600 --> 00:05:19,590
‫So basically, for a guide to check

80
00:05:19,590 --> 00:05:21,920
‫was actually booked their tours.

81
00:05:21,920 --> 00:05:23,980
‫So that's one of the use cases that I see

82
00:05:23,980 --> 00:05:27,000
‫for this part of the API, okay?

83
00:05:27,000 --> 00:05:30,140
‫So, again, this query will not happen that often

84
00:05:30,140 --> 00:05:32,350
‫so we can easily populate all of this

85
00:05:32,350 --> 00:05:33,493
‫without any problem.

86
00:05:35,120 --> 00:05:37,850
‫So let's populate also the tour,

87
00:05:37,850 --> 00:05:40,833
‫but here I actually want to only select the tour name.

88
00:05:42,410 --> 00:05:47,410
‫So let's say path, tour, and select just the name, okay?

89
00:05:52,400 --> 00:05:56,070
‫Now here we have some kind of error, all right,

90
00:05:56,070 --> 00:05:59,413
‫so that should of course be mongoose not moongoose.

91
00:06:01,250 --> 00:06:05,420
‫So, one more time, ESLint saved us here.

92
00:06:05,420 --> 00:06:07,163
‫Oh, and actually there are more.

93
00:06:08,860 --> 00:06:12,260
‫Save it one more time, and now we're good.

94
00:06:12,260 --> 00:06:14,060
‫So that's actually it.

95
00:06:14,060 --> 00:06:15,540
‫This is our bookings model

96
00:06:16,429 --> 00:06:18,613
‫that we can now start using in the next video.

