1
00:00:00,890 --> 00:00:02,240
Hi, and welcome back.

2
00:00:02,240 --> 00:00:05,700
In this video, we are going to add another Resource

3
00:00:05,700 --> 00:00:08,883
to our application just to help with testing.

4
00:00:10,530 --> 00:00:13,300
Now, in the last few sections,

5
00:00:13,300 --> 00:00:14,450
we were creating users,

6
00:00:14,450 --> 00:00:17,350
authenticating with them, using Flask JWT

7
00:00:17,350 --> 00:00:20,720
but we were never able to retrieve information

8
00:00:20,720 --> 00:00:22,860
about a specific user to make sure that,

9
00:00:22,860 --> 00:00:26,170
you know, it existed and it was all good.

10
00:00:26,170 --> 00:00:28,810
And also, we were not able to delete users.

11
00:00:28,810 --> 00:00:30,550
So every time we wanted to, you know,

12
00:00:30,550 --> 00:00:33,760
re-register, we had to delete data.db

13
00:00:33,760 --> 00:00:35,310
and bring it back and so forth.

14
00:00:36,750 --> 00:00:39,210
By the way, talking about data.db,

15
00:00:39,210 --> 00:00:41,310
remember that data.db gets created for you

16
00:00:41,310 --> 00:00:43,480
whenever you run your application

17
00:00:43,480 --> 00:00:45,380
and here, in Visual Studio Code,

18
00:00:45,380 --> 00:00:49,020
I've got a terminal that is exactly the same

19
00:00:49,020 --> 00:00:50,450
as in any other terminal.

20
00:00:50,450 --> 00:00:52,270
It's got the virtual environment enabled,

21
00:00:52,270 --> 00:00:55,640
and it can run my application in here, so.

22
00:00:55,640 --> 00:00:57,410
Just gonna make things a little bit simpler for you

23
00:00:57,410 --> 00:00:59,320
without me having to switch back and forth

24
00:00:59,320 --> 00:01:00,270
between terminals.

25
00:01:00,270 --> 00:01:01,660
We've got a terminal here,

26
00:01:01,660 --> 00:01:05,150
and but we're gonna close it for now cause we don't need it.

27
00:01:05,150 --> 00:01:09,390
Okay, let's go into adding our Resource.

28
00:01:09,390 --> 00:01:13,460
We're gonna go into user.py in resources.

29
00:01:13,460 --> 00:01:15,600
And we're gonna see that here we've got our

30
00:01:15,600 --> 00:01:17,930
user register class.

31
00:01:17,930 --> 00:01:20,670
This Resource we created a long time ago

32
00:01:20,670 --> 00:01:23,220
for creating or registering new users.

33
00:01:23,220 --> 00:01:25,760
And it takes in a username and a password,

34
00:01:25,760 --> 00:01:28,590
and it has a host method that allows us

35
00:01:28,590 --> 00:01:32,203
to create new user modules and save them to the database.

36
00:01:33,370 --> 00:01:35,260
Now, I'm gonna just collapse that

37
00:01:35,260 --> 00:01:37,290
by pressing the little minus icon there,

38
00:01:37,290 --> 00:01:38,800
so give us a bit more room.

39
00:01:38,800 --> 00:01:41,080
And we're gonna create a new resource,

40
00:01:41,080 --> 00:01:45,890
so that we can retrieve user details and delete users.

41
00:01:45,890 --> 00:01:49,140
This is gonna be a small resource that you already know

42
00:01:49,140 --> 00:01:53,240
how to make, in order to just enable us to do this.

43
00:01:53,240 --> 00:01:55,310
So, we're gonna call it user,

44
00:01:55,310 --> 00:01:58,160
and it's going to inherit from the Resource class

45
00:01:58,160 --> 00:01:59,853
that we already know and love.

46
00:02:00,730 --> 00:02:02,823
And it's gonna have two methods.

47
00:02:04,330 --> 00:02:09,330
A get, that is going to take a user ID

48
00:02:11,430 --> 00:02:12,800
and is also going to have

