WEBVTT

00:05.200 --> 00:06.790
Hi everyone welcome back.

00:07.040 --> 00:11.370
So in this video we're going to start to build the back API that we talked about.

00:11.450 --> 00:12.050
OK.

00:12.050 --> 00:19.160
So I made a new folder called Banky ions and deep games and I went into it I opened a new terminal and

00:19.160 --> 00:20.680
I'm in it inside of it.

00:20.710 --> 00:23.160
You're going to start building the pride from scratch.

00:23.390 --> 00:23.720
OK.

00:23.720 --> 00:25.470
So what what do we usually do.

00:25.480 --> 00:32.780
Well you start with a docker composed file case or what I do not touch daquiri compose dykey and then

00:32.780 --> 00:34.420
we're going to do two directories.

00:34.490 --> 00:38.870
Make directory web and make directory.

00:39.140 --> 00:51.820
Then again I and the doc files some Livesay touch web slash docker file them and to also touch divi

00:51.880 --> 00:53.920
slash knocker file.

00:54.340 --> 00:55.610
OK.

00:56.470 --> 00:57.160
All right.

00:57.160 --> 00:59.880
So inside webs are going to do is.

00:59.970 --> 01:07.060
I have a couple of things that my application which is apt up is somewhat a touch at duppy wide.

01:07.180 --> 01:09.290
I also have my requirements dot decks.

01:09.380 --> 01:13.780
So I'm doing touch requirements text.

01:14.390 --> 01:20.070
All right so I have my structure now I'm going to go out sites and then I'm going to do Suda at home.

01:20.230 --> 01:20.720
OK.

01:20.790 --> 01:23.420
So almost in the end folder

01:28.140 --> 01:30.240
or write so we have a folder here.

01:30.330 --> 01:33.670
So let's start with the compost file as usual.

01:33.960 --> 01:40.770
So we're going to write as you do version and then three and then we're going to have two services.

01:40.770 --> 01:45.180
We're going to have the Web and we're going to have D-B again.

01:45.270 --> 01:46.990
So for Abebe It's simple.

01:47.010 --> 01:51.320
We're going to build it from not slash TV.

01:51.720 --> 02:04.450
So the docker file and the for when I'm going to build it from dot slash web and I'm going to open it

02:04.450 --> 02:05.140
on board.

02:05.140 --> 02:13.630
So when I connect some boards and is going to be 5000 to 5000 right then I'm going to link it so links

02:14.480 --> 02:16.490
and I'm going to add D-B into that link.

02:16.500 --> 02:16.870
OK.

02:16.900 --> 02:21.230
So I want to link it into the service D-B so that they're linked together.

02:21.550 --> 02:24.520
OK so that's it for the block recomposing I'm always the same.

02:24.520 --> 02:25.610
Very simple.

02:25.980 --> 02:29.190
OK so let's go to the docker file of D-B.

02:29.350 --> 02:36.260
OK so there should be only one line which is from Mongo then 3.6 point for it and redone.

02:36.460 --> 02:41.410
So if you're using any other and there's a newer version of Mongo and you want to use it then feel free

02:41.410 --> 02:43.190
to just update this number.

02:43.210 --> 02:47.550
The API is usually or usually is the same regardless.

02:47.560 --> 02:48.440
OK.

02:48.940 --> 02:53.850
All right so now we have the docker file for the need for the application.

02:53.860 --> 02:59.640
So first of all we need bison because this is flask and classes from bison.

02:59.650 --> 03:08.230
So from bison 3 and then we're going to do work directory and slash user slash source slash app and

03:08.230 --> 03:16.630
then we're going to copy the requirements dot text requirements that text into dot slash to induce slash

03:16.630 --> 03:26.810
user slash slash output and then we're going to do a run chip install and then no cache directory Nashar

03:26.930 --> 03:30.870
and then requirements die dx.

03:30.970 --> 03:31.900
OK.

03:32.320 --> 03:33.030
All right.

03:33.130 --> 03:41.340
So the finals are two files things we need to copy all this folder to our Web applications or do copy

03:41.350 --> 03:42.340
dot dot.

