WEBVTT

00:05.210 --> 00:05.820
Hi everyone.

00:05.820 --> 00:06.710
Welcome back.

00:06.920 --> 00:12.620
So in this video we're going to be implementing the actual Abiah that we designed in the previous video

00:12.620 --> 00:13.250
here.

00:13.340 --> 00:17.400
And then we're going to be implementing it in class.

00:17.600 --> 00:18.660
So let's get started.

00:18.830 --> 00:22.730
So the first thing that we have to do is to handle the database connection right.

00:22.730 --> 00:25.620
As usual we want connect to the database.

00:25.730 --> 00:32.060
So actually we can just that's why I haven't deleted the previous app we can just copy this area over

00:32.060 --> 00:32.660
here.

00:32.750 --> 00:40.460
So if we copy this area over here all the way to here and then copy this then we can automatically just

00:40.490 --> 00:44.520
we don't have to write it from scratch we can look at it so we can delete this one.

00:44.530 --> 00:45.680
You don't need it.

00:45.950 --> 00:46.580
And yeah.

00:46.610 --> 00:46.940
OK.

00:46.940 --> 00:53.040
So from flask import flask Jaison if I had requests So these all these are going to be using.

00:53.120 --> 00:58.880
We're also going to be using flask restful to work we'll import API and resource and we're also going

00:58.880 --> 01:01.390
to be implement using mango pie.

01:01.420 --> 01:07.730
Man would you be right to store the sentences so gonna do from Mongo import Mongo client.

01:07.940 --> 01:11.700
So we initialize the app and we say that it's an API.

01:12.080 --> 01:15.610
And then the client is equal to Mongo client dot.

01:15.710 --> 01:21.280
So we're connecting to this Mongo DBI you're here from knocker compose and then we're saying.

01:21.440 --> 01:29.510
So instead I'm right I said VB is equal to client dot and then all of us call it sentence's database

01:30.230 --> 01:31.320
database.

01:31.610 --> 01:35.430
So this database is going to be called sentence's database.

01:35.570 --> 01:41.530
And then I'm going to have a different table in that I'm going to have a table called let's say sentences

01:41.530 --> 01:43.920
write so sentences so D-B.

01:43.980 --> 01:45.870
So what if we look here.

01:45.890 --> 01:47.320
This is the collection.

01:47.330 --> 01:56.680
So when I have a different collection what I have I see sentences is equal to D.B and then sentences.

01:56.780 --> 02:01.110
Ok so I'm going to have a collection called sentences and I can now remove this one.

02:01.310 --> 02:07.080
So now I have a collection of all the sentences that the users send me.

02:07.250 --> 02:15.440
But if you notice your then then there's a problem because one user should have one once more one or

02:15.440 --> 02:16.240
more sentences.

02:16.240 --> 02:18.350
So this design isn't very good.

02:18.440 --> 02:25.310
So instead what we're going to be using is to make a collection off users and for each of these users

02:25.520 --> 02:31.280
we're going to be putting inside of them their sentences right and we're going to put a embedded document

02:31.280 --> 02:33.210
inside of them with their sentences.

02:33.210 --> 02:37.070
I'm going to say users is equal to D.B users.

02:37.100 --> 02:39.800
OK so restoring the users themselves.

02:39.820 --> 02:43.200
OK hopefully this makes sense.

02:43.310 --> 02:46.580
Now the next thing is we want to support registration right.

02:46.640 --> 02:48.450
So we're going to say class.

02:48.560 --> 02:50.770
And then we say register.

02:51.350 --> 02:52.730
And this is a resource.

02:52.730 --> 02:55.430
So we are bringing resource here.

02:55.760 --> 03:03.820
And then as we said here we said register whereas it's at slash register and where it's also support

03:03.830 --> 03:04.540
post.

03:04.580 --> 03:05.060
Right.

03:05.150 --> 03:07.860
So let's go back to here.

