This repository was archived by the owner on Dec 12, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +45
-1
lines changed
Expand file tree Collapse file tree 2 files changed +45
-1
lines changed Original file line number Diff line number Diff line change 11import json
22import uuid
3-
3+ import collections
44
55class Question :
66
@@ -44,6 +44,45 @@ def show_question(self, question_id):
4444'status' : '404'
4545}
4646
47+ def show_categories (self ):
48+ """return all the categories of questions and the number of questions each one has
49+ ,not the null categories of course"""
50+ #open and read json file
51+ json_file = self .read_question_file ()
52+ categories = {}
53+ categories_num = {}
54+ i = 0
55+
56+ #find categories in the json file and insert them in a dictionary with seperate keys
57+ for category in json_file :
58+ cat = category ['category' ]
59+ if not (cat is None ):
60+ if not (cat in categories .values ()): #check for duplicate categories
61+ key = "category " + str (i + 1 )
62+ categories [key ] = cat
63+ i = i + 1
64+ categories_num [cat ] = 1
65+ else :
66+ if (cat in categories_num ):
67+ categories_num [cat ] = categories_num [cat ] + 1
68+
69+ #create a dictionary with nested values
70+ data = {}
71+ for item in categories :
72+ category = categories [item ]
73+ data [category ] = {}
74+ data [category ]['number of questions' ] = categories_num [category ]
75+
76+ if categories :
77+ return {
78+ "categories" : data
79+ }
80+ else :
81+ #in case all the categories are null it returns special message
82+ return {
83+ "status" : "No categories have been created"
84+ }
85+
4786def show_all_question (self ):
4887return self .read_question_file ()
4988
Original file line number Diff line number Diff line change @@ -31,6 +31,11 @@ def decorator(func):
3131def show_all_question ():
3232return jsonify (Question ().show_all_question ())
3333
34+ @app .route ("/show-all-categories" )
35+ @jwt_cond (jwt_required , False )
36+ def show_categories ():
37+ return jsonify (Question ().show_categories ())
38+
3439@app .route ('/show-question/<int:question_id>' )
3540@jwt_cond (jwt_required , False )
3641def show_question (question_id ):
You can’t perform that action at this time.
0 commit comments