1
00:00:00,250 --> 00:00:01,900
- [Instructor] Hi and
welcome back to the course.

2
00:00:01,900 --> 00:00:02,733
In this video,

3
00:00:02,733 --> 00:00:06,082
we're going to be creating
our store resource.

4
00:00:06,082 --> 00:00:07,915
Now, the first thing to do

5
00:00:07,915 --> 00:00:09,683
is to go over the resources folder

6
00:00:09,683 --> 00:00:13,850
and create a new file which
I'm gonna call store.py.

7
00:00:15,008 --> 00:00:17,417
Now, there are two ways
that we could go about this.

8
00:00:17,417 --> 00:00:19,199
We could start everything from scratch

9
00:00:19,199 --> 00:00:21,575
and create each of the endpoints

10
00:00:21,575 --> 00:00:23,066
or we could copy and paste,

11
00:00:23,066 --> 00:00:25,983
and then change the item endpoints.

12
00:00:25,983 --> 00:00:27,638
For your benefit, I'm gonna go

13
00:00:27,638 --> 00:00:29,908
for creating everything from scratch.

14
00:00:29,908 --> 00:00:31,858
However, you can speed along,

15
00:00:31,858 --> 00:00:33,182
maybe change the video speed

16
00:00:33,182 --> 00:00:35,450
to one times five, if you prefer,

17
00:00:35,450 --> 00:00:37,024
just to go through things a bit faster

18
00:00:37,024 --> 00:00:41,292
since you're already familiar
with how all of these goes.

19
00:00:41,292 --> 00:00:43,366
The first thing to do is to import

20
00:00:43,366 --> 00:00:46,708
the things that we need
from the libraries.

21
00:00:46,708 --> 00:00:49,116
So, from flask_restful,

22
00:00:49,116 --> 00:00:51,449
we're gonna import Resource.

23
00:00:52,949 --> 00:00:55,524
And from the store model,

24
00:00:55,524 --> 00:00:57,524
we're gonna import that.

25
00:00:59,309 --> 00:01:02,642
So, from models.store import StoreModel.

26
00:01:03,741 --> 00:01:08,224
Then we're gonna create our
first resource, the Store,

27
00:01:08,224 --> 00:01:12,383
which is going to extend
the Resource class.

28
00:01:12,383 --> 00:01:14,207
And in Python, remember the way we extend

29
00:01:14,207 --> 00:01:16,582
is by putting between brackets

30
00:01:16,582 --> 00:01:19,082
directly after the class name.

31
00:01:20,851 --> 00:01:24,317
So what endpoints do we need for a store?

32
00:01:24,317 --> 00:01:26,666
A store only has a name,

33
00:01:26,666 --> 00:01:30,833
so I'm only gonna create get,
post, and delete endpoints.

34
00:01:32,332 --> 00:01:34,358
I'm not going to create a put endpoint

35
00:01:34,358 --> 00:01:36,832
because changing a store name

36
00:01:36,832 --> 00:01:39,582
inherently changes everything
about the store, I guess,

37
00:01:39,582 --> 00:01:42,999
so I'm not gonna allow editing of stores.

38
00:01:44,350 --> 00:01:48,499
So I'm gonna create a
get which takes in a name

39
00:01:48,499 --> 00:01:49,766
and I'm gonna put pass.

40
00:01:49,766 --> 00:01:51,599
That means do nothing,

41
00:01:51,599 --> 00:01:54,466
just to create the structure
of what we're making first

42
00:01:54,466 --> 00:01:57,799
and then we'll go implementing each one.

43
00:01:59,200 --> 00:02:00,117
So the post

44
00:02:02,324 --> 00:02:03,574
and the delete.

45
00:02:06,807 --> 00:02:10,158
So these are the three things
that we're gonna implement

46
00:02:10,158 --> 00:02:12,107
and down here, we're also gonna implement

47
00:02:12,107 --> 00:02:15,357
a StoreList(Resource).

