1 00:00:01,130 --> 00:00:04,270 Let's continue creating some factory functions, 2 00:00:04,270 --> 00:00:07,793 this time for updating and for creating some resources. 3 00:00:09,280 --> 00:00:11,750 And so, now that we already know how it works, 4 00:00:11,750 --> 00:00:14,640 it's really simple to just continue doing the same 5 00:00:14,640 --> 00:00:15,543 for updating. 6 00:00:16,660 --> 00:00:18,413 So exports.updateOne, 7 00:00:21,200 --> 00:00:24,520 and let's actually go ahead and copy the code again 8 00:00:24,520 --> 00:00:25,993 from the tour controller. 9 00:00:27,330 --> 00:00:28,832 Okay. 10 00:00:28,832 --> 00:00:30,740 And we have a ton of files now open 11 00:00:30,740 --> 00:00:33,022 because we're actually changing all of them 12 00:00:33,022 --> 00:00:34,573 kind of at the same time. 13 00:00:36,860 --> 00:00:40,253 So let's copy this, 14 00:00:42,110 --> 00:00:43,850 and paste it here. 15 00:00:43,850 --> 00:00:46,953 And so, what we're interested in is, again, this part. 16 00:00:49,130 --> 00:00:50,530 Okay. 17 00:00:50,530 --> 00:00:53,950 And now, of course, we need to create that factory function 18 00:00:53,950 --> 00:00:56,130 because we don't want to return this. 19 00:00:56,130 --> 00:00:58,120 Instead, we want to return a function, 20 00:00:58,120 --> 00:01:01,210 which is then, in turn, going to return this. 21 00:01:01,210 --> 00:01:02,043 Right? 22 00:01:04,000 --> 00:01:06,240 So that's very simple. 23 00:01:06,240 --> 00:01:09,480 We use a simple arrow function where we pass in the model 24 00:01:09,480 --> 00:01:13,300 and then implicitly return whatever comes after the arrow. 25 00:01:13,300 --> 00:01:14,773 So, all of this. 26 00:01:15,830 --> 00:01:16,663 All right. 27 00:01:17,690 --> 00:01:21,010 So just like before, let's replace the specific model 28 00:01:21,010 --> 00:01:22,460 with a general one, 29 00:01:22,460 --> 00:01:26,453 and then replace all of this here with document. 30 00:01:27,500 --> 00:01:31,030 Or actually, with doc, as we did it before. 31 00:01:31,030 --> 00:01:32,443 And here it's no document, 32 00:01:33,640 --> 00:01:36,640 and then down here, where we actually send the data, 33 00:01:36,640 --> 00:01:39,730 it would be nice to actually give it the property name 34 00:01:39,730 --> 00:01:41,370 of the data that we're sending. 35 00:01:41,370 --> 00:01:46,370 For example, reviews, and then the document, or tours. 36 00:01:48,340 --> 00:01:49,173 Okay. 37 00:01:49,173 --> 00:01:52,600 But that's a bit too much work to implement it right now. 38 00:01:52,600 --> 00:01:55,630 And so, I'm simply going to leave it like this 39 00:01:55,630 --> 00:01:57,670 where I sent, again, data. 40 00:01:57,670 --> 00:01:58,660 All right. 41 00:01:58,660 --> 00:02:00,010 So went went our data here, 42 00:02:00,010 --> 00:02:02,600 and then the envelope in there is, again, 43 00:02:02,600 --> 00:02:03,923 going to be called data. 44 00:02:05,610 --> 00:02:06,443 All right. 45 00:02:06,443 --> 00:02:08,650 And so, this should work now 46 00:02:08,650 --> 00:02:12,240 because everything else is really just the same. 47 00:02:12,240 --> 00:02:15,936 And so, we can go ahead, get rid of all of this, 48 00:02:15,936 --> 00:02:17,320 and simply say 49 00:02:18,420 --> 00:02:20,137 factory.updateOne, 50 00:02:22,260 --> 00:02:24,993 pass in the Tour model, and call it a day. 51 00:02:26,910 --> 00:02:28,060 All right? 52 00:02:28,060 --> 00:02:29,323 So, really simple. 53 00:02:30,530 --> 00:02:32,643 Next, the same thing for the users. 54 00:02:33,938 --> 00:02:36,920 So again, we have updateUser. 55 00:02:36,920 --> 00:02:39,190 This route is not yet implemented. 56 00:02:39,190 --> 00:02:42,790 And so, let's simply replace all of this here 57 00:02:42,790 --> 00:02:45,237 with our factory function updateOne. 58 00:02:46,800 --> 00:02:47,850 All right. 59 00:02:47,850 --> 00:02:51,030 And so, just like before, this updateUser function 60 00:02:51,030 --> 00:02:53,600 is only for administrators, okay? 61 00:02:53,600 --> 00:02:56,640 And only for updating data that is not the password, 62 00:02:56,640 --> 00:03:01,640 because remember, whenever we use, where is that, 63 00:03:01,710 --> 00:03:05,240 so whenever we use findById and update, 64 00:03:05,240 --> 00:03:07,890 all the safe middleware is not run. 65 00:03:07,890 --> 00:03:11,160 So remember how we talked about that a couple of times. 66 00:03:11,160 --> 00:03:12,990 And so, again, it's really important 67 00:03:12,990 --> 00:03:17,498 that we do not attempt to change passwords here 68 00:03:17,498 --> 00:03:19,710 using this updateUser. 69 00:03:19,710 --> 00:03:20,810 Okay? 70 00:03:20,810 --> 00:03:22,360 Let's actually write that here. 71 00:03:27,640 --> 00:03:28,473 With this. 72 00:03:30,070 --> 00:03:30,903 Great. 73 00:03:30,903 --> 00:03:32,460 And now, finally, also, 74 00:03:32,460 --> 00:03:34,593 let's put it in the review controller. 75 00:03:36,700 --> 00:03:37,533 All right. 76 00:03:38,420 --> 00:03:40,507 So exports.updateReview, 77 00:03:50,830 --> 00:03:53,260 and then our Review. 78 00:03:53,260 --> 00:03:55,060 So that one is very simple. 79 00:03:55,060 --> 00:03:58,573 Let's now go ahead and add these routes to Postman, as well. 80 00:04:00,210 --> 00:04:01,703 So we have Delete Review, 81 00:04:02,840 --> 00:04:07,840 and now let's simply add it here as PATCH. 82 00:04:08,020 --> 00:04:10,830 And the thing we also need to still implement the route 83 00:04:10,830 --> 00:04:12,280 in the review router. 84 00:04:12,280 --> 00:04:14,130 So let's do that as well. 85 00:04:14,130 --> 00:04:19,130 But first, I'm going to save it here in reviews. 86 00:04:22,730 --> 00:04:25,443 So Update Review. 87 00:04:28,090 --> 00:04:31,263 So here, in the review routes, 88 00:04:32,270 --> 00:04:35,223 we should add the patch method, 89 00:04:37,580 --> 00:04:39,203 and then reviewController.updateReview. 90 00:04:44,610 --> 00:04:45,443 All right. 91 00:04:47,950 --> 00:04:51,410 So let's take a look at one of these reviews here, 92 00:04:51,410 --> 00:04:55,913 let's say this one, and update the rating to four. 93 00:04:57,270 --> 00:04:58,103 Okay. 94 00:05:00,470 --> 00:05:01,783 So the body, 95 00:05:09,060 --> 00:05:10,700 set it to four. 96 00:05:10,700 --> 00:05:12,093 And send it now. 97 00:05:13,760 --> 00:05:18,530 Okay, and as we can see, we get our new review back 98 00:05:18,530 --> 00:05:22,070 with the rating updated, just as expected. 99 00:05:22,070 --> 00:05:24,950 And again, we're going to take care of authorization 100 00:05:24,950 --> 00:05:26,300 a bit later in the section. 101 00:05:27,282 --> 00:05:29,963 For now, I just wanted to make this work. 102 00:05:32,280 --> 00:05:37,280 So users, let's now also update users. 103 00:05:38,100 --> 00:05:38,933 Okay? 104 00:05:42,640 --> 00:05:45,963 So GET, and PATCH. 105 00:05:47,930 --> 00:05:49,583 So let's Get All Users here. 106 00:05:53,970 --> 00:05:55,383 Let's say this one. 107 00:06:04,991 --> 00:06:06,413 And let's change the name 108 00:06:09,600 --> 00:06:10,640 to Administrator, 109 00:06:10,640 --> 00:06:14,443 which before was simply admin. 110 00:06:15,680 --> 00:06:16,513 All right? 111 00:06:18,420 --> 00:06:21,960 And so, this one worked as well. 112 00:06:21,960 --> 00:06:26,010 Okay, so again, I'm saving all of this here in my collection 113 00:06:26,010 --> 00:06:27,320 because then, a bit later, 114 00:06:27,320 --> 00:06:29,880 I can actually go ahead and create documentation 115 00:06:29,880 --> 00:06:32,673 from all of this collection data that we have here. 116 00:06:35,330 --> 00:06:36,863 So Update User, 117 00:06:39,370 --> 00:06:42,740 put that one here, and this one here. 118 00:06:42,740 --> 00:06:43,710 Perfect. 119 00:06:43,710 --> 00:06:46,800 So updating users is now working 120 00:06:46,800 --> 00:06:50,350 for all of our three resources using the factory function. 121 00:06:50,350 --> 00:06:52,980 And so, let's go ahead and add the next one, 122 00:06:52,980 --> 00:06:54,767 which is gonna be createOne. 123 00:06:57,979 --> 00:06:59,770 So exports, createOne. 124 00:07:03,290 --> 00:07:04,983 And again, we pass in the model. 125 00:07:05,840 --> 00:07:07,820 And from there, we create a new function. 126 00:07:07,820 --> 00:07:09,280 And that may, once again, 127 00:07:09,280 --> 00:07:11,103 actually copy it from the Tour here. 128 00:07:14,880 --> 00:07:15,713 All right. 129 00:07:17,420 --> 00:07:21,373 And so, what we need here is only this part. 130 00:07:23,580 --> 00:07:27,400 So model, and document. 131 00:07:27,400 --> 00:07:30,897 And down here, we again call it data. 132 00:07:30,897 --> 00:07:32,450 All right. 133 00:07:32,450 --> 00:07:34,890 And so, that's actually it. 134 00:07:34,890 --> 00:07:36,620 That is our factory function. 135 00:07:36,620 --> 00:07:40,253 And so, again, let's come here and replace it. 136 00:07:41,830 --> 00:07:44,377 So factory.createOne, Tour. 137 00:07:48,860 --> 00:07:51,910 So you see that our code in the controllers 138 00:07:51,910 --> 00:07:55,040 is now getting really clean, right? 139 00:07:55,040 --> 00:07:57,970 But actually, we're not gonna be able to replace 140 00:07:57,970 --> 00:08:01,710 all of the functions that we have here with factories, 141 00:08:01,710 --> 00:08:03,670 because otherwise, we wouldn't even need 142 00:08:03,670 --> 00:08:05,480 this controller file, right? 143 00:08:05,480 --> 00:08:08,750 We could then simply create these handlers on the fly, 144 00:08:08,750 --> 00:08:11,270 basically, in the router file, right? 145 00:08:11,270 --> 00:08:13,947 And so, we could basically skip this entire step 146 00:08:13,947 --> 00:08:16,320 of the controller file. 147 00:08:16,320 --> 00:08:17,600 But again, we still need it 148 00:08:17,600 --> 00:08:19,730 because we have some other stuff in there. 149 00:08:19,730 --> 00:08:23,080 For example, these methods here 150 00:08:23,080 --> 00:08:27,418 for getting the tour statistics, or this monthly plan, 151 00:08:27,418 --> 00:08:29,560 and yeah. 152 00:08:29,560 --> 00:08:33,750 So it makes sense to actually keep this file, okay? 153 00:08:33,750 --> 00:08:35,130 So next up, for the Users, 154 00:08:35,130 --> 00:08:37,930 we actually do not need the createOne 155 00:08:37,930 --> 00:08:39,460 because, for creating new users, 156 00:08:39,460 --> 00:08:41,630 we already have the signUp function, 157 00:08:41,630 --> 00:08:44,290 and we cannot really replace that with a factory 158 00:08:44,290 --> 00:08:47,240 because it really is different from this generic one. 159 00:08:47,240 --> 00:08:48,073 Okay? 160 00:08:49,150 --> 00:08:54,140 But we can do it in the review controller, okay? 161 00:08:54,140 --> 00:08:58,230 So basically, we want to replace this createReview. 162 00:08:58,230 --> 00:08:59,990 Now you see that also, in this one, 163 00:08:59,990 --> 00:09:01,840 we have some additional step. 164 00:09:01,840 --> 00:09:04,170 So we have this code that is also not 165 00:09:04,170 --> 00:09:07,330 in our generic createOne function. 166 00:09:07,330 --> 00:09:09,165 And so, how can we fix that? 167 00:09:09,165 --> 00:09:11,800 Well, we can actually create a middleware 168 00:09:11,800 --> 00:09:15,610 that is going to run before the createReview, okay? 169 00:09:15,610 --> 00:09:19,170 And so, that actually also makes it a bit more decoupled. 170 00:09:19,170 --> 00:09:22,610 So basically, this code here is then gonna be 171 00:09:22,610 --> 00:09:23,960 in its own function, 172 00:09:23,960 --> 00:09:26,430 and really decoupled from createReview, 173 00:09:26,430 --> 00:09:28,550 because actually, it doesn't have much to do 174 00:09:28,550 --> 00:09:30,400 with creating the review. 175 00:09:30,400 --> 00:09:32,630 It simply sets some data, right? 176 00:09:32,630 --> 00:09:37,627 And so, let's actually call this set tour and user IDs. 177 00:09:41,720 --> 00:09:44,173 So request, ah, the response is missing. 178 00:09:47,200 --> 00:09:48,504 Okay? 179 00:09:48,504 --> 00:09:50,110 And all we're gonna do here 180 00:09:50,110 --> 00:09:53,730 is to basically set these IDs on the body 181 00:09:53,730 --> 00:09:56,100 and then move straight to the next middleware, 182 00:09:56,100 --> 00:09:58,660 where then the review is actually created. 183 00:09:58,660 --> 00:09:59,650 Alright? 184 00:09:59,650 --> 00:10:01,350 So before we forget it, 185 00:10:01,350 --> 00:10:04,430 let's then actually add this middleware. 186 00:10:04,430 --> 00:10:08,993 So in the review routes, it's right here in post. 187 00:10:10,780 --> 00:10:13,467 So reviewController, setTourUserIds. 188 00:10:15,700 --> 00:10:16,533 Okay? 189 00:10:16,533 --> 00:10:20,073 So yet another middleware function in this stack. 190 00:10:22,020 --> 00:10:22,853 Okay. 191 00:10:22,853 --> 00:10:26,620 Anyway, let's then get rid of what we have here 192 00:10:26,620 --> 00:10:28,610 because this is easily replaceable 193 00:10:28,610 --> 00:10:32,300 with our factory function, once more. 194 00:10:32,300 --> 00:10:37,300 So createOne, then the Review, and that's it. 195 00:10:39,660 --> 00:10:40,493 Okay. 196 00:10:41,540 --> 00:10:43,483 And just to test if this still works, 197 00:10:44,740 --> 00:10:47,883 let's simply go ahead and create a new review on Tour. 198 00:10:49,656 --> 00:10:50,489 Okay? 199 00:10:50,489 --> 00:10:52,463 And for that, we need the tour IDs, 200 00:10:54,120 --> 00:10:55,903 so let's also say Get All Tours. 201 00:10:58,310 --> 00:11:02,500 And now we get the user belonging is no longer existing, 202 00:11:02,500 --> 00:11:04,600 so let's simply go ahead and log in again. 203 00:11:06,020 --> 00:11:07,753 So get a new token here. 204 00:11:08,600 --> 00:11:10,933 And so, there we go, as an admin. 205 00:11:12,253 --> 00:11:16,490 So now, Get All Tours, here we go. 206 00:11:16,490 --> 00:11:19,423 Let's just choose one of them. 207 00:11:20,330 --> 00:11:21,980 So let's say, The Forest Hiker. 208 00:11:21,980 --> 00:11:25,720 I think this one already has some reviews, doesn't it? 209 00:11:25,720 --> 00:11:26,830 Oh no, it doesn't. 210 00:11:26,830 --> 00:11:29,343 So this one is a good one to try it out now. 211 00:11:30,539 --> 00:11:34,773 And so, Create New Review on Tour. 212 00:11:35,930 --> 00:11:39,110 Remember, we have to change the ID here. 213 00:11:39,110 --> 00:11:40,610 And now we can post this review 214 00:11:40,610 --> 00:11:43,520 only with the rating and the review. 215 00:11:43,520 --> 00:11:45,330 But I think it's actually not going to work 216 00:11:45,330 --> 00:11:48,260 because we're logged in as an administrator, 217 00:11:48,260 --> 00:11:51,410 and only regular users can post reviews. 218 00:11:51,410 --> 00:11:52,243 Okay? 219 00:11:54,550 --> 00:11:57,163 All right, so let's still do this here. 220 00:11:58,810 --> 00:11:59,963 Mind blowing. 221 00:12:01,270 --> 00:12:04,850 But we're gonna get this error that I was just saying. 222 00:12:04,850 --> 00:12:07,850 So you do not have permission to perform. 223 00:12:07,850 --> 00:12:09,620 And so, we need to log in again, 224 00:12:09,620 --> 00:12:12,670 this time as a different user. 225 00:12:12,670 --> 00:12:16,093 And let's take a look at which one we have, actually. 226 00:12:20,010 --> 00:12:21,110 Ah, okay. 227 00:12:21,110 --> 00:12:23,433 So let's just use test. 228 00:12:25,100 --> 00:12:28,563 Or maybe test was the one with the wrong password. 229 00:12:30,020 --> 00:12:30,963 But let's see. 230 00:12:33,477 --> 00:12:34,310 Ah, yeah. 231 00:12:34,310 --> 00:12:36,473 So let's experiment with testuser. 232 00:12:39,206 --> 00:12:40,290 And this one works. 233 00:12:40,290 --> 00:12:43,700 And so, actually, we can go ahead and delete this other one 234 00:12:43,700 --> 00:12:47,063 because I kind of forgot his password. 235 00:12:48,890 --> 00:12:50,137 Okay. 236 00:12:50,137 --> 00:12:51,060 And in this case, that's no problem. 237 00:12:51,060 --> 00:12:54,110 We can simply delete users as we like 238 00:12:54,110 --> 00:12:56,012 since we're only in development. 239 00:12:56,012 --> 00:12:57,200 Okay? 240 00:12:57,200 --> 00:13:00,203 Now we should be able to actually add this review. 241 00:13:02,930 --> 00:13:05,310 And indeed, here we go. 242 00:13:05,310 --> 00:13:06,143 Perfect. 243 00:13:06,143 --> 00:13:08,560 So that wraps up this lecture. 244 00:13:08,560 --> 00:13:10,200 All we need to do in the next one 245 00:13:10,200 --> 00:13:12,160 is then create the factory functions 246 00:13:12,160 --> 00:13:15,490 for getting one and getting all documents 247 00:13:15,490 --> 00:13:16,973 of a certain resource.