Hi everyone welcome back.
So in this video we're going to start to build the back API that we talked about.
OK.
So I made a new folder called Banky ions and deep games and I went into it I opened a new terminal and
I'm in it inside of it.
You're going to start building the pride from scratch.
OK.
So what what do we usually do.
Well you start with a docker composed file case or what I do not touch daquiri compose dykey and then
we're going to do two directories.
Make directory web and make directory.
Then again I and the doc files some Livesay touch web slash docker file them and to also touch divi
slash knocker file.
OK.
All right.
So inside webs are going to do is.
I have a couple of things that my application which is apt up is somewhat a touch at duppy wide.
I also have my requirements dot decks.
So I'm doing touch requirements text.
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.
OK.
So almost in the end folder
or write so we have a folder here.
So let's start with the compost file as usual.
So we're going to write as you do version and then three and then we're going to have two services.
We're going to have the Web and we're going to have D-B again.
So for Abebe It's simple.
We're going to build it from not slash TV.
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
on board.
So when I connect some boards and is going to be 5000 to 5000 right then I'm going to link it so links
and I'm going to add D-B into that link.
OK.
So I want to link it into the service D-B so that they're linked together.
OK so that's it for the block recomposing I'm always the same.
Very simple.
OK so let's go to the docker file of D-B.
OK so there should be only one line which is from Mongo then 3.6 point for it and redone.
So if you're using any other and there's a newer version of Mongo and you want to use it then feel free
to just update this number.
The API is usually or usually is the same regardless.
OK.
All right so now we have the docker file for the need for the application.
So first of all we need bison because this is flask and classes from bison.
So from bison 3 and then we're going to do work directory and slash user slash source slash app and
then we're going to copy the requirements dot text requirements that text into dot slash to induce slash
user slash slash output and then we're going to do a run chip install and then no cache directory Nashar
and then requirements die dx.
OK.
All right.
So the finals are two files things we need to copy all this folder to our Web applications or do copy
dot dot.
So we're going to copy this folder over here Web into slush use your slush or slush out and then the
final thing is what we want to run up.
So I want to do CMT and then Piscean and then add up the way.
Right.
And that should be it.
So your save that and redone.
Now what about the requirements well for this route we won't need any fancy or fancy requirements really.
So I just need a flask and each flask restful.
All right.
So flask rest for I need time mango to communicate with.
The bean and I need be Crypta for the password hashing.
All right.
And that's it only for libraries and we're done with building the architecture if you may say of the
software or of the API.
OK.
So the next step that we want to do is to start implementing.
So let's start implementing for example the registration.
All right.
So first of all we're going to import some libraries and some manuals some constructors and so on.
So first of all we're going to import flump flask import the flask constructor Giese identify which
is a super useful function that we always use and request to get the posted data.
We're also going to import from flask restful and want to import AAPI.
So how do you define this as an API and resource.
Since I always inherit from it when I divide the classes.
Next I'm going to import from PI Mongo.
I want to import Mongo client and remember that M is capital and the C is capital and then I'm going
to import secret.
OK because I want to hash my password and that's it.
Next I'm going to initialize my applications only as a app is equal to Flast constructor and that underscore
underscore name and a square inch square gate.
So that's usually main.
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
off app.
Right now the next step that we need to do is to connect to the byme up to the Mago DBI database writes
on going to a client.
First of all the client is equal to Mongo client.
And then I'm going to write Mongo D-B.
So let's see if I can zoom that in.
Mongered Eby semi-colon slash slash.
And then we're gonna say D-B dot 2 7 0 1 7.
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.
Right so this is D-B.
It means refer to this container over here.
All right.
OK cool.
After we've connected our client we need to find a new database to say Give me is equal to client dot
and then let's just say it banca API again so I'm going to define a new database called Mahnke API.
Next I'm going to do a new collection called users.
So I'm going to say users is equal to a.
And then users inside a get go.
Now let's first write a few helper functions that we might need in the registration function.
So one of them that we always usually use is the user exists and one or I define user exist.
OK.
And then he's got eclipse exists and I gothic in a username and we're going that the way we're going
to check a user exists is we're going to see if the users not find.
And then I want to find some username with the one in the function parameter.
So if I find that the count of such records is equal to zero then I return false.
Right because this user name doesn't exist.
So I work.
So the answer to user exists is false.
Else return true because the user does exist.
If this count is not zero so it's greater than zero.
Then this user exists and I should return true.
All right so I'm done with the user exists now the next thing we need to do is to implement the registration
class.
I'm going to write a class register and then resource what I inherit from resource that I'm not into
since all of these resources are remember all of them are post post post post but post some undefine.
Post self.
And then inside that the first step is usually to get the posted data right so posted data is equal
to your request.
DOD get underscore.
Jason.
OK after that I'm gonna get the username and password so username is equal to posted data username and
password.
You two posted data password.
Alright ok cool.
Now the next thing we're going to do is to check if the user exists so if user exist.
So if user exists with this username.
So if the user exists then I can't allow the user to or to to log in right because that would be.
I can't have two usernames with the same user in two users with the same usernames so I won't allow
that so I'm going to say returned Jason or return map isn't equal to the.
Return map is equal to status is 3 or 1.
Because it's an invalid username 3 or 1.
And for the message I'm going to write invalid username invalid username and then I'm just going to
return Jaysh on the fly.
The return Jasen OK.
All right.
So this user doesn't exist on the other hand.
Then I need to hash passwords I want to say hashed password is equal to the script so we grabbed the
hash password that I'm on right password but don't forget to encode it as UTF 8.
So when I asked UTF 8.
And then when I write the script it generates salt and generates old function.
Ok so now I've had my function.
So what's the next thing to do.
Well I need to store the user name and password into the database of my users.
So his last password of course not the normal backstrap password.
So many new users Doug answered.
Ok then I inserted the user with this username.
The one that was posted to me.
I'm going to set put a user with the hashed password so hashed underscore which is hashed and the score
password.
And I'm going to put 2 extra Ifill's I want to put for each of the users.
Can has 0 1 and get OK.
So 0 in is how much money does he own.
For example in your bank account you might own $1000 or $2000.
So how much money does this person own and how much debt to the bank does this person own.
So how many low how much loans has he taken.
OK.
So when you open an account you usually don't have anything in that country you have $0 and you have
zero debt.
Good.
So we're going to start the user from scratch.
OK.
Now the second thing that we need to do is to return to reply the user that hey you successfully signed
up for the API.
Right.
So going to see your return Jaison is equal to status is 200.
And then for the message I'm going to return something along the line off use successfully signed of
course the API.
OK.
And then just returned.
Jason if I engage in a game and we're done we should be done with the registration class and that's
it.
OK.
So the next class that we need to implement is going to be the class.
Let's see.
So we should probably implement that transfer class right.
So let's see how we can implement that.
OK.
So first of all we need to implement a few helper functions that we're going to be using later on in
the transfer and all the other classes really which are just so we can you know we don't have to reuse
code or copy and paste code.
So are you going to define functions that do the useful operations for us so you can just finish them
in one line of code.
OK.
So the first helper function that we're going to use is the verify password function of an array and
define verify password that is Gothika username and password and either going to return true or false.
It returns true if the password is.
This password matches this username and it returns false otherwise.
All right.
So let's get started.
So first of all I need to make sure that this username exists in my database right.
Because if he doesn't then this username definitely doesn't match this password.
OK.
So the first thing I do is check if not user exists with this username.
Then just return false.
Ok this user this password does not match this username otherwise is this user exists.
So now I need to retrieve his hashed password.
I'm going to get hashed password is equal to users that find.
OK.
And then I find the username user with this user name here that was posted.
And I want to access the first one.
There's only one user with this user name.
And I want to get his password field so his hashed password the one I stored and then I'm going to see
if be crypt.
So if the crypt does Tasche password and then password does encode UTF 8.
OK.
And then hashed password is equal to the hashtag password.
Then in that case I return true.
Yes this password matches they use any else return false.
This is incorrect right.
So this this password does not match this using it because their hatches the hash is don't match.
OK.
And that's it for the function so the verify passwords on you.
Now another function that might be useful is if I want to know how much cash this user has.
How much does he own then.
I should only do it whether it just won't go to a function.
Right.
So when I do define cash with user OK with this username and I want to know how much cash or how much
does he owe you money.
OK.
So when I say cash is equal to users so just users does find that I'm going to find the user with this
user name and I'm going to access the zero user because there's only one with this username and I'm
going to get on because if you remember what he owns is in the field own.
And I'm when to return cash.
OK so that's how much the user owns.
Now another function that we might of course need is to know how much debt does it does this user our.
So why don't you find another function.
Def define debt with a user with this username and I want to get the debt that he has.
So what is a debt is equal to users defined gay and then the user name is going to be username and then
I'm going to access the first user and I'm going to get the amount of debt that he has.
OK.
And then I just returned dead.
So all these functions of course I'm using them with the assumption that this username exists.
Now if you want to add some extra checks you're free to do so of course.
OK.
Now what else.
Well we've got another function that we've been using in previous videos instead of just doing a return
Jay Sawney calls to this and then filling it in every single time that we actually meet a function that
generates this return dictionary automatically.
So let's do it do this function again.
So I want to do define generate a return dictionary.
OK then I'm going to have a status.
And then I have a message.
OK.
And then what does this function is going to do.
It's going to go return Jaison is equal to its status is status message is the message.
And I'm just going to return return they said.
And that's basically the function.
That's all it does.
All right then.
So far so cool.
Now what else do we need.
Well we need a couple of things oles.
We also need a function that verifies credentials of a user.
Right.
So we need to check whether some user is in inputted the correct username and password or not.
But then this function is not only going to return the this this password is correct.
True or false.
But it's also going to return the error that we should return.
OK.
So I'm going to define a function defined are you going to see what I mean by that.
In a few seconds to define verify.
OK so there's very psi credentials with username and password.
OK.
And then I want to say if not user exist user name.
So this function is going to return to things like say this user doesn't exist.
I'm going to return first of all.
So this function to me at the conference here is going to add you're going to return a tuple.
The first part of the tuple is going to be an error.
Now dictionary.
OK.
It is going to be a dictionary with the error.
So for example status three or three message.
Blah blah blah.
OK.
And then the second part of that is going to be true or false.
So this one is going to tell me whether there's an error or not in the credentials.
So true means this user name and password there is a problem.
And the problem is described in this ere dictionary.
But if this is false then that means there is no problems.
And then this dictionary is just none or no or whatever.
OK.
So if the user doesn't exist then I'm going to retire and do things.
OK.
I need to return the generator.
So I'm going to generate dictionary with the air three or one I'm going to tell them invalid username
but I'm also going to read you something else which is true.
So I tell them hey there is an air.
So take care.
OK.
Now I'm going to say correct password.
So what is the correct password of this user.
Well I'm going to say all correct answer is basically a bully and says not tell me whether this password
is correct or not.
So let me use the function that we are in the road here.
Verify and P.W. is going to write verify that with you with this username and this password.
Ok now I'm going to check it's not the correct answer is the password is not correct.
Now I'm going to read Tyrian generate riparian dictionaries on one or turn it and an error dictionary
of three or two which is an incorrect in correct password.
And I'm also going to return true.
So that means I'm returning some.
There is a problem.
However if I pass these do tests and everything is fine then I just return non and false.
So there is no errors and the error dictionary is none because there is no errors right.
OK.
So now a couple of functions that were and it was done with the helper functions and we're not going
to write any help or functions from now on it's gonna to be strictly classes.
So the last two is we are going to write two functions to update the account to update the balance of
what a user owns and what that debt off a user.
OK so we're going to update our update account from to update what the user owns or when I write an
update debt account to update how much debt the user has.
OK.
So the first one is define update account.
OK.
That takes in that username and then a balance and then it needs to update.
What this user owns to the balance.
So when do users update.
OK.
And then user name is username and that what I want update it with.
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
the new balance.
Okay.
And then that's it.
So that's what all the functions.
Now the same thing goes for update debt.
OK so that's a new function with user name and balance.
And I need to update his debt to be this new balance.
Do users update.
Okay.
And then I'm going to choose the correct user.
Without you Capital your posts so use your name and then for a year I'm going to sit through things
I'm going to do a dollar set and I'm going to change his debt.
So the debt is going to be the new balance.
All right.
So now we finally finished the helper functions and now let's finally finally finally right.
The class ad and you're going to see why these helper functions are very useful.
Ok so now I want to write a classical ad which allows me to add a couple of some money into my accounts
own.
OK.
So then a new class added resource.
And then when I do define post self and then first of all I want I need to get the posted data.
I get it from request that I get underscore Jason.
OK.
Next I get username is equal to post a data username password is equal to the posted data password.
All right.
Then we do also want the money right how much money you want to add to the bank account so money you
go to post a data money.
OK.
All right.
Or amounts listless change that to amount to great.
So it makes more sense.
All right so the first thing is we need to verify the credentials.
So I'm going to use this function here which is the verify credentials and it returns two things the
dictionary and the true or false.
OK.
Whether there is an error or not.
So when I say returned Jason and error is equal to verify credentials with this user name and password.
OK.
Now if error.
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
to return that Garrett or whatever it is in this dictionary Okay.
And I'm done.
Just a line of go see how useful this is.
OK.
Another thing is I want to check if the money is less than or equal to zero.
So I'm going to see if money is less than or equal to zero.
Then what do I do.
Well I need to return to justify and then generate return dictionary.
I'm going to wager and there are three or four if you remember from here three or four is if the amount
of money is less than or equal to zero or here okay three or four of them on our return three or four
and then I'm going to also say let's say the money amount entered must be greater than zero.
Okay great then year.
And that's it in case we see how this this is very useful or you're turning all these errors checks
into just one line.
So that's pretty useful.
OK so the money is not less than or equal to zero.
So if the money is greater than zero then first of all I want to know how much cash does this user have.
I say cash is equal to cash with the user with this username.
Now the second thing is I want to add I want to have an account for the bank right.
Is your member here.
I have an account for the bank.
And so I need to give the bank some money for this transaction right.
Or you can think of it as money for using this service.
So in fact what I'm going to do is for every transaction I'm going to take only $1 from the users and
put it and give it to my bank account.
OK.
So I'm going to do see money minus the school wants someone to take away one for myself and give the
remaining to the user.
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
bank have.
So bank cash is equal to cash with the user.
And then I'm going to call the bank a bank.
Great with the all caps.
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
update the bank with the bank cash.
So his previous balance that he had that the bank had plus one because I stick in one dollar as a transaction
fee.
Next I'm going to update.
So I want to update the remaining amount of money to the user.
So I'm going to do update account
to the user with the user name.
OK so no not none.
And so just the user with this username and then I'm going to add cash plus money.
OK.
So it's the cash plus the money minus one day that we took away on.
So the previous cash that he had plus the money and then he just added.
So this is his new balance or her new balance rate.
And then once I'm done with that then I just simply return on if I generate to return dictionary of
200.
So success and amount added successfully to the account.
OK.
So that's our message.
And we were done with the add function.
So you can see why these functions were very very very helpful.
They just allow us to write this very concisely and all the logic is actually in here.
OK.
So we're going to stop here so we've written basically two to two of them sort of written register here
and change the color or not.
OK let's change the color to have written this register here.
So this is a check and then were also written this ad over here.
So this is also a check very bad check but so we do have or we still have transfer.
We still have to check balance.
We still have dayglo and we still have been able to do these ones in the next few years we're going
to pause here and pick up in the next video.
So yeah.
So until the next video happy curbing.