48
00:02:18,124 --> 00:02:20,874
And we're gonna do that later on.

49
00:02:22,150 --> 00:02:24,124
The get method, what this has to do

50
00:02:24,124 --> 00:02:26,833
is return a specific store.

51
00:02:26,833 --> 00:02:30,425
So as we know, we can get a store by name

52
00:02:30,425 --> 00:02:34,425
using the StoreModel, so
StoreModel.find_by_name

53
00:02:36,499 --> 00:02:39,625
and that may return a store object

54
00:02:39,625 --> 00:02:41,908
or it may return none.

55
00:02:41,908 --> 00:02:44,325
So if the store was returned,

56
00:02:45,492 --> 00:02:48,017
then return store.json.

57
00:02:48,017 --> 00:02:52,849
Remember, that's also going
to return the items as well.

58
00:02:52,849 --> 00:02:54,799
And if we didn't return store.json,

59
00:02:54,799 --> 00:02:56,642
that means that the store was not found,

60
00:02:56,642 --> 00:03:00,674
so we can return a message
saying, "Store not found,"

61
00:03:00,674 --> 00:03:02,174
followed by a 404.

62
00:03:03,200 --> 00:03:06,349
Remember, what we're
returning here is a tuple,

63
00:03:06,349 --> 00:03:10,216
a tuple of dictionary
first and code after.

64
00:03:10,216 --> 00:03:14,208
And Flask-SQLAlchemy, as
well as Flask, see this,

65
00:03:14,208 --> 00:03:16,542
and say, "Okay, you wanna
return this in the body

66
00:03:16,542 --> 00:03:19,042
"and this in the status code."

67
00:03:20,091 --> 00:03:22,925
The default is 200, which is
what we're returning there,

68
00:03:22,925 --> 00:03:25,891
so we don't have to put it in.

69
00:03:25,891 --> 00:03:27,200
That's our get method.

70
00:03:27,200 --> 00:03:29,258
All that it does is it
looks in the database

71
00:03:29,258 --> 00:03:30,433
to see if it's there

72
00:03:30,433 --> 00:03:32,250
and then returns it if it is there

73
00:03:32,250 --> 00:03:35,500
or returns a store not found otherwise.

74
00:03:37,399 --> 00:03:40,158
The post is going to be fairly similar.

75
00:03:40,158 --> 00:03:41,017
The first thing we're gonna do

76
00:03:41,017 --> 00:03:44,274
is look to see if the
store already exists.

77
00:03:44,274 --> 00:03:47,024
So if the StoreModel.find_by_name

78
00:03:48,358 --> 00:03:50,016
already exists, then that means

79
00:03:50,016 --> 00:03:51,782
that we don't wanna create a new one

80
00:03:51,782 --> 00:03:53,591
because it's already there,

81
00:03:53,591 --> 00:03:56,174
so we're gonna return a message

82
00:03:58,133 --> 00:04:01,383
saying that the store is already there.

83
00:04:07,075 --> 00:04:09,700
Like that, and here, we're
gonna use the format method

84
00:04:09,700 --> 00:04:12,283
which you can do on any string.

85
00:04:14,007 --> 00:04:15,899
And what that does is that replaces

86
00:04:15,899 --> 00:04:20,066
this opening and closing curly
brace by the value there.

87
00:04:23,567 --> 00:04:28,350
And then we're gonna return
400 as the status code.

88
00:04:28,350 --> 00:04:30,475
Now, if that didn't happen,

89
00:04:30,475 --> 00:04:32,550
that means the store doesn't exist,

90
00:04:32,550 --> 00:04:34,717
so we can create a new one

91
00:04:37,850 --> 00:04:41,891
with a given name that is
passed in as an argument.

92
00:04:41,891 --> 00:04:45,641
And then we're going to
try to save the store

93
00:04:46,591 --> 00:04:47,875
to the database

94
00:04:47,875 --> 00:04:52,392
and if that fails, we're
going to return a wee message

