1
00:00:00,938 --> 00:00:02,421
- [Instructor] Hi, and
welcome back to the course.

2
00:00:02,421 --> 00:00:05,374
In this video, we're just going
to improve the code slightly

3
00:00:05,374 --> 00:00:07,643
by making use of our User class.

4
00:00:07,643 --> 00:00:11,810
The changes are only gonna
be in the user.py file.

5
00:00:13,270 --> 00:00:16,855
Remember how we've got
a User class up here

6
00:00:16,855 --> 00:00:20,688
that gives us the ability
to find by username.

7
00:00:21,627 --> 00:00:23,763
What we're going to do in the post request

8
00:00:23,763 --> 00:00:27,330
of the user register is make
sure that we're not registering

9
00:00:27,330 --> 00:00:30,730
a user with the same username twice.

10
00:00:30,730 --> 00:00:34,897
Now, knowing that the
user.find_by_username method

11
00:00:36,371 --> 00:00:40,819
gives you a user object, or
it gives you none if that

12
00:00:40,819 --> 00:00:44,986
didn't exist, how would you
check that a user exists

13
00:00:46,164 --> 00:00:48,247
in this post method here?

14
00:00:50,277 --> 00:00:52,260
Give yourself a wee
minute to think about it.

15
00:00:52,260 --> 00:00:56,427
How might you check that a
user exists already or not?

16
00:00:57,612 --> 00:00:59,562
Think about it, try to
implement a solution,

17
00:00:59,562 --> 00:01:03,729
and then come back and we
will solve it here as well.

18
00:01:05,994 --> 00:01:08,217
Okay, hopefully you got that.

19
00:01:08,217 --> 00:01:11,286
What I'd recommend is to do

20
00:01:11,286 --> 00:01:15,453
if
User.find_by_username(data['username']).

21
00:01:21,209 --> 00:01:25,292
Remember, if something
means if that is not None.

22
00:01:30,462 --> 00:01:34,210
So, if this is not None, we're gonna do

23
00:01:34,210 --> 00:01:37,210
return {"message": "A user with that

24
00:01:40,023 --> 00:01:42,606
username already exists"}, 400.

25
00:01:45,761 --> 00:01:49,032
Okay, and remember, if
we return, we do not run

26
00:01:49,032 --> 00:01:51,317
any of this stuff here.

27
00:01:51,317 --> 00:01:54,396
Make sure to have this in front of

28
00:01:54,396 --> 00:01:57,372
the connection creation line.

29
00:01:57,372 --> 00:02:00,166
If we put this outside
creating a connection,

30
00:02:00,166 --> 00:02:02,085
that connection will never close

31
00:02:02,085 --> 00:02:04,036
because we will never run this.

32
00:02:04,036 --> 00:02:05,907
So, make sure to put
this at the very start

33
00:02:05,907 --> 00:02:08,490
after parsing the data through.

34
00:02:11,889 --> 00:02:15,059
Now we have our main User class

35
00:02:15,059 --> 00:02:17,659
with this find_by_username
and find_by_id methods,

36
00:02:17,659 --> 00:02:21,826
and we are using the
find_by_username to find a user

37
00:02:22,982 --> 00:02:24,315
or give us None.

38
00:02:26,477 --> 00:02:29,960
So, down here, if User.find_by_username

39
00:02:29,960 --> 00:02:31,760
with the data that is being passed in

40
00:02:31,760 --> 00:02:34,441
through the JSON request is not None,

41
00:02:34,441 --> 00:02:36,035
then that means a user with that username

42
00:02:36,035 --> 00:02:38,595
is already in the database
because we found one

43
00:02:38,595 --> 00:02:41,479
so we can return this message.

44
00:02:41,479 --> 00:02:43,219
You can go ahead and try this in Postman

45
00:02:43,219 --> 00:02:45,216
but, believe me, it works.

46
00:02:45,216 --> 00:02:47,871
This is exactly where you
would expect it to do.

47
00:02:47,871 --> 00:02:49,668
This does exactly what you expect it to do

48
00:02:49,668 --> 00:02:51,935
so we're not gonna go into
trying it in Postman as well

49
00:02:51,935 --> 00:02:53,833
just to save you some time.

50
00:02:53,833 --> 00:02:55,206
Thanks for joining me in this video,

51
00:02:55,206 --> 00:02:56,890
and I'll see you in the next one.