03:42.460 --> 03:49.090
So we're going to copy this folder over here Web into slush use your slush or slush out and then the

03:49.090 --> 03:51.880
final thing is what we want to run up.

03:51.900 --> 03:58.640
So I want to do CMT and then Piscean and then add up the way.

03:58.700 --> 03:59.480
Right.

03:59.680 --> 04:00.480
And that should be it.

04:00.490 --> 04:03.020
So your save that and redone.

04:03.250 --> 04:10.120
Now what about the requirements well for this route we won't need any fancy or fancy requirements really.

04:10.180 --> 04:13.840
So I just need a flask and each flask restful.

04:13.900 --> 04:14.670
All right.

04:14.920 --> 04:19.610
So flask rest for I need time mango to communicate with.

04:19.870 --> 04:23.860
The bean and I need be Crypta for the password hashing.

04:23.860 --> 04:24.840
All right.

04:24.880 --> 04:32.140
And that's it only for libraries and we're done with building the architecture if you may say of the

04:32.140 --> 04:34.610
software or of the API.

04:34.830 --> 04:35.350
OK.

04:35.380 --> 04:38.760
So the next step that we want to do is to start implementing.

04:38.830 --> 04:42.340
So let's start implementing for example the registration.

04:42.750 --> 04:43.390
All right.

04:43.480 --> 04:49.290
So first of all we're going to import some libraries and some manuals some constructors and so on.

04:49.300 --> 04:56.050
So first of all we're going to import flump flask import the flask constructor Giese identify which

04:56.050 --> 05:01.250
is a super useful function that we always use and request to get the posted data.

05:01.540 --> 05:08.420
We're also going to import from flask restful and want to import AAPI.

05:08.530 --> 05:12.350
So how do you define this as an API and resource.

05:12.460 --> 05:16.180
Since I always inherit from it when I divide the classes.

05:16.180 --> 05:19.620
Next I'm going to import from PI Mongo.

05:19.810 --> 05:26.860
I want to import Mongo client and remember that M is capital and the C is capital and then I'm going

05:26.860 --> 05:28.560
to import secret.

05:28.910 --> 05:32.890
OK because I want to hash my password and that's it.

05:32.890 --> 05:40.960
Next I'm going to initialize my applications only as a app is equal to Flast constructor and that underscore

05:40.960 --> 05:43.390
underscore name and a square inch square gate.

05:43.450 --> 05:45.890
So that's usually main.

05:45.910 --> 05:52.720
Next I'm going to do an API and when I get initialized this apt to be an API I want to see API constructor

05:52.730 --> 05:54.260
off app.

05:54.640 --> 06:01.230
Right now the next step that we need to do is to connect to the byme up to the Mago DBI database writes

06:01.230 --> 06:03.870
on going to a client.

06:03.880 --> 06:07.940
First of all the client is equal to Mongo client.

06:08.710 --> 06:11.000
And then I'm going to write Mongo D-B.

06:11.000 --> 06:13.100
So let's see if I can zoom that in.

06:13.180 --> 06:16.690
Mongered Eby semi-colon slash slash.

06:16.690 --> 06:21.190
And then we're gonna say D-B dot 2 7 0 1 7.

06:21.190 --> 06:27.850
OK so that's the default point that Mago DBI runs on and D-B is that you are ELF from the dock or compose.

06:27.850 --> 06:29.360
Right so this is D-B.

06:29.470 --> 06:32.020
It means refer to this container over here.

06:32.230 --> 06:33.390
All right.

06:33.490 --> 06:35.580
OK cool.

06:35.680 --> 06:43.240
After we've connected our client we need to find a new database to say Give me is equal to client dot

06:43.510 --> 06:50.610
and then let's just say it banca API again so I'm going to define a new database called Mahnke API.

06:51.100 --> 06:53.950
Next I'm going to do a new collection called users.

06:53.980 --> 06:56.790
So I'm going to say users is equal to a.

06:56.830 --> 07:01.190
And then users inside a get go.

07:01.320 --> 07:06.580
Now let's first write a few helper functions that we might need in the registration function.