03:08.030 --> 03:14.990
And if you remember here for free if we do what we do for example this class over here class and then

03:14.990 --> 03:21.380
we do a defined post and that if I want to get the data I just say posted to you that is equal to request.

03:21.470 --> 03:27.240
Get Jaison and automatically get the data that the user sent sent me right.

03:27.290 --> 03:34.980
So I'll do the same thing here so register my defined post which is what is supposed to self.

03:35.390 --> 03:41.840
So this is going to be the post the function that handles the registration of the user.

03:41.840 --> 03:51.690
Now what I want to do on step one is to get posted data by the user.

03:52.370 --> 03:58.360
And the way I do this is the same as below I'm going to do a request that get Jansa Jaison.

03:58.550 --> 04:04.610
And now I have this piece on or that they use are posted from a post a data is equal to request JCA.

04:04.850 --> 04:07.670
Now the second thing is out I need to verify.

04:07.940 --> 04:13.600
Well in that case in here we assume that the user always sent the username and password right.

04:13.730 --> 04:19.850
If you want to make this much more detail right you want to add some extra conditions that maybe the

04:19.850 --> 04:23.150
user didn't send a password or maybe that you didn't send the user name.

04:23.150 --> 04:25.240
Or maybe you didn't send anything.

04:25.430 --> 04:31.470
So if you actually want to do this checking you can also add so extra responses in your calls here.

04:31.610 --> 04:35.050
But for now we want to do you know a very basic API.

04:35.210 --> 04:41.210
Let's trust the user let's say OK he's always going to send us the username and password so we don't

04:41.210 --> 04:42.310
have to worry about it.

04:42.500 --> 04:42.840
OK.

04:42.900 --> 04:43.960
Let's go back.

04:45.480 --> 04:49.370
So now how do we how do we access these things.

04:49.410 --> 04:55.870
If you remember if we go down here all the way to add and then we say after we get the posted data.

04:55.950 --> 04:58.970
And that ad we got we call the check posted data.

04:58.980 --> 05:01.220
So let's go to check post data.

05:01.430 --> 05:02.910
And if you remember here.

05:03.060 --> 05:04.370
Let's see where it's at.

05:04.410 --> 05:10.170
So we can access the posted data as really a dictionary with the index that we're looking for.

05:10.170 --> 05:11.780
So for example posted data.

05:11.800 --> 05:12.300
Why.

05:12.300 --> 05:13.140
Right.

05:13.170 --> 05:16.860
So it will do the same thing that we're expecting.

05:16.860 --> 05:19.900
So the first thing is to get that data right.

05:19.950 --> 05:22.860
So get the data.

05:23.040 --> 05:30.870
So what is the data we want the username that the user would register with that user name is equal to

05:31.530 --> 05:43.680
posted data and that say these are signs Senza username and then the password is equal to the data and

05:43.680 --> 05:46.790
then password.

05:46.800 --> 05:49.480
Now obviously the user doesn't send them in this form.

05:49.480 --> 05:54.440
Then you have your server the crash because there is no username or password read.

05:54.510 --> 06:01.200
Once you once you use them as once you assume they're there they're right but they're not.

06:01.200 --> 06:09.480
So of course see if you're you are developing a real API to be you know going to the deployment then

06:09.480 --> 06:16.200
you make sure you're here to to add this response codes here to make sure that everything that you received

06:16.200 --> 06:16.760
is good.

06:16.770 --> 06:19.880
There's no problems here and this registration form.

06:19.910 --> 06:20.570
Right.

06:21.240 --> 06:21.740
OK.

06:21.810 --> 06:24.610
So let's go back now.

06:24.690 --> 06:26.700
I have my username and password.

06:26.700 --> 06:29.900
Now we need to talk about something else.

06:29.970 --> 06:32.130
How do I store the username and password right.

06:32.150 --> 06:38.490
I know what I need to do is to store this username and password into my database and users such that

06:38.580 --> 06:39.800
this user here.

