1 00:00:01,070 --> 00:00:03,610 We just learned a lot about MVC, 2 00:00:03,610 --> 00:00:06,447 and so lets now very quickly refactor our code 3 00:00:06,447 --> 00:00:09,763 in order to fit that architecture a bit better. 4 00:00:11,370 --> 00:00:15,150 And actually we already have our controller folder here, 5 00:00:15,150 --> 00:00:18,120 and the tour and user controllers in them, 6 00:00:18,120 --> 00:00:20,040 and we already have the routes 7 00:00:20,040 --> 00:00:21,550 here in the route folder. 8 00:00:21,550 --> 00:00:24,540 And so really what we need to do in this video 9 00:00:24,540 --> 00:00:26,200 is to create a model folder, 10 00:00:26,200 --> 00:00:29,490 and create a tour model in there. 11 00:00:29,490 --> 00:00:30,896 So, let's do that. 12 00:00:30,896 --> 00:00:33,590 (clicking) 13 00:00:33,590 --> 00:00:35,870 So, models and in there 14 00:00:38,660 --> 00:00:40,260 the tourModel.js 15 00:00:41,330 --> 00:00:42,733 Let's close out these guys, 16 00:00:44,240 --> 00:00:47,500 and so let's go ahead and copy, 17 00:00:47,500 --> 00:00:49,680 or cut actually, this schema 18 00:00:50,730 --> 00:00:52,743 and this entire model declaration here, 19 00:00:54,000 --> 00:00:56,293 and put it in the tour model. 20 00:00:58,000 --> 00:01:03,000 We will also want the Mongoose importing here. 21 00:01:05,210 --> 00:01:08,070 All right, and in fact, 22 00:01:08,070 --> 00:01:11,063 let's get completely rid of this code. 23 00:01:12,050 --> 00:01:14,290 All right, so this here was just for testing, 24 00:01:14,290 --> 00:01:16,420 and we don't need it anymore. 25 00:01:16,420 --> 00:01:18,610 So it has done its job, 26 00:01:18,610 --> 00:01:20,110 and so all we really want to do 27 00:01:20,110 --> 00:01:23,750 in this file here is to connect to the database, 28 00:01:23,750 --> 00:01:26,360 but everything that is about the models themselves, 29 00:01:26,360 --> 00:01:29,120 will always live inside of a file, 30 00:01:29,120 --> 00:01:30,893 inside of the models folder. 31 00:01:31,920 --> 00:01:34,640 All right, now in here eslint tells us 32 00:01:34,640 --> 00:01:36,452 that this variable here has not been used, 33 00:01:36,452 --> 00:01:38,400 but actually we want to use it 34 00:01:38,400 --> 00:01:41,823 because we want to export the model from this file. 35 00:01:44,700 --> 00:01:47,150 So module.exports, and this should actually 36 00:01:47,150 --> 00:01:50,530 be the only thing we export from this file, 37 00:01:50,530 --> 00:01:54,590 and so we use the kind of default export with module.exports 38 00:01:56,080 --> 00:01:59,340 Okay, and so this is our very simple tour model 39 00:01:59,340 --> 00:02:01,300 that we created in our last lecture, 40 00:02:01,300 --> 00:02:04,290 and now here we put it in it's own standalone file, 41 00:02:04,290 --> 00:02:07,100 and then finally exported it from here. 42 00:02:07,100 --> 00:02:10,670 Okay, now where do we actually need this tour. 43 00:02:10,670 --> 00:02:13,290 So, in other words, where are we actually going to create 44 00:02:13,290 --> 00:02:16,850 and query, and delete and update tours. 45 00:02:16,850 --> 00:02:20,643 Well we're going to do so in the tourController, right? 46 00:02:22,320 --> 00:02:23,350 So right here, 47 00:02:23,350 --> 00:02:27,233 and so let's go ahead and actually import the model in here. 48 00:02:28,200 --> 00:02:30,436 So right here at the top, 49 00:02:30,436 --> 00:02:32,540 (clicking) 50 00:02:32,540 --> 00:02:34,583 and I'm giving it the exact same name. 51 00:02:35,920 --> 00:02:37,720 So still tour, 52 00:02:37,720 --> 00:02:42,260 and now I want to require, so the current folder, 53 00:02:42,260 --> 00:02:44,250 then up one folder, 54 00:02:44,250 --> 00:02:47,163 and then down into the models folder, 55 00:02:48,110 --> 00:02:50,623 and in there, tour model. 56 00:02:53,210 --> 00:02:55,220 Okay, now of course it's telling me 57 00:02:55,220 --> 00:02:56,960 that I'm not using this variable, 58 00:02:56,960 --> 00:02:59,020 but don't worry about that for now. 59 00:02:59,020 --> 00:03:01,020 So, next up, I want to get rid 60 00:03:01,020 --> 00:03:04,790 of this place here where we actually import 61 00:03:04,790 --> 00:03:06,580 the data as a json file. 62 00:03:06,580 --> 00:03:08,660 So, of course, we no longer need this. 63 00:03:08,660 --> 00:03:10,960 This here was just for testing purposes. 64 00:03:10,960 --> 00:03:13,080 So let's get rid of it. 65 00:03:13,080 --> 00:03:14,770 You can also comment it out 66 00:03:14,770 --> 00:03:18,540 if you want to leave it maybe as a reference for yourself. 67 00:03:18,540 --> 00:03:20,903 Then just go ahead and comment it out. 68 00:03:22,770 --> 00:03:24,210 Then down here we get this error 69 00:03:24,210 --> 00:03:26,480 because the variable that we just deleted 70 00:03:26,480 --> 00:03:29,280 is no longer defined, 71 00:03:29,280 --> 00:03:32,410 and so let me comment out this piece of code here 72 00:03:32,410 --> 00:03:35,363 because, again, we're gonna need something similar to this. 73 00:03:37,230 --> 00:03:39,900 Okay, get tour, let's comment all 74 00:03:39,900 --> 00:03:44,330 of this here out as well just so that we don't get 75 00:03:44,330 --> 00:03:48,060 any errors as soon as we save this file. 76 00:03:48,060 --> 00:03:50,240 Then here, actually, we can get rid, 77 00:03:50,240 --> 00:03:52,400 well kind of, of all of this. 78 00:03:52,400 --> 00:03:55,210 So we're no longer gonna use any of this. 79 00:03:55,210 --> 00:03:57,010 So let me just keep this piece here. 80 00:03:58,080 --> 00:04:00,130 So I'm gonna copy it, 81 00:04:00,130 --> 00:04:01,780 delete everything else from this, 82 00:04:03,640 --> 00:04:05,240 and put it back here. 83 00:04:05,240 --> 00:04:06,610 We still got this error, 84 00:04:06,610 --> 00:04:09,693 and so I'm commenting out this piece of code. 85 00:04:10,690 --> 00:04:12,950 All right, and here we don't get any errors. 86 00:04:12,950 --> 00:04:15,693 So let's just leave it like this for now, okay? 87 00:04:15,693 --> 00:04:18,210 So what I just did here was to basically 88 00:04:18,210 --> 00:04:20,800 clean the code that we wrote before 89 00:04:20,800 --> 00:04:23,040 in order to no longer depend on the data 90 00:04:23,040 --> 00:04:24,853 that we had in the json file. 91 00:04:25,850 --> 00:04:28,990 All right, so now we should have no more errors, 92 00:04:28,990 --> 00:04:32,190 or actually we should in this checkID function, 93 00:04:32,190 --> 00:04:33,930 and this checkID function, we, 94 00:04:33,930 --> 00:04:37,070 in fact, no longer will need it, okay? 95 00:04:37,070 --> 00:04:39,460 Because from now on we're gonna start working 96 00:04:39,460 --> 00:04:42,930 with the IDs that are coming from MongoDB, 97 00:04:42,930 --> 00:04:45,540 and Mongo itself will give us an error 98 00:04:45,540 --> 00:04:48,046 if we use an invalid ID, 99 00:04:48,046 --> 00:04:50,800 and so this function that we have here 100 00:04:50,800 --> 00:04:53,260 was actually very useful for showing you 101 00:04:53,260 --> 00:04:54,940 how middleware actually works, 102 00:04:54,940 --> 00:04:57,930 by giving you this very practical example here, 103 00:04:57,930 --> 00:04:59,090 and later in the course, people, 104 00:04:59,090 --> 00:05:00,830 of course, use more middleware, 105 00:05:00,830 --> 00:05:02,660 but this particular function here, 106 00:05:02,660 --> 00:05:04,450 we will no longer need it. 107 00:05:04,450 --> 00:05:06,234 So, let's get rid of it, 108 00:05:06,234 --> 00:05:09,870 and also of this file system import, 109 00:05:09,870 --> 00:05:13,170 again, because we're no longer using the file here. 110 00:05:13,170 --> 00:05:14,450 So we still got some error here. 111 00:05:14,450 --> 00:05:16,120 Let's see what's happening, 112 00:05:16,120 --> 00:05:19,140 and it looks like it is in the 113 00:05:19,140 --> 00:05:21,210 in the tour routes, okay? 114 00:05:21,210 --> 00:05:22,250 And so actually that was the one 115 00:05:22,250 --> 00:05:23,853 that I was gonna change next. 116 00:05:25,420 --> 00:05:26,710 So the problem here is that 117 00:05:26,710 --> 00:05:30,780 this tourController.checkID, of course no longer 118 00:05:30,780 --> 00:05:33,077 exists because we just deleted it, 119 00:05:33,077 --> 00:05:35,780 and so let's get rid of this as well, 120 00:05:35,780 --> 00:05:38,930 or actually let me just comment it out, okay? 121 00:05:38,930 --> 00:05:41,000 So you keep in mind that you can use 122 00:05:41,000 --> 00:05:43,580 this .param function here to define 123 00:05:43,580 --> 00:05:47,190 parameter middleware in your own applications, okay? 124 00:05:47,190 --> 00:05:48,820 So I don't want you to forget that, 125 00:05:48,820 --> 00:05:51,060 and so just leave it here in your code 126 00:05:51,060 --> 00:05:53,120 so that maybe later when you come back to it 127 00:05:53,120 --> 00:05:56,270 you still know that it's there, okay? 128 00:05:56,270 --> 00:05:57,520 So now I saved it, 129 00:05:57,520 --> 00:05:59,120 and so we're back on track here. 130 00:05:59,120 --> 00:06:01,860 So everything is back to working, 131 00:06:01,860 --> 00:06:04,800 and our code is now sufficiently well refactored 132 00:06:04,800 --> 00:06:06,880 so that in the next video we can actually start 133 00:06:06,880 --> 00:06:10,343 implementing the correct functions in our API. 134 00:06:11,440 --> 00:06:14,450 So basically, getting all tours, 135 00:06:14,450 --> 00:06:18,120 creating tours, deleting tours, and updating tours. 136 00:06:18,120 --> 00:06:19,420 So, one by one, we will start 137 00:06:19,420 --> 00:06:21,380 implementing these controller functions, 138 00:06:21,380 --> 00:06:24,040 or handler functions if you prefer that, 139 00:06:24,040 --> 00:06:25,743 starting in the next video.