07:06.600 --> 07:12.720
So one of them that we always usually use is the user exists and one or I define user exist.

07:13.290 --> 07:14.020
OK.

07:14.190 --> 07:20.250
And then he's got eclipse exists and I gothic in a username and we're going that the way we're going

07:20.250 --> 07:25.280
to check a user exists is we're going to see if the users not find.

07:25.530 --> 07:31.330
And then I want to find some username with the one in the function parameter.

07:31.560 --> 07:37.600
So if I find that the count of such records is equal to zero then I return false.

07:37.650 --> 07:41.670
Right because this user name doesn't exist.

07:41.670 --> 07:42.390
So I work.

07:42.420 --> 07:45.930
So the answer to user exists is false.

07:45.930 --> 07:50.770
Else return true because the user does exist.

07:50.790 --> 07:53.990
If this count is not zero so it's greater than zero.

07:54.120 --> 07:57.040
Then this user exists and I should return true.

07:57.810 --> 08:03.060
All right so I'm done with the user exists now the next thing we need to do is to implement the registration

08:03.060 --> 08:03.520
class.

08:03.530 --> 08:10.920
I'm going to write a class register and then resource what I inherit from resource that I'm not into

08:10.980 --> 08:18.000
since all of these resources are remember all of them are post post post post but post some undefine.

08:18.020 --> 08:20.070
Post self.

08:20.760 --> 08:27.780
And then inside that the first step is usually to get the posted data right so posted data is equal

08:27.780 --> 08:28.840
to your request.

08:28.850 --> 08:30.600
DOD get underscore.

08:30.720 --> 08:32.270
Jason.

08:33.280 --> 08:44.790
OK after that I'm gonna get the username and password so username is equal to posted data username and

08:44.790 --> 08:46.410
password.

08:46.860 --> 08:51.780
You two posted data password.

08:51.880 --> 08:54.460
Alright ok cool.

08:54.570 --> 09:01.840
Now the next thing we're going to do is to check if the user exists so if user exist.

09:02.190 --> 09:06.290
So if user exists with this username.

09:06.630 --> 09:16.810
So if the user exists then I can't allow the user to or to to log in right because that would be.

09:17.040 --> 09:22.920
I can't have two usernames with the same user in two users with the same usernames so I won't allow

09:22.920 --> 09:28.970
that so I'm going to say returned Jason or return map isn't equal to the.

09:29.740 --> 09:32.670
Return map is equal to status is 3 or 1.

09:32.670 --> 09:35.880
Because it's an invalid username 3 or 1.

09:36.450 --> 09:43.110
And for the message I'm going to write invalid username invalid username and then I'm just going to

09:43.110 --> 09:45.260
return Jaysh on the fly.

09:45.940 --> 09:48.160
The return Jasen OK.

09:48.960 --> 09:49.410
All right.

09:49.420 --> 09:52.640
So this user doesn't exist on the other hand.

09:52.740 --> 10:02.510
Then I need to hash passwords I want to say hashed password is equal to the script so we grabbed the

10:02.610 --> 10:10.570
hash password that I'm on right password but don't forget to encode it as UTF 8.

10:10.700 --> 10:13.210
So when I asked UTF 8.

10:13.740 --> 10:20.530
And then when I write the script it generates salt and generates old function.

10:20.940 --> 10:23.800
Ok so now I've had my function.

10:23.940 --> 10:25.740
So what's the next thing to do.

10:25.830 --> 10:32.220
Well I need to store the user name and password into the database of my users.

10:32.580 --> 10:37.570
So his last password of course not the normal backstrap password.

10:37.650 --> 10:40.550
So many new users Doug answered.

10:40.810 --> 10:45.270
Ok then I inserted the user with this username.

10:45.300 --> 10:47.290
The one that was posted to me.

10:47.580 --> 10:55.710
I'm going to set put a user with the hashed password so hashed underscore which is hashed and the score

10:55.710 --> 10:56.990
password.

10:57.240 --> 11:02.460
And I'm going to put 2 extra Ifill's I want to put for each of the users.

11:02.550 --> 11:09.830
Can has 0 1 and get OK.

