1
00:00:00,244 --> 00:00:02,242
- [Instructor] Hi and
welcome back to the course!

2
00:00:02,242 --> 00:00:04,516
In this video we're going to be writing

3
00:00:04,516 --> 00:00:06,683
our items to the database.

4
00:00:07,572 --> 00:00:08,989
The first thing to do, however,

5
00:00:08,989 --> 00:00:10,499
I wanted to show you something

6
00:00:10,499 --> 00:00:13,719
that may become important in the future.

7
00:00:13,719 --> 00:00:17,886
And that is the way that
Python does the importing.

8
00:00:20,750 --> 00:00:24,265
See, when we import a file or, indeed,

9
00:00:24,265 --> 00:00:27,932
something from a file,
Python runs the file.

10
00:00:28,877 --> 00:00:31,314
So for example, here we are importing

11
00:00:31,314 --> 00:00:33,060
user register from user.

12
00:00:33,060 --> 00:00:34,851
So the first thing that Python does

13
00:00:34,851 --> 00:00:36,986
is it's going to run the user file

14
00:00:36,986 --> 00:00:39,065
and make sure that everything is in order.

15
00:00:39,065 --> 00:00:42,565
So it goes to the user file and it creates

16
00:00:43,581 --> 00:00:47,211
the class user and the class user register

17
00:00:47,211 --> 00:00:50,150
and it looks through the
methods and that sort of stuff.

18
00:00:50,150 --> 00:00:52,324
It doesn't actually run the methods.

19
00:00:52,324 --> 00:00:54,953
But, for example, if we
have a full stop there,

20
00:00:54,953 --> 00:00:58,453
it will identify that this is not correct.

21
00:00:59,455 --> 00:01:01,488
If we never import user, then it would not

22
00:01:01,488 --> 00:01:03,417
identify that that's not correct.

23
00:01:03,417 --> 00:01:05,477
It wouldn't run the file.

24
00:01:05,477 --> 00:01:07,470
So that's the first thing it does.

25
00:01:07,470 --> 00:01:08,997
It runs the file through.

26
00:01:08,997 --> 00:01:10,513
And if it's got any classes,

27
00:01:10,513 --> 00:01:12,531
then it verifies if the class is correct,

28
00:01:12,531 --> 00:01:14,058
if it's got any method,
it verifies the methods

29
00:01:14,058 --> 00:01:16,742
are correct, but it
doesn't actually run them.

30
00:01:16,742 --> 00:01:19,325
However, if it has a statement,

31
00:01:20,265 --> 00:01:24,286
such as a print statement
here, print "hello",

32
00:01:24,286 --> 00:01:27,270
every time we import this file,

33
00:01:27,270 --> 00:01:29,516
it runs that statement.

34
00:01:29,516 --> 00:01:32,349
And now that's interesting because

35
00:01:34,221 --> 00:01:37,531
imagine we have our user.py file here

36
00:01:37,531 --> 00:01:41,080
and we've made a lot of
code and at some point,

37
00:01:41,080 --> 00:01:44,574
we want to import something from app.py.

38
00:01:44,574 --> 00:01:46,839
For example, just for
the sake of imagination,

39
00:01:46,839 --> 00:01:49,256
imagine we want to import app

40
00:01:50,121 --> 00:01:52,325
so that see can get the secret key.

41
00:01:52,325 --> 00:01:53,707
Okay, not something that we're gonna do,

42
00:01:53,707 --> 00:01:55,423
but just imagine we have something here

43
00:01:55,423 --> 00:01:57,173
that we wanna import.

44
00:01:58,698 --> 00:02:02,375
All of these things would already exist

45
00:02:02,375 --> 00:02:04,792
if we've run the app.py file.

46
00:02:05,728 --> 00:02:08,504
For example, nowadays we do python app.py

47
00:02:08,504 --> 00:02:11,558
and that runs the app.py file.

48
00:02:11,558 --> 00:02:15,225
But if we run the user.py
file, for example,

49
00:02:17,390 --> 00:02:20,390
and that imports app, then on import

50
00:02:22,463 --> 00:02:26,044
we would run the
following statements here.

51
00:02:26,044 --> 00:02:27,877
Now, that may be okay.

52
00:02:29,457 --> 00:02:33,290
Because creating a flask
app is not a problem.

53
00:02:34,619 --> 00:02:37,433
Creating an API is not a problem.

54
00:02:37,433 --> 00:02:41,527
What is a problem, however, is running it.

55
00:02:41,527 --> 00:02:45,694
If we import something from
app.py and we want the app,

