1
1
from fastapi import FastAPI , HTTPException , Query
2
- from typing import Optional , List
2
+ from dotenv import load_dotenv
3
3
from models .endpoint_models import QuestionBase64Images
4
4
from models .exceptions import QuestionNotFoundException
5
5
from utils import batch_convert_base64_to_bytes , batch_convert_bytes_to_base64
6
- import crud
6
+ from crud import (
7
+ create_question ,
8
+ get_question ,
9
+ get_random_question_by_difficulty_and_topic ,
10
+ override_question ,
11
+ delete_question ,
12
+ list_difficulties_and_topics
13
+ )
14
+
15
+
16
+ load_dotenv ()
7
17
8
18
app = FastAPI ()
9
19
@@ -12,7 +22,7 @@ def create_question(q: QuestionBase64Images):
12
22
# Convert images and call CRUD with primitive types
13
23
images_bytes = batch_convert_base64_to_bytes (q .images )
14
24
15
- new_qid = crud . create_question (
25
+ new_qid = create_question (
16
26
name = q .name ,
17
27
description = q .description ,
18
28
difficulty = q .difficulty ,
@@ -30,19 +40,20 @@ def create_question(q: QuestionBase64Images):
30
40
@app .get ("/questions/{qid}" , response_model = QuestionBase64Images )
31
41
def read_question (qid : str ):
32
42
try :
33
- question_dict = crud . get_question (qid )
43
+ question_dict = get_question (qid )
34
44
except QuestionNotFoundException as e :
35
45
raise HTTPException (status_code = 404 , detail = f"Question { e .question_id } not found" )
36
46
return batch_convert_bytes_to_base64 (question_dict )
37
47
48
+
38
49
@app .get ("/questions/random" )
39
50
def get_random_question (
40
51
difficulty : str = Query (..., description = "The difficulty level to filter by" ),
41
52
topic : str = Query (..., description = "The topic to filter by" )
42
53
):
43
54
"""Get a random question by difficulty and topic"""
44
55
try :
45
- question_dict = crud . get_random_question_by_difficulty_and_topic (difficulty , topic )
56
+ question_dict = get_random_question_by_difficulty_and_topic (difficulty , topic )
46
57
except QuestionNotFoundException as e :
47
58
raise HTTPException (status_code = 404 , detail = str (e ))
48
59
return batch_convert_bytes_to_base64 (question_dict )
@@ -53,7 +64,7 @@ def update_question(qid: str, q: QuestionBase64Images):
53
64
images_bytes = batch_convert_base64_to_bytes (q .images )
54
65
55
66
try :
56
- crud . override_question (
67
+ override_question (
57
68
qid = qid ,
58
69
name = q .name ,
59
70
description = q .description ,
@@ -68,16 +79,18 @@ def update_question(qid: str, q: QuestionBase64Images):
68
79
"message" : "Updated successfully"
69
80
}
70
81
82
+
71
83
@app .delete ("/questions/{qid}" )
72
84
def delete_question (qid : str ):
73
85
try :
74
- crud . delete_question (qid )
86
+ delete_question (qid )
75
87
except QuestionNotFoundException as e :
76
88
raise HTTPException (status_code = 404 , detail = str (e ))
77
89
return {
78
90
"message" : "Deleted successfully"
79
91
}
80
92
93
+
81
94
@app .get ("/metadata" )
82
95
def get_metadata ():
83
- return crud . list_difficulties_and_topics ()
96
+ return list_difficulties_and_topics ()
0 commit comments