11:10.790 --> 11:14.180
So 0 in is how much money does he own.

11:14.190 --> 11:19.140
For example in your bank account you might own $1000 or $2000.

11:19.140 --> 11:26.470
So how much money does this person own and how much debt to the bank does this person own.

11:26.550 --> 11:29.120
So how many low how much loans has he taken.

11:29.280 --> 11:29.810
OK.

11:29.910 --> 11:34.650
So when you open an account you usually don't have anything in that country you have $0 and you have

11:34.650 --> 11:35.330
zero debt.

11:35.370 --> 11:36.050
Good.

11:36.390 --> 11:38.990
So we're going to start the user from scratch.

11:39.060 --> 11:40.110
OK.

11:40.650 --> 11:47.070
Now the second thing that we need to do is to return to reply the user that hey you successfully signed

11:47.070 --> 11:48.300
up for the API.

11:48.330 --> 11:48.900
Right.

11:48.900 --> 11:54.490
So going to see your return Jaison is equal to status is 200.

11:54.810 --> 12:02.850
And then for the message I'm going to return something along the line off use successfully signed of

12:02.870 --> 12:04.260
course the API.

12:04.260 --> 12:05.210
OK.

12:05.820 --> 12:07.120
And then just returned.

12:07.170 --> 12:17.760
Jason if I engage in a game and we're done we should be done with the registration class and that's

12:17.760 --> 12:18.140
it.

12:18.180 --> 12:18.890
OK.

12:19.110 --> 12:24.760
So the next class that we need to implement is going to be the class.

12:24.850 --> 12:25.380
Let's see.

12:25.420 --> 12:29.380
So we should probably implement that transfer class right.

12:29.520 --> 12:31.650
So let's see how we can implement that.

12:31.660 --> 12:32.110
OK.

12:32.250 --> 12:38.340
So first of all we need to implement a few helper functions that we're going to be using later on in

12:38.340 --> 12:44.820
the transfer and all the other classes really which are just so we can you know we don't have to reuse

12:44.820 --> 12:46.790
code or copy and paste code.

12:46.800 --> 12:52.560
So are you going to define functions that do the useful operations for us so you can just finish them

12:52.560 --> 12:53.610
in one line of code.

12:53.610 --> 12:54.220
OK.

12:54.690 --> 12:59.640
So the first helper function that we're going to use is the verify password function of an array and

12:59.910 --> 13:08.930
define verify password that is Gothika username and password and either going to return true or false.

13:09.240 --> 13:12.990
It returns true if the password is.

13:13.210 --> 13:18.620
This password matches this username and it returns false otherwise.

13:19.230 --> 13:19.770
All right.

13:19.800 --> 13:21.210
So let's get started.

13:21.210 --> 13:25.220
So first of all I need to make sure that this username exists in my database right.

13:25.230 --> 13:29.900
Because if he doesn't then this username definitely doesn't match this password.

13:30.090 --> 13:30.340
OK.

13:30.360 --> 13:37.500
So the first thing I do is check if not user exists with this username.

13:37.770 --> 13:40.230
Then just return false.

13:40.340 --> 13:46.940
Ok this user this password does not match this username otherwise is this user exists.

13:46.980 --> 13:49.780
So now I need to retrieve his hashed password.

13:49.930 --> 13:55.460
I'm going to get hashed password is equal to users that find.

13:55.620 --> 13:56.320
OK.

13:56.540 --> 14:02.990
And then I find the username user with this user name here that was posted.

14:03.690 --> 14:05.640
And I want to access the first one.

14:05.640 --> 14:08.090
There's only one user with this user name.

14:08.250 --> 14:16.410
And I want to get his password field so his hashed password the one I stored and then I'm going to see

14:16.500 --> 14:17.890
if be crypt.

14:17.970 --> 14:28.550
So if the crypt does Tasche password and then password does encode UTF 8.

14:29.210 --> 14:29.890
OK.

14:30.190 --> 14:37.260
And then hashed password is equal to the hashtag password.

14:37.360 --> 14:39.300
Then in that case I return true.