56
00:02:47,634 --> 00:02:50,742
then we can import the app
and everything is in order.

57
00:02:50,742 --> 00:02:52,731
We just create an object of type flask

58
00:02:52,731 --> 00:02:54,669
and then we import it.

59
00:02:54,669 --> 00:02:57,192
However, if we import from app,

60
00:02:57,192 --> 00:02:59,383
we would always run app.run,

61
00:02:59,383 --> 00:03:01,291
which means that on import

62
00:03:01,291 --> 00:03:03,439
we would start our flask application.

63
00:03:03,439 --> 00:03:06,778
And that's not really
what we want on an import.

64
00:03:06,778 --> 00:03:10,090
So on import, we don't want to do things

65
00:03:10,090 --> 00:03:14,605
that we would only wanna
do when we run the file.

66
00:03:14,605 --> 00:03:17,931
So here we only want to run the flask app

67
00:03:17,931 --> 00:03:19,931
if we say python app.py.

68
00:03:21,603 --> 00:03:22,904
If we are importing app,

69
00:03:22,904 --> 00:03:24,887
we don't wanna run the flask app

70
00:03:24,887 --> 00:03:28,005
because likely is that
we don't want to do that.

71
00:03:28,005 --> 00:03:29,855
So hopefully this makes a bit of sense.

72
00:03:29,855 --> 00:03:31,756
And fortunately, there
is a way that we can

73
00:03:31,756 --> 00:03:33,948
prevent the flask app from running

74
00:03:33,948 --> 00:03:36,387
if we were to import something from app.

75
00:03:36,387 --> 00:03:40,554
And that is: if
__name__=='__main__' and do that.

76
00:03:42,099 --> 00:03:43,992
I think we've looked
at this syntax before.

77
00:03:43,992 --> 00:03:48,159
I just wanted to explain
that this is why we do this.

78
00:03:49,678 --> 00:03:52,595
If we import app, then that creates

79
00:03:53,610 --> 00:03:56,045
the flask app, or if that already exists

80
00:03:56,045 --> 00:03:58,121
because Python's already run it,

81
00:03:58,121 --> 00:04:00,477
then it just gets the app from there.

82
00:04:00,477 --> 00:04:03,151
But if we import, then we would always run

83
00:04:03,151 --> 00:04:05,579
app.run which is not ideal.

84
00:04:05,579 --> 00:04:08,487
So this circumvents that because

85
00:04:08,487 --> 00:04:11,070
whenever you run a Python file,

86
00:04:12,996 --> 00:04:15,762
for example, if we do python app.py,

87
00:04:15,762 --> 00:04:19,696
Python assigns a special
name to the file we run

88
00:04:19,696 --> 00:04:22,446
and that name is always __main__.

89
00:04:23,715 --> 00:04:27,736
So if we run the app.py
file, that is main.

90
00:04:27,736 --> 00:04:31,569
If we run the user.py
file, then this is main.

91
00:04:32,466 --> 00:04:35,707
So only the file that you run is main

92
00:04:35,707 --> 00:04:37,888
and therefore, if the name is main,

93
00:04:37,888 --> 00:04:40,090
we know that we have run this file,

94
00:04:40,090 --> 00:04:42,947
and therefore we want
to start the flask app.

95
00:04:42,947 --> 00:04:45,446
If it's not main, then that means

96
00:04:45,446 --> 00:04:48,128
that we have imported
this file from elsewhere

97
00:04:48,128 --> 00:04:51,435
and therefore we don't
want to run the flask app.

98
00:04:51,435 --> 00:04:54,812
Okay, hopefully, this all makes sense.

99
00:04:54,812 --> 00:04:57,484
Now, leaving that aside, let's go

100
00:04:57,484 --> 00:04:59,823
into the post of our items.

101
00:04:59,823 --> 00:05:02,392
I'm gonna open item.py
and then we're gonna

102
00:05:02,392 --> 00:05:05,092
navigate down to the post method

103
00:05:05,092 --> 00:05:07,563
and we are gonna implement this, okay?

104
00:05:07,563 --> 00:05:10,749
So sorry for that sort of deviation,

105
00:05:10,749 --> 00:05:14,053
but I thought it was worth mentioning.

106
00:05:14,053 --> 00:05:16,571
Okay, so the first thing that we've got

107
00:05:16,571 --> 00:05:19,530
is something here which,
in the post method,

108
00:05:19,530 --> 00:05:22,639
which checks if an item already exists.

109
00:05:22,639 --> 00:05:23,696
Okay.

110
00:05:23,696 --> 00:05:25,262
So we should do that as well