49
00:02:14,870 --> 00:02:15,973
a delete method,

50
00:02:16,840 --> 00:02:19,600
that is also going to take our user ID.

51
00:02:19,600 --> 00:02:22,320
This is similar to, for example,

52
00:02:22,320 --> 00:02:24,610
our store resource,

53
00:02:24,610 --> 00:02:26,290
that I'm going to open just now,

54
00:02:26,290 --> 00:02:30,250
where each of the endpoints take in the name.

55
00:02:30,250 --> 00:02:33,720
The only difference is that these here are class methods,

56
00:02:33,720 --> 00:02:37,360
because we don't actually need to access self at all.

57
00:02:37,360 --> 00:02:38,820
So I'm going to make them class methods

58
00:02:38,820 --> 00:02:43,493
just for simplicity and this is fine in flask_restful.

59
00:02:44,810 --> 00:02:45,643
Okay.

60
00:02:46,810 --> 00:02:51,480
Now, in the get, all we need to do is take this user ID

61
00:02:51,480 --> 00:02:54,910
and find a user model and then return it.

62
00:02:54,910 --> 00:02:59,853
So we're gonna do user is user model, find by ID, user ID.

63
00:03:00,900 --> 00:03:03,030
And then we're gonna say, if not user,

64
00:03:03,030 --> 00:03:05,990
meaning we didn't find a user in our database,

65
00:03:05,990 --> 00:03:08,500
we're gonna return just a message saying,

66
00:03:08,500 --> 00:03:11,663
user not found.

67
00:03:12,620 --> 00:03:15,370
And we're gonna return in code 404.

68
00:03:15,370 --> 00:03:17,893
Otherwise, we're gonna return user.json.

69
00:03:18,840 --> 00:03:21,310
Remember, the user here is the user model

70
00:03:21,310 --> 00:03:23,357
we retrieved from the database.

71
00:03:23,357 --> 00:03:27,610
.Json is gonna call the json method of that user object

72
00:03:27,610 --> 00:03:28,803
and return that.

73
00:03:30,050 --> 00:03:31,760
Now, that's a good point to think about,

74
00:03:31,760 --> 00:03:34,670
whether we have a json method in our user.

75
00:03:34,670 --> 00:03:35,759
And the answer is,

76
00:03:35,759 --> 00:03:37,743
oh sorry opened this store by accident.

77
00:03:38,670 --> 00:03:41,960
The answer is we don't have a json method in our user,

78
00:03:41,960 --> 00:03:44,160
so we'll have to create it, and that's fine.

79
00:03:45,230 --> 00:03:46,470
Here, underneath in it,

80
00:03:46,470 --> 00:03:50,490
we are going to go ahead and create the json.

81
00:03:50,490 --> 00:03:55,363
And always going to return is an ID and the username.

82
00:03:56,940 --> 00:03:58,935
Don't return the password here,

83
00:03:58,935 --> 00:04:02,520
that would be a terribly insecure thing to do,

84
00:04:02,520 --> 00:04:04,450
so that will be a bad idea

85
00:04:04,450 --> 00:04:06,663
even though this is only for testing,

86
00:04:06,663 --> 00:04:09,510
just in case something does happen,

87
00:04:09,510 --> 00:04:13,840
better not return the password there, so, yeah.

88
00:04:13,840 --> 00:04:14,673
Let's close that.

89
00:04:14,673 --> 00:04:19,180
Now we have user.json and this endpoint should work.

90
00:04:19,180 --> 00:04:22,730
Of course we have to add our user class to our ab.py

91
00:04:22,730 --> 00:04:23,890
We're gonna do that in just a moment.

92
00:04:23,890 --> 00:04:28,860
But first, let's go ahead and implement our delete.

93
00:04:28,860 --> 00:04:31,022
It's gonna be fairly similar,

94
00:04:31,022 --> 00:04:34,800
we're gonna find by ID our user ID.

95
00:04:34,800 --> 00:04:36,940
If the user doesn't exist,

96
00:04:36,940 --> 00:04:39,100
we are going to return another 404