14:39.400 --> 14:45.210
Yes this password matches they use any else return false.

14:45.220 --> 14:46.730
This is incorrect right.

14:46.840 --> 14:53.820
So this this password does not match this using it because their hatches the hash is don't match.

14:53.980 --> 14:54.690
OK.

14:54.850 --> 14:58.590
And that's it for the function so the verify passwords on you.

14:58.840 --> 15:04.210
Now another function that might be useful is if I want to know how much cash this user has.

15:04.210 --> 15:06.750
How much does he own then.

15:06.820 --> 15:09.230
I should only do it whether it just won't go to a function.

15:09.250 --> 15:09.820
Right.

15:10.000 --> 15:18.640
So when I do define cash with user OK with this username and I want to know how much cash or how much

15:18.640 --> 15:19.940
does he owe you money.

15:19.960 --> 15:20.540
OK.

15:20.650 --> 15:28.540
So when I say cash is equal to users so just users does find that I'm going to find the user with this

15:28.540 --> 15:36.820
user name and I'm going to access the zero user because there's only one with this username and I'm

15:36.820 --> 15:41.390
going to get on because if you remember what he owns is in the field own.

15:41.570 --> 15:43.350
And I'm when to return cash.

15:43.450 --> 15:46.370
OK so that's how much the user owns.

15:46.510 --> 15:52.680
Now another function that we might of course need is to know how much debt does it does this user our.

15:52.770 --> 15:54.760
So why don't you find another function.

15:54.770 --> 16:03.200
Def define debt with a user with this username and I want to get the debt that he has.

16:03.210 --> 16:12.670
So what is a debt is equal to users defined gay and then the user name is going to be username and then

16:12.670 --> 16:18.030
I'm going to access the first user and I'm going to get the amount of debt that he has.

16:18.190 --> 16:18.860
OK.

16:19.000 --> 16:20.830
And then I just returned dead.

16:21.250 --> 16:26.640
So all these functions of course I'm using them with the assumption that this username exists.

16:26.650 --> 16:31.210
Now if you want to add some extra checks you're free to do so of course.

16:31.410 --> 16:32.320
OK.

16:32.850 --> 16:33.870
Now what else.

16:34.000 --> 16:40.270
Well we've got another function that we've been using in previous videos instead of just doing a return

16:40.270 --> 16:46.840
Jay Sawney calls to this and then filling it in every single time that we actually meet a function that

16:46.840 --> 16:50.000
generates this return dictionary automatically.

16:50.020 --> 16:51.880
So let's do it do this function again.

16:51.880 --> 16:58.710
So I want to do define generate a return dictionary.

16:58.980 --> 17:01.250
OK then I'm going to have a status.

17:01.380 --> 17:02.840
And then I have a message.

17:02.860 --> 17:03.430
OK.

17:03.700 --> 17:06.540
And then what does this function is going to do.

17:06.640 --> 17:16.080
It's going to go return Jaison is equal to its status is status message is the message.

17:16.540 --> 17:19.780
And I'm just going to return return they said.

17:19.900 --> 17:21.440
And that's basically the function.

17:21.460 --> 17:24.210
That's all it does.

17:24.430 --> 17:25.040
All right then.

17:25.050 --> 17:27.000
So far so cool.

17:27.130 --> 17:28.770
Now what else do we need.

17:28.930 --> 17:30.890
Well we need a couple of things oles.

17:31.060 --> 17:35.080
We also need a function that verifies credentials of a user.

17:35.110 --> 17:35.600
Right.

17:35.680 --> 17:45.340
So we need to check whether some user is in inputted the correct username and password or not.

17:45.340 --> 17:52.710
But then this function is not only going to return the this this password is correct.

17:52.750 --> 17:53.640
True or false.

17:53.710 --> 17:57.080
But it's also going to return the error that we should return.

17:57.190 --> 17:57.930
OK.

17:58.000 --> 18:01.260
So I'm going to define a function defined are you going to see what I mean by that.

18:01.270 --> 18:03.800
In a few seconds to define verify.

18:04.120 --> 18:12.100
OK so there's very psi credentials with username and password.