111
00:05:25,262 --> 00:05:27,403
when we rewrite the method.

112
00:05:27,403 --> 00:05:30,236
This filters through the items list,

113
00:05:30,236 --> 00:05:32,472
which no longer exists,
now we need to make sure

114
00:05:32,472 --> 00:05:35,874
to check the database to see
if an item already exists.

115
00:05:35,874 --> 00:05:37,389
So how do we do that?

116
00:05:37,389 --> 00:05:41,701
Well, we've got the get
method which does that.

117
00:05:41,701 --> 00:05:43,809
However we cannot call the get method.

118
00:05:43,809 --> 00:05:45,976
We cannot say if get name,

119
00:05:49,258 --> 00:05:52,833
or rather if self.get
name because self.get

120
00:05:52,833 --> 00:05:57,341
requires a JWT token and
the post method does not.

121
00:05:57,341 --> 00:05:59,843
So then we would have a bunch of problems.

122
00:05:59,843 --> 00:06:01,646
So what we're gonna do
instead is we're going

123
00:06:01,646 --> 00:06:04,986
to extract some code from the get method

124
00:06:04,986 --> 00:06:08,275
into a separate method and then use that.

125
00:06:08,275 --> 00:06:11,211
So I'm gonna create a class method

126
00:06:11,211 --> 00:06:13,589
that is going to be find_by_name,

127
00:06:13,589 --> 00:06:16,799
and that's gonna be
taking cls and the name.

128
00:06:16,799 --> 00:06:19,830
Cls because it's a class
method with double s,

129
00:06:19,830 --> 00:06:22,052
and the name because that's
what we're gonna be finding

130
00:06:22,052 --> 00:06:24,510
and the find_my_name
method, all it's gonna do

131
00:06:24,510 --> 00:06:28,260
is it is going to do
all of this stuff there.

132
00:06:30,849 --> 00:06:32,412
So it's going to connect to the database,

133
00:06:32,412 --> 00:06:36,527
get a cursor, select
the item, run the query,

134
00:06:36,527 --> 00:06:38,956
fetch a row, close the connection,

135
00:06:38,956 --> 00:06:40,143
and then if the row exists,

136
00:06:40,143 --> 00:06:42,970
it's going to return the JSON.

137
00:06:42,970 --> 00:06:45,340
Now, the get method doesn't
have to be duplicated.

138
00:06:45,340 --> 00:06:48,502
So instead we can just say that the item

139
00:06:48,502 --> 00:06:50,252
is self.find_by_name,

140
00:06:53,482 --> 00:06:57,181
with the name and if item, return item.

141
00:06:57,181 --> 00:07:00,431
And if not item, then return not found.

142
00:07:01,338 --> 00:07:03,263
So now what we've done is we've extracted

143
00:07:03,263 --> 00:07:04,948
the useful code, essentially the code

144
00:07:04,948 --> 00:07:07,501
that doesn't deal with returning things.

145
00:07:07,501 --> 00:07:09,892
We've extracted that
into a separate method

146
00:07:09,892 --> 00:07:11,610
which means that we can now call it

147
00:07:11,610 --> 00:07:13,447
from our post method.

148
00:07:13,447 --> 00:07:17,614
Instead of if self.get, we
say if self.find_by_name.

149
00:07:18,960 --> 00:07:22,143
And I do appreciate that
this is a class method

150
00:07:22,143 --> 00:07:25,810
and not sort of a self
method, like post is,

151
00:07:26,666 --> 00:07:29,079
but nevertheless we can
still call it using self.,

152
00:07:29,079 --> 00:07:32,996
or we could call it using
Item., if you prefer.

153
00:07:33,994 --> 00:07:37,046
It doesn't really matter, either is fine.

154
00:07:37,046 --> 00:07:37,879
Okay.

155
00:07:39,485 --> 00:07:40,734
So there we go.

156
00:07:40,734 --> 00:07:43,880
Now we are able to return that an item

157
00:07:43,880 --> 00:07:46,951
with the name already
exists, if it already exists.

158
00:07:46,951 --> 00:07:48,039
Okay.

159
00:07:48,039 --> 00:07:50,689
Now let's go below and
what we have to change

160
00:07:50,689 --> 00:07:52,439
is this items.append.

161
00:07:54,094 --> 00:07:56,585
Now, items.append is only useful

162
00:07:56,585 --> 00:07:59,677
if we have a list that we can append to.

163
00:07:59,677 --> 00:08:03,266
Now we are gonna have to
write it to the database.

164
00:08:03,266 --> 00:08:06,143
So what to do is you guessed it.