06:39.810 --> 06:50.790
So this username and password this user would would have basically this user would be able to when he

06:50.810 --> 06:55.440
tried to log in again he would be able to access this username and password.

06:55.690 --> 07:00.230
He would give it to me and then I would compare it to the one stored in my database that if they're

07:00.250 --> 07:04.470
the same that I would let him to log in or access to the API.

07:04.680 --> 07:11.040
But you've probably heard every once in a while about a company which you know gets very bad publicity

07:11.250 --> 07:16.410
and usually it's because they've stored this password wrongly in the database.

07:16.410 --> 07:24.690
And the way they do this is they take this password as Ro data so this password might be sent as 1 2

07:24.690 --> 07:26.780
3 x y z.

07:26.820 --> 07:27.630
Right.

07:27.690 --> 07:32.580
So they take this password over here and it is just they don't it into the database.

07:32.580 --> 07:35.460
They just put it into the database and they don't care about it.

07:35.490 --> 07:36.270
Right.

07:36.270 --> 07:43.500
So this is really really really bad because if they give you if any hacker is able to access database

07:43.560 --> 07:49.440
which happens a lot if any data if any hacker acts as that then you're in big trouble.

07:49.440 --> 07:49.700
Right.

07:49.700 --> 07:53.320
Because he can see all the usernames and passwords of people.

07:53.400 --> 07:55.700
And usually they use your names and e-mail.

07:55.710 --> 07:59.810
So now they use it and most people don't create new passwords right.

07:59.820 --> 08:04.340
So mostly they can just reuse these passwords with these user names.

08:04.530 --> 08:12.010
And they can just get access to your customers information or e-mails and passwords and all that stuff.

08:12.210 --> 08:13.360
So that's very bad.

08:13.590 --> 08:18.590
So we never store a password in a database in its general form.

08:18.720 --> 08:25.620
So usually what we do is we do something called hashing what is happening now hashing is a function.

08:25.620 --> 08:32.370
So we take a hash of the password plus solved and we'll talk about solved later.

08:32.550 --> 08:38.010
But we basically hash the password and we add to it a little bit of soap and then it should give us

08:38.010 --> 08:40.060
something some jibberish you're right.

08:40.080 --> 08:45.080
And we just got denoted by this and this is what we store in the database.

08:45.090 --> 08:45.990
Right.

08:46.560 --> 08:51.350
And then the main idea of this is that you can go backward.

08:51.360 --> 08:51.780
Right.

08:51.870 --> 08:57.870
If you know this you can't know the password but you have to know that password to generate this.

08:57.870 --> 09:00.870
So this is a very unique thing about hash functions.

09:01.020 --> 09:08.050
If you know the password you can generate this very.

09:08.230 --> 09:14.160
But if you don't know the if you don't if you know the hash you cannot generate the password.

09:14.200 --> 09:23.770
So what we do is we store this hash inside this database over here and then for when when the user tries

09:23.770 --> 09:28.260
to log in again I hash the password that he sends me.

09:28.510 --> 09:32.060
So let's say he sends me again 1 to 3 x y Zed.

09:32.080 --> 09:35.480
So when I hash it it generates the same hash.

09:35.500 --> 09:38.830
So now I can compare it to the hash that I have.

09:38.830 --> 09:45.340
So in other words I'm not storing the password of the user of the other people who are registering into

09:45.340 --> 09:46.690
my API.

09:46.780 --> 09:53.030
I'm actually storing just the hash of their password and reversing the hash is very very difficult.

09:53.080 --> 09:57.920
It's almost impossible to do so except if you've done something wrong.

09:57.970 --> 09:59.770
Right along the line.

10:00.010 --> 10:06.910
So now what we need to do is to hash the password and to use this we're going to use a tool called Piatt

10:06.910 --> 10:07.400
crypt.

10:07.400 --> 10:14.350
OK so this is pipe B script and this is a python library that allows us to hash function.

10:14.350 --> 10:16.770
Now I want to show you how this function works.

