1 00:00:01,560 --> 00:00:06,360 We've got our data layer put together which means we can now go back over to our new rabbit handler 2 00:00:06,420 --> 00:00:10,570 the one we were working on just a little bit ago and start to put this implementation together. 3 00:00:10,570 --> 00:00:15,360 So inside of my new root handler at the very top I'm going to import those two models that we just created. 4 00:00:15,360 --> 00:00:24,860 So I'm going to get tickets from up one directory models ticket and order from up one directory at models 5 00:00:24,950 --> 00:00:26,710 order. 6 00:00:26,740 --> 00:00:32,080 Now we've got the ability to potentially fetch a ticket out of our database and potentially save a order 7 00:00:32,080 --> 00:00:33,070 as well. 8 00:00:34,640 --> 00:00:36,630 So now let's go down to our right handler. 9 00:00:36,630 --> 00:00:40,040 The first thing we're going to do inside of here is write out a couple of comments and really think 10 00:00:40,310 --> 00:00:43,010 about what logic we need to implement inside of here. 11 00:00:43,010 --> 00:00:48,330 So this is really going to require us to deeply understand what this application is all about. 12 00:00:48,350 --> 00:00:52,550 So as a quick reminder the whole idea here is that we want to build to create an order associate with 13 00:00:52,550 --> 00:00:55,700 a ticket that has already been created inside the database. 14 00:00:55,700 --> 00:00:59,780 And we also want to make sure that order will expire after 15 minutes. 15 00:00:59,810 --> 00:01:04,070 So we make sure need to make sure we set that expires at flag as well. 16 00:01:04,070 --> 00:01:08,570 So this mind let's write out a couple of different comments just to guide ourselves. 17 00:01:08,570 --> 00:01:12,610 The first step to do inside of here is find the ticket. 18 00:01:12,620 --> 00:01:20,960 The user is trying to order in the database so when we try to find the ticket that the user trying to 19 00:01:20,960 --> 00:01:21,560 order. 20 00:01:21,560 --> 00:01:26,120 If we fail to find it for some reason then potentially the ticket has already been purchased maybe the 21 00:01:26,120 --> 00:01:27,340 ticket has been deleted. 22 00:01:27,470 --> 00:01:32,600 Maybe the user for some reason crafted a malicious request to try to buy a ticket that doesn't exist 23 00:01:32,600 --> 00:01:34,110 for some crazy reason. 24 00:01:34,130 --> 00:01:38,390 So if we fail to find the ticket that the user is trying to purchase we're going to want to definitely 25 00:01:38,390 --> 00:01:42,570 return some kind of air immediately after that. 26 00:01:42,610 --> 00:01:51,880 We need to make sure that this ticket is not already reserved so the entire idea here with reserving 27 00:01:51,880 --> 00:01:56,080 a ticket for 15 minutes is that we're kind of planning around the idea of our application getting a 28 00:01:56,080 --> 00:02:00,410 lot of traffic and a lot of people trying to buy the same tickets at the same point in time. 29 00:02:00,430 --> 00:02:06,220 So in just one second of time in other words in the instant of time we can kind of imagine that we might 30 00:02:06,220 --> 00:02:13,110 get 10 different requests of a bunch of users all trying to reserve the exact same ticket so we need 31 00:02:13,110 --> 00:02:17,420 to somehow make sure that this ticket is not already been reserved by someone else. 32 00:02:17,550 --> 00:02:19,940 Just finding that ticket inside the database is not enough. 33 00:02:22,180 --> 00:02:29,990 Once we have done these two steps right here we're then going to calculate an expiration date for this 34 00:02:29,990 --> 00:02:35,010 order so as I mentioned we're going to assume that the order has 15 minutes to pay. 35 00:02:35,020 --> 00:02:41,020 After that the ticket will essentially go unlocked and the order will enter a canceled state that 15 36 00:02:41,020 --> 00:02:45,860 minutes is entirely arbitrary of course we can make it 15 seconds we can make it 15 days. 37 00:02:45,910 --> 00:02:47,010 It really doesn't matter. 38 00:02:47,020 --> 00:02:51,430 We're going to make it very easy to change variable but we do have to make sure that something time 39 00:02:51,430 --> 00:02:54,050 we calculate when that expiration date occurs. 40 00:02:54,130 --> 00:02:57,350 So we're gonna do that inside of here. 41 00:02:57,410 --> 00:03:02,090 Then after that assuming we get past all these checks we calculate that expiration date then we will 42 00:03:02,090 --> 00:03:10,210 finally go ahead and build the order and save it to the database and then immediately after that we 43 00:03:10,210 --> 00:03:14,520 definitely are going to want to tell the rest of our application that an order has just been created. 44 00:03:14,620 --> 00:03:17,650 We definitely need to think through what other services need to know. 45 00:03:17,650 --> 00:03:18,840 That order has been created. 46 00:03:18,850 --> 00:03:22,990 So what do a little bit of research or thinking on that front but chance all right now we can definitely 47 00:03:22,990 --> 00:03:23,740 assume that. 48 00:03:23,740 --> 00:03:26,800 Well we need to tell the rest of our application that order was created. 49 00:03:26,800 --> 00:03:34,540 We'll say publish an event saying that an order was created and remember publishing and events or creating 50 00:03:34,570 --> 00:03:35,390 a new event. 51 00:03:35,440 --> 00:03:39,200 We don't have any infrastructure right now to say that a order was created. 52 00:03:39,220 --> 00:03:43,780 The only kind of events we have are list out inside of our common module specifically inside of that 53 00:03:43,780 --> 00:03:45,160 events directory. 54 00:03:45,160 --> 00:03:49,390 So right now the only two events we have our ticket created and to get updated. 55 00:03:49,450 --> 00:03:53,110 So for this last step right here we will definitely once again have to go back over to our common module 56 00:03:53,140 --> 00:03:59,440 and create these new types of events something and say hey the order was created and then after we create 57 00:03:59,440 --> 00:04:03,520 that event of course we'll also have to define a publisher inside of our orders project. 58 00:04:04,120 --> 00:04:07,150 So there really is a lot of stuff going on inside of here. 59 00:04:07,180 --> 00:04:08,280 Let's get started right away. 60 00:04:08,320 --> 00:04:10,190 So we're going to go through this first step right here. 61 00:04:10,210 --> 00:04:16,120 We're going to try to find the ticket that the user is trying to order inside of our database on a line 62 00:04:16,120 --> 00:04:16,740 right above it. 63 00:04:16,770 --> 00:04:20,710 I'm going to take a look at the incoming request body and I'm going to pull off the I.D. the ticket 64 00:04:20,710 --> 00:04:22,590 that the user is trying to order. 65 00:04:22,870 --> 00:04:26,890 So I'll do a ticket I.D. I mean from wrecked up body 66 00:04:31,020 --> 00:04:33,960 so let's go ahead and then try to find that ticket inside the database. 67 00:04:33,960 --> 00:04:37,480 This is going to be a very standard Mongo DV or Mongoose query. 68 00:04:37,750 --> 00:04:48,870 So do a ticket is a weight ticket find by I.D. and we'll pass in the I.D.. 69 00:04:48,970 --> 00:04:53,140 So now when we do this find my I.D. We're going to either get our ticket or not a ticket. 70 00:04:53,170 --> 00:04:57,650 So this might possibly be a value of null if we were not able to find an I.D. with the one that the 71 00:04:57,650 --> 00:04:58,940 user just provided. 72 00:04:58,940 --> 00:05:02,320 So we need to make sure that we handled a case in which no ticket was found. 73 00:05:02,330 --> 00:05:08,560 So say if there is not a ticket then let's throw a new not found error. 74 00:05:08,630 --> 00:05:14,080 Remember this is one of those custom errors we put together if we throw this air then our area handling 75 00:05:14,080 --> 00:05:18,900 middleware it will automatically capture it turn a response into a for a four and send it back to the 76 00:05:18,900 --> 00:05:23,190 user we do need to make sure that we import that type into our file however. 77 00:05:23,240 --> 00:05:36,030 Let's go back up to the top very quickly find our common module and import not found error. 78 00:05:36,040 --> 00:05:40,270 Now I don't want this video to run too long as well as we normally do. 79 00:05:40,330 --> 00:05:41,870 So let's now take a pause right here. 80 00:05:41,890 --> 00:05:45,070 We'll come back and start to implement everything else inside here in just a moment.