Probably everyone will come back.
So in this video we're going to be finishing the four last resources in the API for the bank and we're
going to be wrapping up the whole API basically.
OK.
So let's get started.
So the next class that we're going to be riding's Let's go back to your robot I'm writing the next class
which is the transfer transfer ability.
OK.
So we're going to have a class called transfer.
Then we're going to inherit from resource.
OK.
And then we're going to do define boast self.
So we're going to only support posts because all of them support posts here.
So you can post post post post post and post it.
All of them support post.
Someone I knew posted data is equal to your request so we get to the crux of the first step was always
to get the posts the data.
Then for this one I have a couple of things right.
I have the username the password that.
And the money.
OK so is your memory here.
I have the username password too and the amount.
OK so when a new user name is equal to posted data the user name the and want to get the username password
as usual.
And then there's a certain thing I want to get to Gaisford do is equal to posted data and then I want
to get to just let's just leave the field to them the money is going to be posted data and then amount.
OK so how much amount or how much money do you want to transfer from what I got to you.
OK now a couple things.
First of all we need to verify as usual the credentials.
OK so I want to say return on error is equal to verify credentials.
With this username and password then I'm going to say if there is any errors in the code in that this
function finds out that I'm going to return the size I The Returned is OK.
All right so now I've checked the credentials of the user.
Now I need to check that the catch of the user is enough to transfer right.
So what to say cash is equal to cash with user usernames I must see how much money does he have.
Then I say if cash is less than or equal to zero then in that case I'm going to be returning an error
message which is going to be used on inside and then generate return dictionary.
This is going to be a three or four if you remember three or four is not enough.
So you're out of money so he usually needs to add more money than you're going to do.
You're out of money.
Please add or take a loan.
OK and that's it.
OK so that's if the money is less than or equal to zero.
Now what about if the user does not exist right.
So which user do you mean here.
I mean they use it too.
So let's say you're trying to say two to no sense mind to Santa Claus or Santa Claus doesn't exist right.
Hopeful.
Well if you're a kid and he does exist.
But anyways so if this account doesn't exist in the bank then I can't just transfer it to that account.
So I'm going to do if not use or exists.
So if this user was the name two doesn't exist then in that case I'm going to return the sun if I generate
return dictionary that I'm going to pass to the user a serial one because this user doesn't exist so
it's an invalid username.
Now I'm going to pass the message receiver.
User name is invalid.
OK.
And we're done.
Ok cool.
So other than that what do we need to do.
Well I think to first see how much money does the user how how much money does the recipient have said
the receiver how and also how much money is in the bank because I'm going to take $1 from the trans
as a transaction fee some say cash from is equal to cash with a user username then I'm also going to
see cash to is equal to cash with the user too.
So this is the cash from is the money with me the cash.
Who has the money.
The person I'm sending do.
And the bank cash is equal to cash with user.
His name is bank.
Man in all capitals.
Now going to update the account update account.
Bank and some guy at a desk bank underscore cash plus want someone to add $1 into the bank count as
a transaction fee.
I'm also going to add.
So I'm going to also update the account of the person to reset the money.
And I'm going to send in cash too.
So the money that he already had plus the amount of case plus the money that was sent to him minus one
which is the transaction fee that I that the bank got.
Correct.
And then finally I want to update the account of the person who sent.
So it's the journey that I'm going to basically subtract the money from him.
OK.
So I'm going to do cash no score from minus money and that's it.
OK.
Now of course you can argue that.
Or how do I know that he has enough money that we can add also this next yearbook.
Feel free to if you don't want to do that then that's fine too.
All right.
So now once we're taken taking away the money from the amount that he had then the only thing left is
to journey to success is a success message.
OK.
So I'm going to return Sunnyside and then Gennari to return dictionary as do hundred pounds amount transferred
successfully.
OK great.
And that's it for the for the transfer function.
So that's the transfer function over here.
So it's transferred some money from one account to the other account every day.
OK.
Now the next one is to you and we tried the balance function right.
How much balance are these are Hatsune this one is over.
So how much balance are they use or have someone write class balance that inherits from resource then
I'm going to support the post because all of them are post and then the first step is as usual get the
posted data and we get it from request dobbed get their score.
Jason OK once they have that then I want the username and the password so my user name is equal to posted
data username and password is you to post a data password.
All right.
Now the next thing is I need to check for credentials again sir returned Jason.
Error is difficult to verify credentials.
This username and password none of my check if there then return.
If I the return Jason.
OK.
So that's the one that has years.
All right.
Now a return Jason is equal to users.
Signed.
Going to find the user name the user with this user name.
And then you get access.
Well I'm going to basically.
So I'm trying to find a user.
OK.
But this user I want to get their response that I want to get is they want to get only his user name
his 0 0 1 or 0 1 and his debt.
OK.
So I don't want to see the I.D. and I don't want to see the password of this user return.
So we're glad to hear the projection if you remember from the Mongo D-B slides we talked about projection
which is to hide some of the some of the fields from showing up so I'm going to do a comma here so I'll
come here and then I'm to do another.
Ansal here.
No mercy password.
I don't want to see that password return.
So I'm going to do zero.
OK.
Then I also don't want to see the underscore ID automatic feel underscore ID and then also put zero
aside.
So that means it seems to me the the file off the user with this user name but omit the fields password
and amid the fields ID because they don't want the user to see you know to transmit to him the hashed
password.
Right.
It doesn't make sense and it doesn't make sense to send him the underscore ID.
OK.
So I'm going to make both of these Troung being sent in get.
All right.
And then of course zero because that's the first person right.
There's only one person to find.
OK.
So now what we need to do is to simply return.
Jason if I return Jason and that's it.
And we have basically we are done.
OK.
And we're done with the class we have gotten that balance the bounce information for this user and can
tell you what the user what his balance is what he owns and what he what that he has.
OK.
So now the last two classes that we have so let's go back here.
So now we're done with the check balance and that we need to take a loan and pay low.
OK.
So the first one is the the payload.
OK slow or let's do the take long first class take loan and then we're going to do research
and we're going to define the post function on itself because all of them are ghosts.
And then we're going to get the posted data.
So the data is equal to request I get discouraged.
We're going to get the user name is equal to posted data username
you're also going to get the password from the post posted data to who posted data password.
OK.
Now what else do or are we going to get where we want to know how much money does the user want to take
as loans or what to do.
Money is equal to posted data amount.
OK.
All right.
So the first step is as usual verify that the credentials are correct.
So return Jason and password is equal to verify the credentials.
And then with this username and password then.
So this one should be here.
So return Jason an air is able to verify credentials.
Then again I say if there then return this on if I return Jason.
OK.
All right now there is no ears that I need to see the cash with the user and his debt.
And then I need to increase his cash and his debt.
OK.
So when I do cash is you go to cash with the user.
You don't want to do.
Debt is equal to cash with user or debt.
True these are some that this one isn't cash with the user.
This one's going to be debt whether user user name that are going to say update account.
So I'm going to update the account the user name.
OK.
And then I'm going to add change to happen cash plus money.
So now this user has withdrawn or took a loan off this money.
Right.
Of this amount from the bank.
So I'm going to increase his own by this money so if you're for example you have zero debt and you have
zero money in the bank account.
So if you take a loan will you increase your money by two.
Let's say you take a loan of $200.
Then you increase your account by $200.
But you also increase your debt so update debt here increase your debt with so that it's the previous
debt plus then you know that you took the new loan that that your debt is now OK.
And then now you can just return Jason if I
generate return dictionary and then we're going to say 200.
And then a message for example loan added to your account.
OK.
So that's basically you're saying the loan was added to your account as a money.
OK.
So the last class that we're going on right is finally class pillow cases where he is going to be the
opposite of this class.
Exactly the opposite right.
Because what we're doing is well we're taking away the debt subtracting the debt that he has.
OK.
So when do you class pay the loan and then resource.
Then we're going to find the post function.
So.
All right.
And then we're going to get first of all as usual Vost and Dana and you go to request that get underscored
any on that we're going to use your name is equal to posted Nina.
You didn't name and password UKL the posted data password.
And then money is equal to the posted data amount.
OK cool.
So far so good.
Now we checked the credentials.
And then we see if there is any error.
So if there then return your son if I turn Jason the Greek or answer once we're done with that we want
to check how much money is they use your house we say cash is equal to cash with user on the username
McCain and then I'm going to say if cash is less than money say the cash is less than the amount that
he's paying that he doesn't have enough money.
So I'm going to say return the sun if I generate return dictionary of three or three and then not enough
cash in your account.
OK.
And we're done.
If on the other hand this is not true then I'm going to do.
Debt is equal to debt with the user.
So when I see how much debt does he have then I'm going to subtract the amount.
So the the accounts are going to subtract the amount that he paid from his from what he owns.
So I'm going to do cash.
So the previous cash that he had minus money and now and then add subtract also what he goes to the
bank to do update debt and then this username with debt minus moneys and such like this amount from
his debt.
OK.
And that should be it.
And we're just going to return to the user return on if I generate return dictionary of 200.
You've successfully paid your loan something like that or your loan in Spain.
Ok done.
So the only thing left to do is to actually add the resources or are going to do it API.
And underscore resource.
And then I'm going to copy that and then I copy it since we have six classes.
So I'm going to copy that six times.
OK.
So one two three four five six.
OK.
So we have the register class which is at slash register.
We have the case let's go back here.
All the spaces we have the add which is add slash and we have also the transfer.
Right.
And slash transfer.
We have.
What else do we have.
So we have the transfer you have also the balance which is at slash balance.
And finally we're now finally we have two new ones which appear to take low and the payload to each
of them and take loan and the other one is at payload bay.
So pay long and slash the loan so that all the resources into our API.
Now the last thing we do is you say if name is equal to underscore main then at door around then host
is equal to zero point zero point zero point zero.
Okay.
And that's it.
So we have successfully written and finished the entire API.
So we're going to stop here and in the next video we're going to be testing the entire API and seeing
how it works.
We're going to do several accounts and see how we can transfer money from one account to the other and
so on.
So we're going to stop here and we're going to complete the next video.
So until the next video decoding.