95
00:04:52,392 --> 00:04:56,559
and you have to spell things
correctly, not like I do.

96
00:05:05,399 --> 00:05:07,641
And the code that we're
returning here is 500

97
00:05:07,641 --> 00:05:11,526
because we are not sure
exactly what the error was.

98
00:05:11,526 --> 00:05:14,150
Likely, it's some sort
of database problem,

99
00:05:14,150 --> 00:05:18,017
so it's not the user's
fault, so we'll return 500,

100
00:05:18,017 --> 00:05:19,949
which means internal server error.

101
00:05:19,949 --> 00:05:21,865
It's our fault.

102
00:05:21,865 --> 00:05:24,417
Now, if that didn't get returned,

103
00:05:24,417 --> 00:05:26,750
that means that we succeeded

104
00:05:27,817 --> 00:05:31,984
at saving to database, so
we can return store.json

105
00:05:33,699 --> 00:05:37,116
and 201, saying that it has been created.

106
00:05:37,975 --> 00:05:40,657
Finally, the delete method here

107
00:05:40,657 --> 00:05:44,824
is going to once again look
to see if the store exists

108
00:05:48,607 --> 00:05:52,024
and if it does, we're going to delete it.

109
00:05:53,375 --> 00:05:54,974
And if it doesn't, we're
not gonna do anything.

110
00:05:54,974 --> 00:05:58,066
So really, we're just gonna
say that it was deleted.

111
00:05:58,066 --> 00:06:00,649
And if the store doesn't exist,

112
00:06:01,732 --> 00:06:04,250
the end scenario is the same,

113
00:06:04,250 --> 00:06:06,541
the store doesn't exist.

114
00:06:06,541 --> 00:06:10,516
So the delete method, really
what the delete method causes,

115
00:06:10,516 --> 00:06:13,350
is for the store to stop existing.

116
00:06:13,350 --> 00:06:16,324
If the store didn't exist to begin with,

117
00:06:16,324 --> 00:06:17,950
the user doesn't really care.

118
00:06:17,950 --> 00:06:22,478
It doesn't care that the
store wasn't there already.

119
00:06:22,478 --> 00:06:24,274
It only cares that it has been deleted.

120
00:06:24,274 --> 00:06:26,024
It's no longer there.

121
00:06:28,642 --> 00:06:31,149
And the StoreList as
well, for good measure,

122
00:06:31,149 --> 00:06:35,692
is going to be very
similar to the item list.

123
00:06:35,692 --> 00:06:39,742
All that is gonna return
is a dictionary of stores

124
00:06:39,742 --> 00:06:43,742
where the stores each
have to be their own JSON.

125
00:06:44,899 --> 00:06:47,307
So go ahead and implement this yourselves,

126
00:06:47,307 --> 00:06:48,567
if you know how to do this,

127
00:06:48,567 --> 00:06:50,665
or look at the item list resource

128
00:06:50,665 --> 00:06:53,248
if you need some more guidance.

129
00:06:57,249 --> 00:06:58,158
Hopefully you got that.

130
00:06:58,158 --> 00:06:59,275
There are two ways to do it.

131
00:06:59,275 --> 00:07:02,192
Once again, one is to do store.json

132
00:07:03,282 --> 00:07:06,032
for store in StoreModel.query.all

133
00:07:09,683 --> 00:07:12,082
or you can use the list and the map

134
00:07:12,082 --> 00:07:16,400
as we have seen in the
past, if you prefer.

135
00:07:16,400 --> 00:07:18,342
That's everything for our resources.

136
00:07:18,342 --> 00:07:20,155
Now we've got our two resources.

137
00:07:20,155 --> 00:07:22,807
And as you can see, now that
we know what we're doing,

138
00:07:22,807 --> 00:07:25,992
it's fairly quick to
implement these resources

139
00:07:25,992 --> 00:07:29,858
because we know exactly what
each endpoint is meant to do.