10:16.960 --> 10:20.060
So I'll just open the terminal here on the right.

10:20.230 --> 10:28.430
Then I just realize I don't have pie grips on most a pimp install decrypt.

10:28.710 --> 10:29.470
OK.

10:30.180 --> 10:36.210
So whoops suruh app Instow by some pick first.

10:36.310 --> 10:40.600
So we make sure we have some bip that's important.

10:40.900 --> 10:50.000
And then once we have it a pause now and then once I have it I'll come back and resume the video.

10:50.020 --> 10:51.270
OK so welcome back.

10:51.400 --> 10:59.570
So now we're going to do PID install script and that is going to be collecting me cribs and installing

10:59.570 --> 11:03.990
it it shouldn't be too big.

11:06.940 --> 11:12.340
OK so now that we have be created are going to open bytes and just open a python terminal and we're

11:12.340 --> 11:14.290
going to important be great.

11:14.290 --> 11:16.900
Great great.

11:16.930 --> 11:18.730
Now for a look at the examples here.

11:18.730 --> 11:26.320
So the example is actually at if if you're if you just write in Google the PI B script and then you

11:26.320 --> 11:26.930
choose.

11:26.980 --> 11:36.530
So why and then dash and then b y c r y P T and then you choose the first link it's at mandragora or

11:36.700 --> 11:41.020
slash projects slash pi pi be great.

11:41.470 --> 11:45.710
So if we go down to the example they have the API is very simple to use.

11:45.760 --> 11:54.340
So we're going to use first of RSA hashed is equal to be crypt dot hash password.

11:54.620 --> 11:56.870
And then for the password we're going to enter strings.

11:56.870 --> 12:01.310
Let's say that my password is 1 2 3 x y is it OK.

12:01.580 --> 12:09.080
And then I'm also going to be crypt that generate SOAD and then generate us all.

12:09.330 --> 12:11.060
And then I click enter.

12:11.120 --> 12:14.600
So now my password to 1 2 3 x y is and has been hashed.

12:14.600 --> 12:20.640
Now if I print Hashd you would see that this is just a bunch of garbage right.

12:20.680 --> 12:22.330
You don't understand what this is right.

12:22.370 --> 12:27.580
You can't you can't go back to my original password 1 2 3 X was it.

12:27.590 --> 12:34.290
So what you do is you store this hash or your and you store it in your database with the user name.

12:34.520 --> 12:36.770
Now let's say the user now wants to log in.

12:36.880 --> 12:42.320
So he gives you his user name and the password so that we know that the password that he's giving you

12:42.320 --> 12:44.170
is actually the correct password right.

12:44.180 --> 12:47.980
That gives you this hash Well you hash it again.

12:47.980 --> 12:48.420
Right.

12:48.590 --> 12:50.230
So you say so over here.

12:50.240 --> 12:54.890
So if I do be cribbed that hash password.

12:54.890 --> 13:00.550
Now let's say the user gives me 1 2 3 4 x y is that so that's not the correct password.

13:00.860 --> 13:04.240
And then Hashd is equal to Hashd.

13:04.250 --> 13:10.910
Now if this is true then that means that this Basford or here is the same as the original password 1

13:10.910 --> 13:12.280
2 3 X was it.

13:12.290 --> 13:15.960
But of course this would give me false because they're not the same.

13:16.310 --> 13:20.410
But if I delete this for over a year so it's now 1 2 3 x y Zed.

13:20.570 --> 13:23.670
And then I click enter then it should give me true.

13:23.990 --> 13:31.010
So in other words if you notice here the only time that I used 5:59 the original password is when I

13:31.010 --> 13:32.260
was hashing it.

13:32.270 --> 13:38.690
And then I stored the hash in the in my database and then when I wanted to make sure whether the user

13:38.690 --> 13:45.650
was the same user or not then I would generate I would hash the password that he sends me and compare

13:45.650 --> 13:48.170
it to the hash that I have in my database.