18:12.730 --> 18:13.480
OK.

18:13.720 --> 18:18.820
And then I want to say if not user exist user name.

18:18.820 --> 18:23.320
So this function is going to return to things like say this user doesn't exist.

18:23.320 --> 18:25.380
I'm going to return first of all.

18:25.400 --> 18:31.510
So this function to me at the conference here is going to add you're going to return a tuple.

18:31.570 --> 18:36.720
The first part of the tuple is going to be an error.

18:37.220 --> 18:38.890
Now dictionary.

18:38.920 --> 18:39.480
OK.

18:39.510 --> 18:41.380
It is going to be a dictionary with the error.

18:41.380 --> 18:44.600
So for example status three or three message.

18:44.640 --> 18:45.320
Blah blah blah.

18:45.340 --> 18:46.120
OK.

18:46.360 --> 18:51.490
And then the second part of that is going to be true or false.

18:51.580 --> 18:55.710
So this one is going to tell me whether there's an error or not in the credentials.

18:55.720 --> 18:59.240
So true means this user name and password there is a problem.

18:59.410 --> 19:02.390
And the problem is described in this ere dictionary.

19:03.130 --> 19:06.040
But if this is false then that means there is no problems.

19:06.040 --> 19:10.200
And then this dictionary is just none or no or whatever.

19:10.360 --> 19:11.240
OK.

19:11.470 --> 19:14.960
So if the user doesn't exist then I'm going to retire and do things.

19:14.980 --> 19:15.530
OK.

19:15.610 --> 19:18.490
I need to return the generator.

19:18.550 --> 19:25.570
So I'm going to generate dictionary with the air three or one I'm going to tell them invalid username

19:27.580 --> 19:30.200
but I'm also going to read you something else which is true.

19:30.250 --> 19:32.020
So I tell them hey there is an air.

19:32.040 --> 19:33.470
So take care.

19:33.940 --> 19:34.650
OK.

19:34.870 --> 19:37.410
Now I'm going to say correct password.

19:37.420 --> 19:40.050
So what is the correct password of this user.

19:40.180 --> 19:45.430
Well I'm going to say all correct answer is basically a bully and says not tell me whether this password

19:45.430 --> 19:46.680
is correct or not.

19:46.690 --> 19:49.400
So let me use the function that we are in the road here.

19:49.630 --> 19:57.670
Verify and P.W. is going to write verify that with you with this username and this password.

19:57.900 --> 20:04.790
Ok now I'm going to check it's not the correct answer is the password is not correct.

20:04.900 --> 20:12.460
Now I'm going to read Tyrian generate riparian dictionaries on one or turn it and an error dictionary

20:12.550 --> 20:20.830
of three or two which is an incorrect in correct password.

20:20.830 --> 20:22.340
And I'm also going to return true.

20:22.360 --> 20:23.980
So that means I'm returning some.

20:23.970 --> 20:25.440
There is a problem.

20:25.930 --> 20:33.550
However if I pass these do tests and everything is fine then I just return non and false.

20:33.550 --> 20:39.420
So there is no errors and the error dictionary is none because there is no errors right.

20:39.950 --> 20:40.470
OK.

20:40.530 --> 20:46.300
So now a couple of functions that were and it was done with the helper functions and we're not going

20:46.300 --> 20:51.030
to write any help or functions from now on it's gonna to be strictly classes.

20:51.130 --> 20:57.070
So the last two is we are going to write two functions to update the account to update the balance of

20:57.100 --> 21:01.360
what a user owns and what that debt off a user.

21:01.360 --> 21:07.270
OK so we're going to update our update account from to update what the user owns or when I write an

21:07.270 --> 21:11.260
update debt account to update how much debt the user has.

21:11.260 --> 21:11.930
OK.

21:12.280 --> 21:15.780
So the first one is define update account.

21:16.070 --> 21:16.810
OK.

21:16.960 --> 21:21.840
That takes in that username and then a balance and then it needs to update.

21:21.880 --> 21:24.180
What this user owns to the balance.

21:24.310 --> 21:26.900
So when do users update.

21:27.100 --> 21:27.780
OK.