140
00:07:29,858 --> 00:07:32,917
Now we're gonna go over to the app

141
00:07:32,917 --> 00:07:37,084
and we're going to import the
resource from resources.store

142
00:07:38,016 --> 00:07:40,266
import Store and StoreList.

143
00:07:41,183 --> 00:07:42,408
And then we're going to make sure

144
00:07:42,408 --> 00:07:44,732
to add the resources to the API,

145
00:07:44,732 --> 00:07:46,399
so api.add_resource,

146
00:07:47,941 --> 00:07:49,941
Store, and the endpoint for our Store

147
00:07:49,941 --> 00:07:52,524
is gonna be store and the name.

148
00:07:53,499 --> 00:07:54,332
And on the bottom,

149
00:07:54,332 --> 00:07:57,499
we're gonna add also api.add_resource,

150
00:07:59,491 --> 00:08:03,658
StoreList, and that's gonna be /stores.

151
00:08:05,365 --> 00:08:07,817
Something important to remember

152
00:08:07,817 --> 00:08:11,567
is that SQLAlchemy
creates the tables for you

153
00:08:12,625 --> 00:08:17,266
and so we have to delete
data.db there, so it does that,

154
00:08:17,266 --> 00:08:22,142
but also it only creates
the tables that it sees.

155
00:08:22,142 --> 00:08:23,809
Now that's important

156
00:08:25,567 --> 00:08:27,533
because it goes through imports.

157
00:08:27,533 --> 00:08:30,749
So here, for example,
we're importing Store

158
00:08:30,749 --> 00:08:33,767
and then that goes over to the StoreModel

159
00:08:33,767 --> 00:08:35,640
and the model, sorry, the Store(Resource),

160
00:08:35,640 --> 00:08:39,192
and the Store(Resource)
imports the StoreModel.

161
00:08:39,192 --> 00:08:43,475
And the StoreModel has the table
and the column definitions.

162
00:08:43,475 --> 00:08:46,450
So as you can see, there is a way for us

163
00:08:46,450 --> 00:08:49,750
to have imported the StoreModel.

164
00:08:49,750 --> 00:08:51,833
If we don't import Store,

165
00:08:53,474 --> 00:08:55,709
the table for stores won't get created.

166
00:08:55,709 --> 00:08:57,966
Well, if we don't import
really any of this.

167
00:08:57,966 --> 00:08:59,858
The table for stores won't get created

168
00:08:59,858 --> 00:09:02,266
because SQLAlchemy will not be able to see

169
00:09:02,266 --> 00:09:05,574
that that is a thing
that exists in our app.

170
00:09:05,574 --> 00:09:08,508
So it's important that
you import the things

171
00:09:08,508 --> 00:09:12,550
that you want SQLAlchemy
to be able to know about.

172
00:09:12,550 --> 00:09:16,150
And in some cases, if
you don't have a resource

173
00:09:16,150 --> 00:09:16,983
associated with something

174
00:09:16,983 --> 00:09:18,591
that you wanna store in a database,

175
00:09:18,591 --> 00:09:20,699
you can just import a model directly

176
00:09:20,699 --> 00:09:24,366
and then SQLAlchemy
will see that it exists.

177
00:09:25,675 --> 00:09:27,524
So that's everything for this video.

178
00:09:27,524 --> 00:09:29,066
In the next one, we're going to be looking

179
00:09:29,066 --> 00:09:31,108
at testing this entire thing

180
00:09:31,108 --> 00:09:32,191
and so, yeah.

181
00:09:33,599 --> 00:09:34,775
As you can see, it's really easy

182
00:09:34,775 --> 00:09:38,341
to add new models and
relationships between them

183
00:09:38,341 --> 00:09:42,500
and that's part of the power
that SQLAlchemy gives us.

184
00:09:42,500 --> 00:09:43,882
So that's everything for this video

185
00:09:43,882 --> 00:09:46,067
and I'll see you on the next one.