13:48.290 --> 13:50.840
So that way this is a much more secure system.

13:50.870 --> 13:51.410
Right.

13:51.650 --> 13:52.920
So we need decrypt.

13:53.030 --> 13:57.810
So we're going to add it to our requirements that where we're going to add be grouped.

13:57.950 --> 14:00.930
So be correct.

14:01.670 --> 14:01.960
Right.

14:02.000 --> 14:03.010
That's how it's built.

14:03.020 --> 14:06.580
I think you point b c r y p t.

14:06.740 --> 14:09.330
OK so we're going to import the crib.

14:09.500 --> 14:14.480
And then we're going to go back to our application that we're developing and we're going to do import

14:15.310 --> 14:16.770
regrouped again.

14:17.440 --> 14:21.800
OK so now after the user has sent us the password we want to hash it.

14:21.980 --> 14:29.990
So we say hashed password is equal to and then we copy the code from your so it's hashed is equal to

14:30.800 --> 14:31.990
this line over years.

14:32.000 --> 14:32.920
Copy it.

14:32.930 --> 14:34.730
Put it here.

14:35.330 --> 14:35.870
Oops.

14:35.900 --> 14:37.870
So as to delete this.

14:38.020 --> 14:40.330
So it's B script dot hash password.

14:40.490 --> 14:47.970
And then the password is this password over here that the user posted to us and I generate a sock.

14:48.540 --> 14:51.740
So now what I'm going to do is I want to store.

14:51.810 --> 14:57.580
So I want to store the username and password too.

14:57.800 --> 15:04.840
So I want to store the username and the password into the database.

15:05.610 --> 15:06.730
So how do you do this.

15:06.840 --> 15:12.250
Well we access the users where we access that we insert something into the users.

15:12.480 --> 15:18.740
So we do users that insert going insert something.

15:19.490 --> 15:26.960
And so I'm going to insert user name is going to be user name that he posted to me.

15:27.510 --> 15:36.930
And I'm also going to put the password right so password and for the password I'm going to put the hashed

15:37.650 --> 15:38.780
password OK.

15:41.140 --> 15:48.950
Now an extra thing I'm going to put it I'm going to put the the sentence to be stored so each user has

15:48.950 --> 15:54.970
the corresponding sentence a sentence and then the corresponding sentence for now is just going to be

15:55.660 --> 15:56.520
an empty string.

15:56.530 --> 15:56.800
Right.

15:56.800 --> 15:59.890
So the user doesn't have any stored sentence for now.

15:59.960 --> 16:03.280
So let's just initialize it with an empty sentence.

16:03.580 --> 16:04.300
OK.

16:04.570 --> 16:05.470
I'm gonna save that.

16:05.480 --> 16:13.780
So now this user I'm going to insert into it our record with this username password sentence and that's

16:13.780 --> 16:19.540
it OK once I'm done with that then I go what's the next step.

16:19.540 --> 16:22.480
Well I want to return to the user addiction.

16:22.510 --> 16:23.650
So I'm going to say.

16:23.650 --> 16:25.720
So how do we how do we use to do that.

16:25.720 --> 16:31.790
If you look here we would generate a map and then we would do a turn it was just on ephi.

16:32.100 --> 16:34.450
OK we'll do the same thing here.

16:34.560 --> 16:39.600
Redo return Jaison is equal to a map.

16:39.730 --> 16:40.200
OK.

16:40.330 --> 16:48.430
So I want to tell the user that he successfully logged into a status 200.

16:48.430 --> 16:51.160
So now the status quo is 200.

16:51.340 --> 16:53.270
And that means that he successfully.

16:53.280 --> 17:00.040
So we get even at a message a message and you've successfully

17:02.440 --> 17:05.790
signed up get signed up for the API.

17:06.090 --> 17:12.530
So now the user knows that he entered US 206 Esco and he signed up to the API and I returned that as

17:12.570 --> 17:13.060
Jason.

