1 00:00:00,540 --> 00:00:05,070 Know that we are successfully building our users for some amount of money we need to follow up and make 2 00:00:05,070 --> 00:00:09,180 sure that we actually give them credit and say OK we accept that you paid us some money. 3 00:00:09,180 --> 00:00:09,990 Thanks for that. 4 00:00:09,990 --> 00:00:12,640 And now we're going to give you some number of credits. 5 00:00:12,750 --> 00:00:18,810 So in order to record some number of credits with each and every user We're going to add another property 6 00:00:18,810 --> 00:00:21,390 to our user model class. 7 00:00:21,390 --> 00:00:27,690 Remember our user model class currently has a google id property and nothing else assigned to it. 8 00:00:27,720 --> 00:00:32,550 We are going to add in another property to the model called credits. 9 00:00:32,620 --> 00:00:36,210 We'll make sure that we start off the credits as a default of 0. 10 00:00:36,210 --> 00:00:41,520 So by default nobody has any credits you have to actually pay us some amount of money to get some. 11 00:00:41,550 --> 00:00:46,970 And then after a user is successfully built we will add in five additional credits. 12 00:00:47,070 --> 00:00:48,550 So let's get to it. 13 00:00:49,110 --> 00:00:54,990 We'll first start off by finding our models directory inside of there is the user G-S file. 14 00:00:54,990 --> 00:00:59,850 We haven't touched this thing in a while but you'll recall that we created a new mongoose model inside 15 00:00:59,850 --> 00:01:00,690 of here. 16 00:01:00,750 --> 00:01:07,650 We made a new user schema where we specified that this user model would have a google id property that 17 00:01:07,710 --> 00:01:09,470 would start off as a string. 18 00:01:09,720 --> 00:01:13,900 So we're now going to add in this other property this credit's property. 19 00:01:14,130 --> 00:01:21,330 So we'll say credits and then we want to specify that the type of field that this is is going to be 20 00:01:21,330 --> 00:01:22,000 a number. 21 00:01:22,020 --> 00:01:27,000 Because of course you're going to have save 5 credits or four or three or two or zero or one or whatever 22 00:01:27,000 --> 00:01:27,790 it might be. 23 00:01:27,990 --> 00:01:32,610 And then the other thing that we have to do here is make sure that we somehow initialize or default 24 00:01:32,610 --> 00:01:38,310 the value of credits to specifically zero because again we want to make sure that everyone starts off 25 00:01:38,310 --> 00:01:39,460 with no credits. 26 00:01:39,480 --> 00:01:46,770 You have to pay us money to send out any surveys now to both tell this credit's property that it's going 27 00:01:46,770 --> 00:01:48,190 to be of type number. 28 00:01:48,300 --> 00:01:52,750 But also tell it that it needs to have a default value of zero. 29 00:01:52,770 --> 00:01:59,070 We're going to assign it an object to provide both those little pieces of configuration. 30 00:01:59,070 --> 00:02:05,430 So whenever you are defining a schema you can either just assign the type of field it is or if you want 31 00:02:05,430 --> 00:02:11,940 to also specify some other configuration you'll assign an object to it and then pass a couple of different 32 00:02:12,000 --> 00:02:13,940 options into the subject. 33 00:02:14,190 --> 00:02:19,000 So the first option that we want to specify is the type of field that this is. 34 00:02:19,220 --> 00:02:23,380 And so remember last time we had said that the Google idea is going to be a string. 35 00:02:23,490 --> 00:02:31,380 The credits is going to be a number like the next we need to default the value of credits to be zero. 36 00:02:31,520 --> 00:02:34,250 And to do so with say default of 0. 37 00:02:34,700 --> 00:02:39,350 So I think that both of these option names are here are pretty clear pretty straightforward. 38 00:02:39,400 --> 00:02:44,660 There's a whole variety of other option names you could pass to a property and you can check out the 39 00:02:44,660 --> 00:02:48,800 mongoose documentation to get a good sense of what those other options are. 40 00:02:48,860 --> 00:02:53,450 Some of them end up being quite useful depending on the actual application that you're building. 41 00:02:53,990 --> 00:02:54,360 OK. 42 00:02:54,380 --> 00:03:00,150 So now that our user model is aware that it has a credit's property that is in number and starts off 43 00:03:00,190 --> 00:03:01,430 as zero. 44 00:03:01,430 --> 00:03:04,030 We can now go back over to our building routes file. 45 00:03:04,070 --> 00:03:10,640 So you're building around us and we'll say that after we successfully applied a charge to the user's 46 00:03:10,640 --> 00:03:16,670 credit card right after that we're going to then take the user model or the modeled representing the 47 00:03:16,670 --> 00:03:18,760 person who just made this request. 48 00:03:19,010 --> 00:03:23,990 We will add five credits to them and then we'll send the user model back to the client. 49 00:03:24,020 --> 00:03:29,100 So we will essentially respond to the request with the newly updated user model. 50 00:03:29,480 --> 00:03:29,750 Okay. 51 00:03:29,780 --> 00:03:35,630 So first challenge we need to somehow get a reference to the current user model the person who just 52 00:03:35,630 --> 00:03:41,140 made this request or just attempted to pay some money into their account. 53 00:03:41,150 --> 00:03:46,640 You'll recall that whenever we are making use of passport and user has signed into our application we 54 00:03:46,640 --> 00:03:56,450 can access the current user model as rec dot user so request dot user this is set up automatically by 55 00:03:56,450 --> 00:03:57,260 passport. 56 00:03:57,350 --> 00:04:03,270 Remember we had wired up passport inside of our next guest file with a passport initialize and password 57 00:04:03,270 --> 00:04:05,310 that session calls right here. 58 00:04:06,000 --> 00:04:06,270 OK. 59 00:04:06,290 --> 00:04:08,520 So this is our user model right here. 60 00:04:08,540 --> 00:04:14,550 Now we're going to add five credits to their credits property or five units I guess to their credit 61 00:04:14,620 --> 00:04:15,480 property. 62 00:04:15,560 --> 00:04:17,540 We will then persist the user. 63 00:04:17,540 --> 00:04:20,070 So we will save them back to the database. 64 00:04:20,330 --> 00:04:26,060 And then after we get the user back or after we successfully commit that save We'll then send the update 65 00:04:26,060 --> 00:04:29,140 the user back to have made the request. 66 00:04:29,140 --> 00:04:35,300 So to update the number of credits that a user has with say rect-Al user of dot credits plus equals 67 00:04:35,300 --> 00:04:38,310 five that's pretty much it. 68 00:04:38,360 --> 00:04:40,510 We then have to save the user. 69 00:04:40,580 --> 00:04:43,300 So remember that will be user Dotts save. 70 00:04:43,310 --> 00:04:48,490 That's one of the methods or functions that we have on a user model to actually take the model and persist 71 00:04:48,490 --> 00:04:51,770 it to our database just modifying the user. 72 00:04:51,800 --> 00:04:52,180 Right. 73 00:04:52,180 --> 00:04:57,350 It doesn't actually save any changes to our database so we have to actually call Save on this. 74 00:04:57,390 --> 00:05:03,640 Make sure that these changes get persisted so we'll say Reyk user save. 75 00:05:03,800 --> 00:05:07,570 Remember that whenever we save a model it is an asynchronous request. 76 00:05:07,580 --> 00:05:09,540 It's something it takes some amount of time. 77 00:05:09,550 --> 00:05:15,500 So just like we did before with the Awake key word up here we'll put a wait in front of save. 78 00:05:16,010 --> 00:05:21,340 And then whenever the Save process is complete it will return the updated user model. 79 00:05:21,420 --> 00:05:23,950 So we'll say Consta user equals a wait. 80 00:05:23,990 --> 00:05:24,290 Right. 81 00:05:24,290 --> 00:05:26,280 These are not safe. 82 00:05:26,540 --> 00:05:28,480 Now at this point you might say again. 83 00:05:28,640 --> 00:05:35,660 So even if you already have the user model right here as like user why are we taking the result of save 84 00:05:36,020 --> 00:05:37,350 and assigning it to user. 85 00:05:37,460 --> 00:05:38,210 We'll just be clear. 86 00:05:38,210 --> 00:05:38,540 Yes. 87 00:05:38,540 --> 00:05:42,780 These are essentially kind of in theory they represent the same user. 88 00:05:42,800 --> 00:05:49,760 However whenever we save a user model by convention we usually from that point on make reference to 89 00:05:49,760 --> 00:05:54,440 the copy of the model or the copy of the user that we just got back from the database. 90 00:05:54,650 --> 00:06:01,020 So it could be that now at this point in time after making the save this record user copy of the user 91 00:06:01,020 --> 00:06:07,370 a copy of the model might be like old or stale it might have outdated information inside of it. 92 00:06:07,370 --> 00:06:12,260 So just to make sure that we are always using the most possible are the most up to date as possible 93 00:06:12,260 --> 00:06:13,830 model at any given time. 94 00:06:13,910 --> 00:06:19,700 We make use of the user model that got returned from the Save request but again at the end of the day 95 00:06:19,700 --> 00:06:21,990 these really represent the same user. 96 00:06:22,070 --> 00:06:25,860 They are however two completely separate objects in memory. 97 00:06:25,880 --> 00:06:31,280 So they are completely separate even though they do represent the same user just calling saver here 98 00:06:31,310 --> 00:06:36,840 does not intrinsically like somehow update all the properties on the user. 99 00:06:37,460 --> 00:06:43,130 OK so now that we've got the user back the last step is to make sure that we actually respond to the 100 00:06:43,130 --> 00:06:45,220 request with the updated user. 101 00:06:45,470 --> 00:06:51,050 So remember any song we want to respond to requests would just say redstart send using the response 102 00:06:51,050 --> 00:06:55,650 object right here and we send back the data that we want to communicate back to the browser. 103 00:06:55,670 --> 00:07:01,490 So at the very bottom has got send and we'll send back the user like so 104 00:07:04,230 --> 00:07:06,810 OK so that is pretty much it. 105 00:07:07,000 --> 00:07:09,110 I think that we will take a quick break. 106 00:07:09,130 --> 00:07:11,710 We'll come back in the next section we'll test this out. 107 00:07:11,770 --> 00:07:14,840 I will ask you to make sure that your server is running. 108 00:07:14,920 --> 00:07:16,070 Let's do that right now. 109 00:07:16,080 --> 00:07:17,730 I'm in sodomised server directory. 110 00:07:17,760 --> 00:07:20,800 I'm going to ensure that the server is running so I'll start that up right now. 111 00:07:20,800 --> 00:07:25,060 And by the time everything warms up we'll continue in the next section and test this out. 112 00:07:25,130 --> 00:07:26,860 So I'll see you in just a minute.