Skip to content
This repository was archived by the owner on Dec 12, 2022. It is now read-only.

Commit 64be4a2

Browse files
Merge pull request #3 from kirki333/master
Added new type of request and it's method
2 parents 00a6691 + ebc54c6 commit 64be4a2

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

app/api/Question.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import json
22
import uuid
3-
3+
import collections
44

55
class 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+
4786
def show_all_question(self):
4887
return self.read_question_file()
4988

app/run.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ def decorator(func):
3131
def show_all_question():
3232
return 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)
3641
def show_question(question_id):

0 commit comments

Comments
 (0)