1 00:00:01,600 --> 00:00:05,660 We've got this nice query written out right here that will determine whether or not a given ticket is 2 00:00:05,660 --> 00:00:06,620 already reserved. 3 00:00:06,620 --> 00:00:10,520 And as I mentioned at the end of last video there's very likely going to be some other scenarios where 4 00:00:10,520 --> 00:00:14,060 we need to decide whether or not a given ticket has been reserved. 5 00:00:14,060 --> 00:00:16,970 So we're going to extract this logic into a separate file. 6 00:00:16,970 --> 00:00:20,880 In particular we're going to extract it into the ticket model file. 7 00:00:20,880 --> 00:00:27,780 It's going to go back over to models ticket to yes we're going to add a new method onto the ticket model. 8 00:00:27,800 --> 00:00:32,710 This is going to allow us to decide whether or not a ticket has actually been reserved. 9 00:00:32,720 --> 00:00:33,920 Let me show you how this is going to work. 10 00:00:34,400 --> 00:00:40,890 I'm going to go down to the interface of ticket doc right here and on the ticket talk interface. 11 00:00:40,890 --> 00:00:46,980 We're going to add in some new method that's going to exist on a individual ticket document and say 12 00:00:47,970 --> 00:00:51,390 that we're going to add in a new method called is reserved. 13 00:00:51,390 --> 00:00:55,740 If this thing ever gets called we're going to return a promise that is going to resolve with a boolean 14 00:00:57,310 --> 00:01:01,550 so once we implement this is reserved method I'm going to flip back over to our right handler really 15 00:01:01,550 --> 00:01:10,330 quickly once we implement that method we will be able to write out some code like is reserved is a wait 16 00:01:10,600 --> 00:01:12,850 ticket dot is reserved. 17 00:01:12,850 --> 00:01:13,570 That is the goal. 18 00:01:13,570 --> 00:01:15,960 That's what we're going to try to eventually add in. 19 00:01:15,970 --> 00:01:19,870 So we're going to build a ticket ticket called Method and that's going to return to us whether or not 20 00:01:19,870 --> 00:01:25,180 this has been already assigned to some other valid order inside of rap so inside of that is reserved 21 00:01:25,180 --> 00:01:26,530 method that we can write in just a second. 22 00:01:26,530 --> 00:01:30,850 We're going to essentially just run this exact query right here that's gonna make it a lot easier to 23 00:01:30,850 --> 00:01:35,230 figure out whether or not a ticket has been reserved and really shorten down the code we have inside 24 00:01:35,290 --> 00:01:36,960 of this root handler. 25 00:01:37,060 --> 00:01:38,440 So that's the idea. 26 00:01:38,440 --> 00:01:41,550 So let's go back over to the ticket model file. 27 00:01:41,710 --> 00:01:48,240 We're gonna go down towards the bottom and right after we define that build method on statics the statics 28 00:01:48,250 --> 00:01:52,370 object is how we add a new method directly to the ticket model itself. 29 00:01:52,390 --> 00:01:54,120 Remember this is the ticket model. 30 00:01:54,190 --> 00:01:58,870 If the object that allows us get to get access to the overall collection if you want to add a new method 31 00:01:58,930 --> 00:02:09,870 to a document then we're going to add in a method or a property to ticket schema methods that is reserved. 32 00:02:09,960 --> 00:02:13,800 We're gonna set the sequel to a keyword function supercritical here. 33 00:02:13,800 --> 00:02:16,980 Please make sure you write out a function keyword instead of an era. 34 00:02:16,980 --> 00:02:21,740 Function so by putting in ticket schema methods is reserved. 35 00:02:21,830 --> 00:02:26,040 We can now run what you saw just over here a moment ago. 36 00:02:26,210 --> 00:02:27,580 We can do something like this. 37 00:02:27,590 --> 00:02:32,960 This is how we add a method directly to the ticket document itself. 38 00:02:32,990 --> 00:02:36,070 So why did we add in a function keyword. 39 00:02:36,110 --> 00:02:41,780 Well Mongoose has you seen several times is kind of stuck in the ways of old so to speak in the old 40 00:02:41,780 --> 00:02:44,120 ways of writing javascript I suppose. 41 00:02:44,120 --> 00:02:49,670 So in order to actually get information about the ticket document that we're operating on when we call 42 00:02:49,670 --> 00:02:57,310 this method right here we're going to access this so inside this function this is equal to the ticket 43 00:02:58,830 --> 00:03:06,280 document that we just called is reserved on in the context of our new route handler. 44 00:03:06,310 --> 00:03:11,450 We're just looking at a moment ago when we write out something like this right here the value of this 45 00:03:11,480 --> 00:03:16,000 inside of that function is going to be equal to that ticket document that we just found inside the database. 46 00:03:16,010 --> 00:03:21,400 A moment ago the reason we have to use a function keyword inside of here is because if we use an arrow 47 00:03:21,400 --> 00:03:25,130 function it's going to mess around with the value of this inside of the function. 48 00:03:25,150 --> 00:03:28,190 So that's what we have to use a keyword function. 49 00:03:28,240 --> 00:03:34,210 We're going to also mark this function as being async so we can use the async await syntax than inside 50 00:03:34,210 --> 00:03:34,440 of here. 51 00:03:34,450 --> 00:03:38,050 We're gonna write out essentially the same query that we were looking at a moment ago inside of our 52 00:03:38,050 --> 00:03:39,000 root handler. 53 00:03:39,160 --> 00:03:44,830 Let's just go and copy paste thing to save a little bit of time don't find the query that we just wrote 54 00:03:46,180 --> 00:03:47,560 here it is right here. 55 00:03:47,880 --> 00:03:53,790 I'm going to cut that whole thing go back over to our ticket model file and then paste it inside of 56 00:03:53,800 --> 00:03:53,990 your 57 00:03:57,870 --> 00:03:59,430 it's now inside this function. 58 00:03:59,710 --> 00:04:04,740 The ticket document that we're trying to look for is going to be equal to this inside this function. 59 00:04:04,840 --> 00:04:09,020 So we're gonna replace ticket with this like so. 60 00:04:09,130 --> 00:04:13,650 We also need to make sure we import the order model and the order status you name at the top as well 61 00:04:15,130 --> 00:04:16,510 so go to the top. 62 00:04:16,570 --> 00:04:28,160 I'm going to import order from same directory order and now we could get order status from our common 63 00:04:28,160 --> 00:04:29,490 module inside of here. 64 00:04:29,570 --> 00:04:35,740 But there's another thing we could do very easily in the context of this orders service right here. 65 00:04:35,740 --> 00:04:39,400 There are several different files that are going to need to access the order status. 66 00:04:39,430 --> 00:04:45,220 It's a little bit confusing to have the definition of exactly what an order is coming from either the 67 00:04:45,250 --> 00:04:51,070 order bottle file and then in some cases our common module instead you'll make a lot more sense if we 68 00:04:51,070 --> 00:04:54,340 could do something like order status. 69 00:04:54,340 --> 00:04:58,240 Like so you know just make a little bit more sense because now the implication here is that we have 70 00:04:58,240 --> 00:05:02,990 one file that has a definition of everything order relayed inside of application. 71 00:05:03,100 --> 00:05:07,370 So rather than having the separate import same right here we're going to try to do this. 72 00:05:07,390 --> 00:05:09,790 So we're going to try to get order status in the order file. 73 00:05:10,150 --> 00:05:14,850 So in order to write this right here that means we have to go back over to our order model file. 74 00:05:14,990 --> 00:05:17,300 We already imported order status over here. 75 00:05:17,300 --> 00:05:22,990 Now we're just going to re export that thing like so. 76 00:05:23,040 --> 00:05:25,860 So now we go back over to the ticket model file that import is now working. 77 00:05:26,430 --> 00:05:33,000 So again the idea here is just to have one source for everything order related okay. 78 00:05:33,060 --> 00:05:34,830 Let's get back on track here. 79 00:05:34,830 --> 00:05:39,000 So now those errors to be gone so inside this is reserve function. 80 00:05:39,000 --> 00:05:44,220 We should now have our existing order that again that thing is going to be either some order document 81 00:05:44,490 --> 00:05:45,180 or null. 82 00:05:45,180 --> 00:05:50,870 If we do not actually find a value from is reserved we don't really care necessarily about the ordered 83 00:05:50,900 --> 00:05:55,370 that this ticket is associated with the name of is reserved kind of just implies that we're going to 84 00:05:55,370 --> 00:05:57,710 return a boolean value of either true. 85 00:05:57,710 --> 00:05:59,330 Yes this thing is reserved or false. 86 00:05:59,330 --> 00:06:03,740 No it is not reserved and as a matter of fact that's even what we wrote out inside of our method signature 87 00:06:03,740 --> 00:06:05,060 right fort as well. 88 00:06:05,060 --> 00:06:11,290 We said it the promise was going to reserve a similar result with a billion rather than just returning 89 00:06:11,290 --> 00:06:12,740 the existing order. 90 00:06:12,820 --> 00:06:20,340 We're going to return not not existing order that's essentially going to take existing order. 91 00:06:20,350 --> 00:06:26,110 If that thing is equal to no it will be flip to true by the first exclamation and then flip back to 92 00:06:26,410 --> 00:06:32,570 FALSE with the second one and then if existing order is defined we're gonna flip at first a false and 93 00:06:32,570 --> 00:06:34,070 then flip it to true. 94 00:06:34,130 --> 00:06:39,290 So we're just doing that to the side or seeming to return a boolean true Yes it is reserved or false. 95 00:06:39,290 --> 00:06:41,230 No it is not. 96 00:06:41,270 --> 00:06:46,780 So that should be it is going to save this file we can now go back over to our new root handler 97 00:06:49,920 --> 00:06:55,290 and now we can write out something right underneath that comment where we just had a really long query 98 00:06:55,290 --> 00:07:03,230 we could say something like is reserved is a weight ticket dot is reserved and then on the if statement 99 00:07:03,360 --> 00:07:06,470 will say if is reserved. 100 00:07:06,540 --> 00:07:11,030 Well I don't know about you but this is a lot easier to read than what we had before. 101 00:07:11,070 --> 00:07:13,910 It's not really as clear what it means to be reserved. 102 00:07:14,020 --> 00:07:16,450 So this comment being right here does it really make a lot of sense. 103 00:07:16,500 --> 00:07:19,030 We might want to move that comment over to the model file. 104 00:07:19,080 --> 00:07:19,710 I'm going to delete it. 105 00:07:19,710 --> 00:07:21,510 You can certainly move it over if you wish. 106 00:07:21,630 --> 00:07:24,950 But this right here a lot easier to understand what is going on. 107 00:07:25,080 --> 00:07:28,590 We've got a very well-known method that's going to tell us with a simple boolean. 108 00:07:28,600 --> 00:07:28,860 Yep. 109 00:07:28,890 --> 00:07:31,200 It's reserved or no it's not. 110 00:07:31,240 --> 00:07:35,380 So this is a definite good and prevent still a little bit to take care of inside of this handler. 111 00:07:35,490 --> 00:07:36,750 Let's continue in just a moment.