21:28.300 --> 21:34.180
And then user name is username and that what I want update it with.

21:34.180 --> 21:43.900
So I'm going to do a comma and then set and that is that year I want to change his what he owns into

21:43.900 --> 21:44.790
the new balance.

21:44.800 --> 21:45.850
Okay.

21:46.540 --> 21:48.010
And then that's it.

21:48.010 --> 21:50.360
So that's what all the functions.

21:50.740 --> 21:54.420
Now the same thing goes for update debt.

21:54.540 --> 21:57.760
OK so that's a new function with user name and balance.

21:58.020 --> 22:01.460
And I need to update his debt to be this new balance.

22:01.760 --> 22:04.340
Do users update.

22:04.480 --> 22:05.290
Okay.

22:05.780 --> 22:11.690
And then I'm going to choose the correct user.

22:12.910 --> 22:20.020
Without you Capital your posts so use your name and then for a year I'm going to sit through things

22:20.030 --> 22:23.520
I'm going to do a dollar set and I'm going to change his debt.

22:23.650 --> 22:26.260
So the debt is going to be the new balance.

22:26.260 --> 22:27.130
All right.

22:27.310 --> 22:33.070
So now we finally finished the helper functions and now let's finally finally finally right.

22:33.070 --> 22:37.820
The class ad and you're going to see why these helper functions are very useful.

22:37.960 --> 22:46.180
Ok so now I want to write a classical ad which allows me to add a couple of some money into my accounts

22:46.250 --> 22:46.810
own.

22:46.870 --> 22:47.640
OK.

22:47.940 --> 22:50.430
So then a new class added resource.

22:50.780 --> 22:58.190
And then when I do define post self and then first of all I want I need to get the posted data.

22:58.230 --> 23:02.340
I get it from request that I get underscore Jason.

23:02.530 --> 23:03.240
OK.

23:03.550 --> 23:17.360
Next I get username is equal to post a data username password is equal to the posted data password.

23:17.560 --> 23:18.520
All right.

23:19.030 --> 23:25.090
Then we do also want the money right how much money you want to add to the bank account so money you

23:25.080 --> 23:27.980
go to post a data money.

23:28.120 --> 23:29.280
OK.

23:29.620 --> 23:30.580
All right.

23:30.850 --> 23:33.560
Or amounts listless change that to amount to great.

23:33.580 --> 23:36.640
So it makes more sense.

23:36.640 --> 23:39.690
All right so the first thing is we need to verify the credentials.

23:39.850 --> 23:45.850
So I'm going to use this function here which is the verify credentials and it returns two things the

23:45.850 --> 23:48.510
dictionary and the true or false.

23:48.520 --> 23:49.290
OK.

23:49.720 --> 23:51.370
Whether there is an error or not.

23:51.580 --> 24:01.590
So when I say returned Jason and error is equal to verify credentials with this user name and password.

24:01.690 --> 24:02.820
OK.

24:03.340 --> 24:04.710
Now if error.

24:05.020 --> 24:11.530
So there is an error if this is true then I'm just going to return this on if I return Jason I'm going

24:11.530 --> 24:14.850
to return that Garrett or whatever it is in this dictionary Okay.

24:15.010 --> 24:15.590
And I'm done.

24:15.640 --> 24:18.340
Just a line of go see how useful this is.

24:18.880 --> 24:19.460
OK.

24:19.540 --> 24:23.450
Another thing is I want to check if the money is less than or equal to zero.

24:23.530 --> 24:27.560
So I'm going to see if money is less than or equal to zero.

24:27.850 --> 24:28.980
Then what do I do.

24:29.200 --> 24:36.610
Well I need to return to justify and then generate return dictionary.

24:36.790 --> 24:42.700
I'm going to wager and there are three or four if you remember from here three or four is if the amount

24:42.700 --> 24:49.630
of money is less than or equal to zero or here okay three or four of them on our return three or four

24:49.870 --> 24:59.080
and then I'm going to also say let's say the money amount entered must be greater than zero.

24:59.100 --> 25:01.550
Okay great then year.

