1
1
import os
2
- from flask import Flask , request , abort , jsonify
2
+ from flask import Flask , request , abort , jsonify , Response
3
3
from flask_sqlalchemy import SQLAlchemy
4
4
from flask_cors import CORS
5
5
import random
6
+ from sqlalchemy import delete
6
7
7
- from models import setup_db , Question , Category
8
+ from models import db , setup_db , Question , Category
8
9
9
10
QUESTIONS_PER_PAGE = 10
10
11
@@ -46,13 +47,12 @@ def get_categories():
46
47
start = (page - 1 ) * 10
47
48
end = start + 10
48
49
49
- categories = Category .query .all ()
50
- formatted_categories = [category .format () for category in categories ]
50
+ categories = get_all_formatted_category ()
51
51
52
52
return jsonify ({
53
53
'success' : True ,
54
- 'categories' : formatted_categories [start :end ],
55
- 'total_categories' : len (formatted_categories )
54
+ 'categories' : categories [start :end ],
55
+ 'total_categories' : len (categories )
56
56
})
57
57
58
58
"""
@@ -67,15 +67,14 @@ def get_categories():
67
67
ten questions per page and pagination at the bottom of the screen for three pages.
68
68
Clicking on the page numbers should update the questions.
69
69
"""
70
- @app .route ('/questions' )
70
+ @app .route ('/questions' , methods = [ 'GET' ] )
71
71
def get_questions ():
72
72
# Implement pagination
73
73
page = request .args .get ('page' , 1 , type = int )
74
74
start = (page - 1 ) * 10
75
75
end = start + 10
76
76
77
- categories = Category .query .all ()
78
- formatted_categories = [category .format () for category in categories ]
77
+ categories = get_all_formatted_category ()
79
78
80
79
questions = Question .query .all ()
81
80
formatted_questions = [question .format () for question in questions ]
@@ -84,7 +83,7 @@ def get_questions():
84
83
'success' : True ,
85
84
'questions' : formatted_questions [start :end ],
86
85
'total_questions' : len (formatted_questions ),
87
- 'categories' : formatted_categories ,
86
+ 'categories' : categories
88
87
})
89
88
90
89
"""
@@ -94,6 +93,32 @@ def get_questions():
94
93
TEST: When you click the trash icon next to a question, the question will be removed.
95
94
This removal will persist in the database and when you refresh the page.
96
95
"""
96
+ @app .route ('/questions/<int:id>' , methods = ['DELETE' ])
97
+ def delete_question (id ):
98
+ id_question = int (id )
99
+
100
+ try :
101
+ question = Question .query .filter (Question .id == id_question ).one_or_none ()
102
+
103
+ if question is None :
104
+ handle_error (404 , 'Error: question not found' )
105
+
106
+ db .session .delete (question )
107
+ db .session .commit ()
108
+ except :
109
+ handle_error (422 , 'An error occurred!' )
110
+
111
+ return jsonify ({
112
+ 'success' : True ,
113
+ 'message' : "deleted successfully"
114
+ })
115
+
116
+ # return jsonify({
117
+ # 'success': True,
118
+ # 'deleted': id_question,
119
+ # 'question': current_questions,
120
+ # 'total_question': len(current_questions)
121
+ # })
97
122
98
123
"""
99
124
@TODO:
@@ -105,6 +130,24 @@ def get_questions():
105
130
the form will clear and the question will appear at the end of the last page
106
131
of the questions list in the "List" tab.
107
132
"""
133
+ @app .route ('/questions' , methods = ['POST' ])
134
+ def create_question ():
135
+ body = request .get_json ()
136
+ new_question = body .get ('question' , None )
137
+ new_answer = body .get ('answer' , None )
138
+ new_difficulty = body .get ('difficulty' , None )
139
+ new_category = body .get ('category' , None )
140
+
141
+ try :
142
+ question = Question (question = new_question , answer = new_answer , category = int (new_category ), difficulty = int (new_difficulty ))
143
+ question .insert ()
144
+ except :
145
+ handle_error (422 , 'An error occurred!' )
146
+
147
+ return ({
148
+ 'success' : True ,
149
+ 'message' : 'Create successfully!'
150
+ })
108
151
109
152
"""
110
153
@TODO:
@@ -116,6 +159,22 @@ def get_questions():
116
159
only question that include that string within their question.
117
160
Try using the word "title" to start.
118
161
"""
162
+ @app .route ('/questions/search' , methods = ['POST' ])
163
+ def search_questions ():
164
+ page = request .args .get ('page' , 1 , type = int )
165
+ start = (page - 1 ) * 10
166
+ end = start + 10
167
+
168
+ body = request .get_json ()
169
+ key_word = body ['searchTerm' ]
170
+ questions = db .session .query (Question ).filter (Question .question .ilike (f'%{ key_word } %' )).all ()
171
+ formatted_questions = [question .format () for question in questions ]
172
+
173
+ return jsonify ({
174
+ 'success' : True ,
175
+ 'questions' : formatted_questions [start :end ],
176
+ 'total_questions' : len (formatted_questions ),
177
+ })
119
178
120
179
"""
121
180
@TODO:
@@ -125,6 +184,25 @@ def get_questions():
125
184
categories in the left column will cause only questions of that
126
185
category to be shown.
127
186
"""
187
+ @app .route ("/categories/<int:id>/questions" )
188
+ def get_all_question (id ):
189
+ # Implement pagination
190
+ page = request .args .get ('page' , 1 , type = int )
191
+ start = (page - 1 ) * 10
192
+ end = start + 10
193
+
194
+ id_category = int (id )
195
+ categories = Category .query .filter_by (id = id_category ).all ()
196
+ formatted_categories = [category .format () for category in categories ]
197
+ questions = Question .query .filter_by (category = id_category ).all ()
198
+ formatted_questions = [question .format () for question in questions ]
199
+
200
+ return jsonify ({
201
+ 'success' : True ,
202
+ 'questions' : formatted_questions [start :end ],
203
+ 'total_questions' : len (formatted_questions ),
204
+ 'currentCategory' : formatted_categories [0 ]
205
+ })
128
206
129
207
"""
130
208
@TODO:
@@ -137,12 +215,46 @@ def get_questions():
137
215
one question at a time is displayed, the user is allowed to answer
138
216
and shown whether they were correct or not.
139
217
"""
218
+ @app .route ("/quizzes" , methods = ['POST' ])
219
+ def get_question_to_play ():
220
+ data = request .get_json ()
221
+ previous_questions = data .get ('previous_questions' )
222
+ quiz_category = data .get ('quiz_category' )
223
+ result = None
224
+ questions = []
225
+
226
+ # get all questions
227
+ if quiz_category ['id' ] is 0 :
228
+ questions = Question .query .all ()
229
+ else :
230
+ questions = Question .query .filter_by (category = quiz_category ['id' ]).all ()
231
+
232
+ format_questions = [question .format () for question in questions ]
233
+ if len (format_questions ) != 0 :
234
+ if len (previous_questions ) is 0 :
235
+ result = format_questions [0 ]
236
+ else :
237
+ data = [question for question in format_questions if question ['id' ] not in previous_questions ]
238
+ if len (data ) != 0 :
239
+ result = data [0 ]
240
+
241
+ return jsonify ({
242
+ 'question' : result
243
+ })
140
244
141
245
"""
142
246
@TODO:
143
247
Create error handlers for all expected errors
144
248
including 404 and 422.
145
249
"""
250
+ def handle_error (code , message ):
251
+ error_message = ({'message' : message })
252
+ abort (Response (error_message , code ))
253
+
254
+ def get_all_formatted_category ():
255
+ categories = Category .query .all ()
256
+ formatted_categories = [category .format () for category in categories ]
257
+ return formatted_categories
146
258
147
259
return app
148
260
0 commit comments