17:13.060 --> 17:18.870
So we turn Jason if I was your son if I returned Jewson.

17:19.660 --> 17:20.490
OK.

17:20.920 --> 17:29.200
Now a couple things to consider here is for example what happens to you if the user tries to sign up

17:29.200 --> 17:31.840
twice using the same username and password.

17:31.990 --> 17:39.280
So again if we go back to our resources here we should assume that we have some air quality here that

17:39.340 --> 17:44.230
tells if a user tries to sign to sign up twice then I can return him and Eyrecourt.

17:44.270 --> 17:46.820
There's there is a user already with this user name.

17:46.850 --> 17:47.350
Right.

17:47.410 --> 17:48.910
What we didn't do that here.

17:48.970 --> 17:53.980
So there is also something to consider and we talked about a lot of stuff that we could add here but

17:53.980 --> 18:00.040
we haven't really added that many right because we're trying to do is just a very basic API or proof

18:00.040 --> 18:03.320
of concept so we're not going to be deploying this anytime soon.

18:04.460 --> 18:06.960
So this is just an idea to keep in mind.

18:07.250 --> 18:13.360
OK so now that we have to we have we have this register we now we can add it to our API.

18:13.370 --> 18:22.010
So I'm going to do API that add resource and then I'm going to add this resource register over here

18:23.540 --> 18:29.480
and I'm going to add it at slash and we agreed that it will be slash register.

18:29.720 --> 18:33.160
So I do slash register.

18:33.290 --> 18:34.390
Great.

18:34.720 --> 18:35.230
Okay.

18:35.300 --> 18:37.950
So also we might as well add this.

18:38.000 --> 18:41.360
The last thing here if name is Main then APTA right.

18:41.390 --> 18:42.120
Right.

18:42.530 --> 18:46.620
It's it's common for all the applications in general.

18:47.210 --> 18:48.990
So we're going to add this one here.

18:49.440 --> 18:54.890
Okay so the next thing that we need to do is to handle the next two resources which are store sentence

18:54.920 --> 18:56.390
and retrieve sentence.

18:56.630 --> 18:58.640
So I'm going to do.

18:58.640 --> 19:05.320
First of all the story centers on one new class or and this is a resource.

19:06.320 --> 19:12.170
And then we said that for a store we're going to be using post-ride because we're sending a sentence

19:12.170 --> 19:16.560
to the user to do the the API.

19:16.820 --> 19:21.690
So we're going to say Fine post itself.

19:22.340 --> 19:25.780
And then in this year we first got Want to get that posted data.

19:25.780 --> 19:34.730
So step 1 get the posted data so we get that using the same line of code here as opposed to data is

19:34.730 --> 19:36.640
equal to requests that get us up.

19:36.680 --> 19:48.860
So we can copy that and then step two step two is to read the data read the data so we have a couple

19:48.860 --> 19:55.220
of things here we have my user name is equal to posted data e.g.

19:57.900 --> 20:11.320
and password is equal to posted data password and we also have the last thing the sentence sentence

20:11.410 --> 20:17.020
is equal to posted data sentence.

20:18.010 --> 20:18.680
All right.

20:18.820 --> 20:20.810
So we have all three things here.

20:20.890 --> 20:22.540
Now first of all we want to.

20:22.560 --> 20:31.370
So the steps three or you'll see steps three here is verify the username password match.

20:31.450 --> 20:31.720
OK.

20:31.720 --> 20:39.220
So verify that this user is indeed at a password and it matches his password.

20:39.430 --> 20:41.690
And then step four Once verified.

20:41.720 --> 20:52.420
So step for once I've verified that I should verify also that he has enough tokens or verify very high

20:52.660 --> 21:03.770
user has enough tokens and that if he has enough tokens then steps live Storer the sentence.

21:05.500 --> 21:08.720
And return 200.

21:08.800 --> 21:09.800
OK.