165
00:08:06,143 --> 00:08:08,143
First of all we connect,

166
00:08:09,431 --> 00:08:12,014
data.db and we create a cursor.

167
00:08:13,714 --> 00:08:15,734
I'm sure you remember how
to do all this stuff by now.

168
00:08:15,734 --> 00:08:17,221
Then we create a query which is gonna be

169
00:08:17,221 --> 00:08:22,082
insert into items the values,
question mark, question mark,

170
00:08:22,082 --> 00:08:25,499
but this is gonna the name and the price.

171
00:08:26,398 --> 00:08:29,559
Then we are going to execute the query

172
00:08:29,559 --> 00:08:31,815
with the item's name and price.

173
00:08:31,815 --> 00:08:33,474
We've defined the item up here,

174
00:08:33,474 --> 00:08:35,154
just as we had in the last section,

175
00:08:35,154 --> 00:08:37,016
so we're just gonna use that item's name

176
00:08:37,016 --> 00:08:38,766
and the item's price.

177
00:08:43,002 --> 00:08:44,513
Okay.

178
00:08:44,513 --> 00:08:48,511
And once we've inserted the item there,

179
00:08:48,511 --> 00:08:50,721
all we have to do is close it,

180
00:08:50,721 --> 00:08:53,138
well, commit it and close it.

181
00:08:55,236 --> 00:08:59,701
And then we return the item
which is this JSON here.

182
00:08:59,701 --> 00:09:02,680
Remember we cannot return item objects

183
00:09:02,680 --> 00:09:05,993
or user objects, we
always have to return JSON

184
00:09:05,993 --> 00:09:09,493
and the item here is a piece of JSON data.

185
00:09:11,367 --> 00:09:13,323
Okay, so that's our item.

186
00:09:13,323 --> 00:09:15,248
Let's go ahead and run this.

187
00:09:15,248 --> 00:09:16,919
So, the first thing I'm gonna do actually

188
00:09:16,919 --> 00:09:19,533
is go to the create_tables file

189
00:09:19,533 --> 00:09:21,813
and remove the test data from there.

190
00:09:21,813 --> 00:09:24,280
So don't insert test data anymore,

191
00:09:24,280 --> 00:09:25,536
we don't need the test data

192
00:09:25,536 --> 00:09:28,291
since we are now able to insert data.

193
00:09:28,291 --> 00:09:31,136
Now let's go over to the terminal.

194
00:09:31,136 --> 00:09:33,504
For me this restarts automatically.

195
00:09:33,504 --> 00:09:37,221
But if it doesn't, then you
can restart it yourself.

196
00:09:37,221 --> 00:09:40,982
What I'm gonna do is
remove the data.db file.

197
00:09:40,982 --> 00:09:43,307
You can do that through
Atom if you prefer,

198
00:09:43,307 --> 00:09:45,120
just right click and remove,

199
00:09:45,120 --> 00:09:47,409
or you can do it on the terminal like so,

200
00:09:47,409 --> 00:09:50,399
and then run the app again.

201
00:09:50,399 --> 00:09:52,887
Now remember, because we've run the app

202
00:09:52,887 --> 00:09:56,887
that is __main__, so it
starts the flask server.

203
00:09:58,506 --> 00:10:01,359
And then let's go over to Postman

204
00:10:01,359 --> 00:10:03,929
and I am going to close that,

205
00:10:03,929 --> 00:10:05,595
and the first thing that
we have to do, remember,

206
00:10:05,595 --> 00:10:08,211
is register because we
don't have any users

207
00:10:08,211 --> 00:10:10,544
so I'm gonna register there.

208
00:10:11,522 --> 00:10:14,830
And of course, if you delete, apologies.

209
00:10:14,830 --> 00:10:17,935
We have deleted data.db so we also have

210
00:10:17,935 --> 00:10:20,136
to create the tables again.

211
00:10:20,136 --> 00:10:20,969
My bad.

212
00:10:21,923 --> 00:10:23,517
Easy to forget to do that.

213
00:10:23,517 --> 00:10:25,497
Just remember that if it tells you

214
00:10:25,497 --> 00:10:29,026
sqlite3.OperationalError,
no such table users,

215
00:10:29,026 --> 00:10:31,890
you know that you forgot
to create the tables.

216
00:10:31,890 --> 00:10:33,512
Okay, let's try that again.

217
00:10:33,512 --> 00:10:35,553
We create the user.

218
00:10:35,553 --> 00:10:38,782
Then let's go over to auth and sign up,

