4
4
from app .models .exceptions import QuestionNotFoundException
5
5
from app .utils import batch_convert_base64_to_bytes , batch_convert_bytes_to_base64
6
6
from app .crud import (
7
+ add_difficulty ,
8
+ delete_difficulty ,
9
+ add_topic ,
10
+ delete_topic ,
7
11
create_question ,
8
12
get_question ,
9
13
get_random_question_by_difficulty_and_topic ,
18
22
app = FastAPI ()
19
23
20
24
@app .post ("/questions" )
21
- def create_question (q : QuestionBase64Images ):
25
+ def create_question_endpoint (q : QuestionBase64Images ):
22
26
# Convert images and call CRUD with primitive types
23
27
images_bytes = batch_convert_base64_to_bytes (q .images )
24
28
@@ -38,16 +42,17 @@ def create_question(q: QuestionBase64Images):
38
42
39
43
40
44
@app .get ("/questions/{qid}" , response_model = QuestionBase64Images )
41
- def read_question (qid : str ):
45
+ def get_question_endpoint (qid : str ):
42
46
try :
43
47
question_dict = get_question (qid )
44
48
except QuestionNotFoundException as e :
45
49
raise HTTPException (status_code = 404 , detail = f"Question { e .question_id } not found" )
46
- return batch_convert_bytes_to_base64 (question_dict )
50
+ question_dict ['images' ] = batch_convert_bytes_to_base64 (question_dict ['images' ])
51
+ return question_dict
47
52
48
53
49
54
@app .get ("/questions/random" )
50
- def get_random_question (
55
+ def get_random_question_endpoint (
51
56
difficulty : str = Query (..., description = "The difficulty level to filter by" ),
52
57
topic : str = Query (..., description = "The topic to filter by" )
53
58
):
@@ -56,11 +61,12 @@ def get_random_question(
56
61
question_dict = get_random_question_by_difficulty_and_topic (difficulty , topic )
57
62
except QuestionNotFoundException as e :
58
63
raise HTTPException (status_code = 404 , detail = str (e ))
59
- return batch_convert_bytes_to_base64 (question_dict )
64
+ question_dict ['images' ] = batch_convert_bytes_to_base64 (question_dict ['images' ])
65
+ return question_dict
60
66
61
67
62
68
@app .put ("/questions/{qid}" )
63
- def update_question (qid : str , q : QuestionBase64Images ):
69
+ def update_question_endpoint (qid : str , q : QuestionBase64Images ):
64
70
images_bytes = batch_convert_base64_to_bytes (q .images )
65
71
66
72
try :
@@ -81,7 +87,7 @@ def update_question(qid: str, q: QuestionBase64Images):
81
87
82
88
83
89
@app .delete ("/questions/{qid}" )
84
- def delete_question (qid : str ):
90
+ def delete_question_endpoint (qid : str ):
85
91
try :
86
92
delete_question (qid )
87
93
except QuestionNotFoundException as e :
@@ -91,6 +97,34 @@ def delete_question(qid: str):
91
97
}
92
98
93
99
94
- @app .get ("/metadata " )
95
- def get_metadata ():
100
+ @app .get ("/difficulties-topics " )
101
+ def get_difficulties_topics ():
96
102
return list_difficulties_and_topics ()
103
+
104
+ @app .post ("/difficulties" )
105
+ def add_difficulty_endpoint (difficulty : str = Query (..., description = "The difficulty level to add" )):
106
+ add_difficulty (difficulty )
107
+ return {
108
+ "message" : f"Difficulty '{ difficulty } ' added successfully"
109
+ }
110
+
111
+ @app .delete ("/difficulties" )
112
+ def delete_difficulty_endpoint (difficulty : str = Query (..., description = "The difficulty level to delete" )):
113
+ delete_difficulty (difficulty )
114
+ return {
115
+ "message" : f"Difficulty '{ difficulty } ' deleted successfully"
116
+ }
117
+
118
+ @app .post ("/topics" )
119
+ def add_topic_endpoint (topic : str = Query (..., description = "The topic to add" )):
120
+ add_topic (topic )
121
+ return {
122
+ "message" : f"Topic '{ topic } ' added successfully"
123
+ }
124
+
125
+ @app .delete ("/topics" )
126
+ def delete_topic_endpoint (topic : str = Query (..., description = "The topic to delete" )):
127
+ delete_topic (topic )
128
+ return {
129
+ "message" : f"Topic '{ topic } ' deleted successfully"
130
+ }
0 commit comments