25:02.380 --> 25:08.620
And that's it in case we see how this this is very useful or you're turning all these errors checks

25:08.650 --> 25:09.880
into just one line.

25:09.880 --> 25:11.580
So that's pretty useful.

25:12.100 --> 25:15.180
OK so the money is not less than or equal to zero.

25:15.220 --> 25:20.650
So if the money is greater than zero then first of all I want to know how much cash does this user have.

25:20.940 --> 25:27.880
I say cash is equal to cash with the user with this username.

25:27.880 --> 25:32.620
Now the second thing is I want to add I want to have an account for the bank right.

25:32.630 --> 25:33.850
Is your member here.

25:34.030 --> 25:35.790
I have an account for the bank.

25:35.880 --> 25:40.400
And so I need to give the bank some money for this transaction right.

25:40.420 --> 25:44.220
Or you can think of it as money for using this service.

25:44.230 --> 25:50.800
So in fact what I'm going to do is for every transaction I'm going to take only $1 from the users and

25:50.800 --> 25:53.080
put it and give it to my bank account.

25:53.110 --> 25:54.110
OK.

25:54.130 --> 26:03.140
So I'm going to do see money minus the school wants someone to take away one for myself and give the

26:03.140 --> 26:04.870
remaining to the user.

26:04.880 --> 26:10.340
OK so I'm going to take away one from one from the money and then I'm going to check how much does the

26:10.340 --> 26:11.000
bank have.

26:11.000 --> 26:14.360
So bank cash is equal to cash with the user.

26:14.420 --> 26:16.510
And then I'm going to call the bank a bank.

26:16.510 --> 26:18.310
Great with the all caps.

26:18.350 --> 26:26.270
So this is I want to know how much of that the bank has and then I'm I do update account or I want to

26:26.270 --> 26:31.020
update the bank with the bank cash.

26:31.100 --> 26:40.340
So his previous balance that he had that the bank had plus one because I stick in one dollar as a transaction

26:40.340 --> 26:41.940
fee.

26:41.940 --> 26:43.690
Next I'm going to update.

26:43.730 --> 26:48.420
So I want to update the remaining amount of money to the user.

26:48.470 --> 26:50.870
So I'm going to do update account

26:53.270 --> 26:56.280
to the user with the user name.

26:56.390 --> 26:58.930
OK so no not none.

26:59.080 --> 27:04.130
And so just the user with this username and then I'm going to add cash plus money.

27:04.130 --> 27:04.720
OK.

27:04.910 --> 27:09.070
So it's the cash plus the money minus one day that we took away on.

27:09.140 --> 27:12.410
So the previous cash that he had plus the money and then he just added.

27:12.410 --> 27:16.760
So this is his new balance or her new balance rate.

27:17.180 --> 27:25.490
And then once I'm done with that then I just simply return on if I generate to return dictionary of

27:25.850 --> 27:26.430
200.

27:26.420 --> 27:33.650
So success and amount added successfully to the account.

27:33.680 --> 27:34.110
OK.

27:34.160 --> 27:35.880
So that's our message.

27:35.930 --> 27:38.370
And we were done with the add function.

27:38.450 --> 27:42.690
So you can see why these functions were very very very helpful.

27:42.740 --> 27:48.850
They just allow us to write this very concisely and all the logic is actually in here.

27:49.120 --> 27:49.770
OK.

27:50.000 --> 27:55.790
So we're going to stop here so we've written basically two to two of them sort of written register here

27:55.980 --> 27:58.630
and change the color or not.

27:58.910 --> 28:02.750
OK let's change the color to have written this register here.

28:02.750 --> 28:06.110
So this is a check and then were also written this ad over here.

28:06.110 --> 28:13.920
So this is also a check very bad check but so we do have or we still have transfer.

28:13.940 --> 28:15.860
We still have to check balance.

28:15.860 --> 28:20.810
We still have dayglo and we still have been able to do these ones in the next few years we're going

28:20.810 --> 28:23.410
to pause here and pick up in the next video.

28:23.420 --> 28:24.450
So yeah.

28:24.650 --> 28:26.390
So until the next video happy curbing.