219
00:10:38,782 --> 00:10:42,508
sing in rather, copy the access token,

220
00:10:42,508 --> 00:10:46,175
create a new item which
I'm gonna call test,

221
00:10:48,275 --> 00:10:50,777
and the item is there created.

222
00:10:50,777 --> 00:10:53,152
This endpoint doesn't need the JW Token

223
00:10:53,152 --> 00:10:56,319
but just to verify we are going to get

224
00:10:57,751 --> 00:11:00,834
the item out and indeed it comes out.

225
00:11:01,746 --> 00:11:04,018
So now we're able to write data

226
00:11:04,018 --> 00:11:06,731
to the database as well,
which we already were

227
00:11:06,731 --> 00:11:09,268
with the user but
nevertheless just going over

228
00:11:09,268 --> 00:11:11,051
through the motions once again.

229
00:11:11,051 --> 00:11:12,989
So what we're doing first is we check

230
00:11:12,989 --> 00:11:15,643
the item's not already in the database

231
00:11:15,643 --> 00:11:18,522
and then we parse the
data using the parser,

232
00:11:18,522 --> 00:11:21,313
which, as you know, checks for price.

233
00:11:21,313 --> 00:11:23,636
We create a JSON of the item and then

234
00:11:23,636 --> 00:11:26,791
we use that to insert into the database.

235
00:11:26,791 --> 00:11:28,307
And finally we return the item there.

236
00:11:28,307 --> 00:11:30,531
So there's nothing magical going on here

237
00:11:30,531 --> 00:11:33,990
and in fact many, many, many REST APIs

238
00:11:33,990 --> 00:11:36,692
like this one are just this,

239
00:11:36,692 --> 00:11:40,859
are what's called CRUD APIs:
create, read, update, delete.

240
00:11:41,844 --> 00:11:45,562
That's what this item
resource does essentially.

241
00:11:45,562 --> 00:11:49,729
Create, read, update is down
here, the put, and delete.

242
00:11:51,758 --> 00:11:53,257
And it's really this simple.

243
00:11:53,257 --> 00:11:55,205
I'm sure you could go ahead and implement

244
00:11:55,205 --> 00:11:57,791
and delete and put yourselves as well.

245
00:11:57,791 --> 00:11:59,918
There's really no much
magic going into it,

246
00:11:59,918 --> 00:12:02,942
just a couple of SQL statements.

247
00:12:02,942 --> 00:12:04,212
And that's it.

248
00:12:04,212 --> 00:12:07,505
And if you have stores as well as items,

249
00:12:07,505 --> 00:12:09,467
then you would also have a store resource

250
00:12:09,467 --> 00:12:11,334
that has these four methods,

251
00:12:11,334 --> 00:12:14,625
and they're all very, very similar.

252
00:12:14,625 --> 00:12:16,980
You can also get much
more complicated APIs,

253
00:12:16,980 --> 00:12:20,297
of course, that do other
things, like send emails

254
00:12:20,297 --> 00:12:23,880
or look for flights
even, things like that.

255
00:12:25,037 --> 00:12:27,812
But in many cases you will be creating

256
00:12:27,812 --> 00:12:30,792
CRUD APIs like this one
and, as you can see,

257
00:12:30,792 --> 00:12:32,790
there's no magic going on.

258
00:12:32,790 --> 00:12:35,331
It's just a matter of
interacting with the database

259
00:12:35,331 --> 00:12:38,464
and putting things in
it or taking things out.

260
00:12:38,464 --> 00:12:41,011
So it's not too complicated.

261
00:12:41,011 --> 00:12:44,779
It's just a matter of writing
the code to do it really.

262
00:12:44,779 --> 00:12:48,158
But there's nothing
difficult inherently in it.

263
00:12:48,158 --> 00:12:49,767
Anyway, I hope you feel that way as well

264
00:12:49,767 --> 00:12:53,283
because if not, that
would be rather awkward.

265
00:12:53,283 --> 00:12:57,355
So if you do think that this is difficult,

266
00:12:57,355 --> 00:13:01,114
do ask a question because the process

267
00:13:01,114 --> 00:13:04,942
for inserting, retrieving
data and the interactions

268
00:13:04,942 --> 00:13:08,812
are inherently simple,
there's not much going on.

269
00:13:08,812 --> 00:13:11,346
So if you think it is,
then do ask a question,

270
00:13:11,346 --> 00:13:14,472
I'm always eager to help out.

271
00:13:14,472 --> 00:13:15,619
And that's it for this video.

272
00:13:15,619 --> 00:13:18,182
So I'll see you in the very next one.