97
00:04:39,100 --> 00:04:41,970
saying that the user does not exist.

98
00:04:41,970 --> 00:04:45,990
Otherwise, we are going to user.delete.from_db

99
00:04:45,990 --> 00:04:48,140
and then we are going to return our message

100
00:04:49,230 --> 00:04:51,093
saying something like user deleted.

101
00:04:53,570 --> 00:04:56,710
Okay, now delete_from_db,

102
00:04:56,710 --> 00:04:58,273
is that present in our user?

103
00:05:00,210 --> 00:05:01,910
It seems like it is not,

104
00:05:01,910 --> 00:05:03,083
so we can do that.

105
00:05:04,057 --> 00:05:05,224
Delete_from_db

106
00:05:08,250 --> 00:05:11,800
or we can do db.session.delete self

107
00:05:13,010 --> 00:05:14,780
and db.session.commit

108
00:05:16,520 --> 00:05:19,363
Okay, now we have that in here.

109
00:05:20,210 --> 00:05:22,060
So once again, our Resource, all it's doing

110
00:05:22,060 --> 00:05:24,350
is going over and finding a user by ID

111
00:05:24,350 --> 00:05:28,640
which is coming over to our model and running this here.

112
00:05:28,640 --> 00:05:32,970
Gonna query all directories filtered by the userID

113
00:05:32,970 --> 00:05:35,310
being equal to the parameter we pass in.

114
00:05:35,310 --> 00:05:37,280
And it's gonna get the first user that matches that,

115
00:05:37,280 --> 00:05:38,630
hopefully there should only be one

116
00:05:38,630 --> 00:05:40,893
since our IDs should be unique.

117
00:05:42,240 --> 00:05:44,360
If it doesn't exist, it's gonna return user not found

118
00:05:44,360 --> 00:05:45,680
in both cases,

119
00:05:45,680 --> 00:05:48,171
otherwise it's either gonna return the user.json

120
00:05:48,171 --> 00:05:50,470
or it's gonna delete it from the database

121
00:05:50,470 --> 00:05:52,943
and return a message saying user deleted.

122
00:05:54,290 --> 00:05:57,600
Now we have to go over to our ab.py and make sure

123
00:05:57,600 --> 00:06:00,683
that we import the user.

124
00:06:02,690 --> 00:06:06,490
And the user resource there, from resources.user,

125
00:06:06,490 --> 00:06:07,840
and then of course down here,

126
00:06:07,840 --> 00:06:09,680
we also have to add it in.

127
00:06:09,680 --> 00:06:13,060
So we're gonna add resource user

128
00:06:13,060 --> 00:06:16,293
and it's gonna be slash user there.

129
00:06:17,440 --> 00:06:19,930
Now, remember how just like our stores,

130
00:06:19,930 --> 00:06:24,290
the user endpoints here are taking a user ID.

131
00:06:24,290 --> 00:06:25,830
So going back to our app.py,

132
00:06:25,830 --> 00:06:27,700
we also have to make sure to pass that in

133
00:06:27,700 --> 00:06:30,380
just like we did for items and stores.

134
00:06:30,380 --> 00:06:33,740
So we're gonna do int:user_id

135
00:06:33,740 --> 00:06:36,703
because our user IDs in our database are always numbers.

136
00:06:38,090 --> 00:06:38,923
Saving that,

137
00:06:38,923 --> 00:06:40,020
let's go over to our model,

138
00:06:40,020 --> 00:06:43,210
and remember, here the IDs they find as an integer,

139
00:06:43,210 --> 00:06:46,790
so the thing that we're passing over to our endpoints

140
00:06:46,790 --> 00:06:48,090
also has to be an integer.

141
00:06:50,380 --> 00:06:51,440
So that's it,

142
00:06:51,440 --> 00:06:52,680
we've set it up,

143
00:06:52,680 --> 00:06:55,820
now our Resource should be usable.

144
00:06:55,820 --> 00:07:00,230
Let's go ahead and have a look at how we can use it

145
00:07:00,230 --> 00:07:01,283
from within Postman.