21:10.240 --> 21:16.450
Now a couple of things in the original registration we should have started the user with 10 tokens right

21:16.460 --> 21:23.280
so we got this year through going to and dash and then we got tokens and then 10.

21:23.500 --> 21:28.530
So we're going to give the user or actually just give him five OK so or six.

21:28.570 --> 21:29.550
An even number.

21:29.590 --> 21:35.020
So we will give the user only six tokens for every time he wants to input something.

21:35.110 --> 21:36.200
Right.

21:36.970 --> 21:40.900
OK so the first step is we want to verify that they use your name pass.

21:40.900 --> 21:50.710
So for that I'm going to make a helper function so I'm going to say correct password is equal to verify

21:51.280 --> 21:55.240
and then I'll I'll give it the user name and password.

21:55.270 --> 21:55.810
OK.

21:56.050 --> 22:03.100
So I'm going to verify this username and password to verify password.

22:03.790 --> 22:06.450
And I'm also once I verified it.

22:06.460 --> 22:09.900
Want to say if correct.

22:10.090 --> 22:12.840
If not correct password.

22:13.690 --> 22:20.850
So if not correct password then return Jason is equal to free go back to our choice here.

22:20.920 --> 22:24.670
Then if his user name is wrong then it's three or two.

22:24.760 --> 22:27.710
So I'm going to give him a status of three and two.

22:28.000 --> 22:32.490
So let's go back and then I'm say returned Jaison.

22:32.790 --> 22:37.190
And then I can tell him status is three or two.

22:37.420 --> 22:41.560
So he knows what he did wrong and then return Jaison if I

22:44.300 --> 22:50.080
returned J-Zone this.

22:51.000 --> 22:51.610
OK.

22:51.870 --> 22:52.710
So that's if.

22:52.730 --> 23:00.420
So we still need to write the verse password function and then also we're going to say enough tokens

23:02.180 --> 23:08.610
is equal to verify tokens for this username and password.

23:09.030 --> 23:13.970
So since we're already here so we don't need the password right because if we're past this dept and

23:14.010 --> 23:19.650
that means his username and password are correct so we can just check if this user name has enough.

23:19.860 --> 23:23.930
Has this in the past has enough tokens or not.

23:23.940 --> 23:31.260
So we're going to do it enough toker enough tokens is equal to verify tokens and then we say if not

23:31.360 --> 23:32.980
enough tokens.

23:33.460 --> 23:35.610
So if not enough tokens.

23:35.730 --> 23:36.670
And what do we do.

23:36.670 --> 23:46.360
And the return Jaison is equal to a map and the status in it is going to go back here.

23:46.440 --> 23:49.520
It will be 3 or 1 right out of tokens.

23:50.460 --> 23:52.080
So three or one.

23:52.550 --> 23:58.100
And then we simply return to satisfy our return Jason.

23:58.420 --> 23:59.490
OK.

24:00.020 --> 24:04.600
So if these two steps pass then we finally can return.

24:04.640 --> 24:06.460
First of all store the sentence.

24:06.530 --> 24:07.450
So how do we do that.

24:07.450 --> 24:15.160
I always say users write users so users what is users that's needed the collection.

24:15.160 --> 24:18.120
So are you going to say users data update.

24:18.380 --> 24:19.200
OK.

24:19.820 --> 24:20.990
And there's two things right.

24:20.990 --> 24:26.990
The selection criteria is going to be by the user name so user name.

24:26.990 --> 24:34.210
So I'm going to update and then the criteria I'm looking for is the user name here.

24:34.220 --> 24:38.950
I want his user name to match the selection criteria.

24:39.370 --> 24:47.260
Let's put that on your line so the user name is going to be user name.

24:48.130 --> 24:48.940
Right.

24:49.360 --> 24:50.070
Let's see.

24:50.140 --> 24:50.500
Yep.

24:50.530 --> 24:55.230
It's going to be user name but without a capital.

