Repository navigation
Expand file tree
/
Copy pathcontroller.py
More file actions
310 lines (226 loc) · 8.66 KB
/
Copy pathcontroller.py
File metadata and controls
310 lines (226 loc) · 8.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"""
.. module:: controller
:synopsis: All endpoints of the Teacher API are defined here
.. moduleauthor:: Rich Yap <github.com/richyap13>
"""
from flask import request, jsonify, abort, make_response, Blueprint
from teacherAPI.database import db_session
from teacherAPI.models import Teacher
from sqlalchemy import exc
import os
teacher_api = Blueprint('teacher_api', __name__)
@teacher_api.route('/', methods=['GET'])
def index():
"""
**Get List of Teachers**
This function allows users to get a list of teachers and the subjects they're teaching.
:return: teacher's information in json and http status code
- Example::
curl -X GET http://localhost:5000/ -H 'cache-control: no-cache' -H 'content-type: application/json'
- Expected Success Response::
HTTP Status Code: 200
{
"Teachers": [
{
"id": 1,
"name": "Jane Vargas",
"subject": "Science"
},
{
"id": 2,
"name": "John Doe",
"subject": "Math"
},
{
"id": 3,
"name": "Jenny Lisa",
"subject": "English"
}
]
}
"""
teachers = Teacher.query.all()
return make_response(jsonify(Teachers=[Teacher.serialize() for Teacher in teachers]), 200)
@teacher_api.route('/', methods=['POST'])
def create_teacher():
"""
**Create Teacher Record**
This function allows user to create(post) a teacher record.
:return: teacher's information added by the user in json and http status code
- Example::
curl -X POST http://localhost:5000/ -H 'cache-control: no-cache' -H 'content-type: application/json' \
-d '{
"name": "Mary Rose",
"subject": "Biology"
}'
- Expected Success Response::
HTTP Status Code: 201
{
"name": "Mary Rose",
"subject": "Biology"
}
- Expected Fail Response::
HTTP Status Code: 400
{'error': 'Duplicate teacher name'}
"""
if not request.json:
abort(400)
content = request.get_json()
if type(content['name']) != str:
return make_response(jsonify({'error':'Teacher name should be a string'}), 400)
try:
teacher_temp = Teacher(name=content['name'],
subject=content['subject'])
db_session.add(teacher_temp)
db_session.commit()
return jsonify(content), 201
except exc.IntegrityError as e:
return make_response(jsonify({'error': 'Duplicate teacher name'}), 400)
@teacher_api.route('/<int:teacher_id>', methods=['GET'])
def get_teacher(teacher_id):
"""
**Get information of a specific teacher**
This function allows user to get a specific teacher's information through their teacher_id.
:param teacher_id: id of the teacher
:type teacher_id: int
:return: teacher's information accessed by user in json and http status code
- Example::
curl -X GET http://127.0.0.1:5000/1 -H 'cache-control: no-cache' -H 'content-type: application/json'
- Expected Success Response::
HTTP Status Code: 200
{
"Teacher": {
"id": 1,
"name": "Jane Vargas",
"subject": "Science"
}
}
- Expected Fail Response::
HTTP Status Code: 404
{'error': 'Not found'}
"""
teachers = Teacher.query.all()
teacher = [teacher for teacher in teachers if teacher.id == teacher_id]
if len(teacher) == 0:
not_found()
return make_response(jsonify(Teacher=Teacher.serialize(teacher[0])), 200)
@teacher_api.route('/<int:teacher_id>', methods=['PUT'])
def update_teacher(teacher_id):
"""
**Update Information of a Specific Teacher Record**
This function allows user to update a specific teacher's information through their teacher_id.
:param teacher_id: id of the teacher
:type teacher_id: int
:return: teacher's information updated by user in json and http status code
- Example::
curl -X PUT http://localhost:5000/1 -H 'cache-control: no-cache' -H 'content-type: application/json' \
-d '{
"name": "Jane Cruz",
"subject": "Science"
}'
- Expected Success Response::
HTTP Status Code: 200
{
"name": "Jane Cruz",
"subject": "Science"
}
- Expected Fail Response::
HTTP Status Code: 404
{'error': 'Not found'}
or
HTTP Status Code: 404
{'error': 'Duplicate teacher name'}
"""
teachers = Teacher.query.all()
teacher = [teacher for teacher in teachers if teacher.id == teacher_id]
if len(teacher) == 0:
not_found()
if 'name' in request.json and type(request.json['name']) != str:
return make_response(jsonify({'error': 'Teacher name not a string'}), 400)
if 'subject' in request.json and type(request.json['subject']) != str:
return make_response(jsonify({'error': 'Subject not a string'}), 400)
content = request.get_json()
# updating the requested teacher record
try:
queried_teacher = Teacher.query.get(teacher_id)
queried_teacher.name = content['name']
queried_teacher.subject = content['subject']
db_session.commit()
return make_response(jsonify(content), 200)
except exc.IntegrityError as e:
return make_response(jsonify({'error': 'Duplicate teacher name'}), 400)
@teacher_api.route('/<int:teacher_id>', methods=['DELETE'])
def delete_teacher(teacher_id):
"""
**Delete Teacher Record**
This function allows user to delete a teacher record.
:param teacher_id: id of the teacher
:type teacher_id: int
:return: delete status in json and http status code
- Example::
curl -X DELETE http://127.0.0.1:5000/4 -H 'cache-control: no-cache' -H 'content-type: application/json'
- Expected Success Response::
HTTP Status Code: 200
{
"Delete": true
}
- Expected Fail Response::
HTTP Status Code: 404
{'error': 'Not found'}
"""
teachers = Teacher.query.all()
teacher = [teacher for teacher in teachers if teacher.id == teacher_id]
if len(teacher) == 0:
not_found()
Teacher.query.filter_by(id=teacher_id).delete()
db_session.commit()
return make_response(jsonify({'Delete': True}), 200)
@teacher_api.route('/search', methods=['POST'])
def search():
"""
**Search Teacher Records**
This function allows user to search for teacher/s through substring search of teachers' names.
:return: searched teachers in json and http status code
- Example::
curl -X POST http://localhost:5000/search -H 'cache-control: no-cache' -H 'content-type: application/json' \
-d '{
"value": "J"
}'
- Expected Success Response::
HTTP Status Code: 200
{
"Teachers": [
{
"id": 1,
"name": "Jane Cruz",
"subject": "Science"
},
{
"id": 2,
"name": "John Doe",
"subject": "Math"
},
{
"id": 3,
"name": "Jenny Lisa",
"subject": "English"
}
]
}
"""
if not request.json:
abort(400)
if 'value' in request.json and type(request.json['value']) is not str:
abort(400)
content = request.get_json()
teachers = Teacher.query.filter(Teacher.name.like('%' + content['value']+ '%'))
return make_response(jsonify(Teachers=[Teacher.serialize() for Teacher in teachers]), 200)
# Act as an error handler when a page is not found
@teacher_api.errorhandler(404)
def not_found():
"""
**Error handler**
This function returns a not found error in json when called.
:return: not found error in json
"""
return make_response(jsonify({'error': 'Not found'}), 404)