24:55.440 --> 25:01.260
And then that's the selection criteria and for what we're going to replace it with we're going to replace

25:01.260 --> 25:06.610
it with a dollar or dollar set.

25:06.630 --> 25:09.210
So we're going to let's open a new line actually.

25:09.660 --> 25:20.520
So we're going to do a dollar set and then it's going to be a list or a document and this document is

25:20.520 --> 25:22.580
going to contain what I want to replace.

25:22.580 --> 25:32.460
And going to replace a sentence or two here this one over your with the new sentence so sentence.

25:35.090 --> 25:39.210
With the new sentence so sentence.

25:39.710 --> 25:46.790
And I also want to set the tokens to be minus one right so let's do it on a new line.

25:46.790 --> 25:48.160
So first of all it's.

25:48.170 --> 25:53.810
His is his the sentence that he sent us.

25:53.810 --> 25:57.110
Now we take away was that he said restore the sentence.

25:57.370 --> 26:01.860
Take one token away and returned to him.

26:01.910 --> 26:02.440
OK.

26:02.570 --> 26:06.450
So the first thing is we're going to update the user.

26:06.590 --> 26:09.180
The user with the user name is your name.

26:09.290 --> 26:12.560
And then we're going to set his sentence to the sentence.

26:13.090 --> 26:21.920
Now that that is a second thing is actually you know what we can actually change the idea to verify

26:21.920 --> 26:29.520
it for verified tokens to actually count tokens so he can count the number of tokens that he had and

26:29.520 --> 26:32.590
then number of tokens here.

26:33.300 --> 26:39.130
And so we say if not our number off tokens is bigger than zero.

26:39.780 --> 26:47.020
So is the number of tokens is less than or equal to zero then returned Jaison is.

26:47.040 --> 26:49.570
So you can actually get rid of this.

26:49.750 --> 26:54.610
And so we have the number of tokens is less than or equal to zero.

26:54.690 --> 26:58.530
And then in that case returns three or one else.

26:58.620 --> 27:05.700
I'm gonna update histo again so I can actually do it in this file right here so I'll click enter here

27:06.000 --> 27:09.320
and Tab this one over here.

27:09.480 --> 27:09.880
Right.

27:09.930 --> 27:13.390
So we don't have to go back and count the number of tokens again.

27:13.470 --> 27:19.680
I can just automatically do a comma here and then I'm going to say the number of tokens.

27:19.700 --> 27:19.990
Right.

27:20.010 --> 27:22.900
And if your memory is T with a capital.

27:23.160 --> 27:24.390
So tokens.

27:24.810 --> 27:31.510
And then I'm going to say none tokens and tokens minus one.

27:31.510 --> 27:34.440
So I subtract one from his tokens.

27:34.810 --> 27:42.210
So now I'm replacing his for this user and the user with this username on setting his sentence to be

27:42.220 --> 27:46.550
this and his tokens to be this over here.

27:47.260 --> 27:51.850
OK so now we've subtracted one from his tokens and restored his sentence.

27:51.850 --> 28:05.370
Now we can tell him returned Jason is equal to status is 200 and we can tell him you know we can we

28:05.370 --> 28:14.760
can you can also send him a message if he wants a message and sentence saved successfully.

28:15.050 --> 28:15.830
OK.

28:16.100 --> 28:17.620
And then we can now return.

28:17.720 --> 28:28.700
Jason if I return Jason so that should be it for for the registration and for storage.

28:29.610 --> 28:39.120
Now the final thing is if you know this year that we have done the register and the store so we'll stop

28:39.120 --> 28:42.650
here and in the next video we want to see first test this.

28:42.650 --> 28:45.710
So make sure this is working with no problems.

28:45.900 --> 28:51.300
And then we're going to do the final resource which is the retrieve the sentence.

28:51.480 --> 28:53.770
And that should be it for the API.

28:54.200 --> 28:57.350
So yeah we'll stop here and until the next video.

28:57.460 --> 28:58.060
Be Koning.
