1
- from fastapi import FastAPI , HTTPException , Query
1
+ from fastapi import FastAPI
2
2
from dotenv import load_dotenv
3
- from app .models .endpoint_models import QuestionBase64Images
4
- from app .models .exceptions import QuestionNotFoundException
5
- from app .utils import batch_convert_base64_to_bytes , batch_convert_bytes_to_base64
6
- from app .crud import (
7
- add_difficulty ,
8
- delete_difficulty ,
9
- add_topic ,
10
- delete_topic ,
11
- create_question ,
12
- get_question ,
13
- get_random_question_by_difficulty_and_topic ,
14
- override_question ,
15
- delete_question ,
16
- list_difficulties_and_topics
17
- )
18
-
3
+ from app .routers import questions_router , metadata_router
19
4
20
5
load_dotenv ()
21
6
22
7
app = FastAPI ()
23
8
24
- @app .post ("/questions" )
25
- def create_question_endpoint (q : QuestionBase64Images ):
26
- # Convert images and call CRUD with primitive types
27
- images_bytes = batch_convert_base64_to_bytes (q .images )
28
-
29
- new_qid = create_question (
30
- name = q .name ,
31
- description = q .description ,
32
- difficulty = q .difficulty ,
33
- topic = q .topic ,
34
- images = images_bytes
35
- )
36
-
37
- # Get the created question and convert to response model
38
- return {
39
- "id" : new_qid ,
40
- "message" : "Created successfully"
41
- }
42
-
43
-
44
- @app .get ("/questions/{qid}" , response_model = QuestionBase64Images )
45
- def get_question_endpoint (qid : str ):
46
- try :
47
- question_dict = get_question (qid )
48
- except QuestionNotFoundException as e :
49
- raise HTTPException (status_code = 404 , detail = f"Question { e .question_id } not found" )
50
- question_dict ['images' ] = batch_convert_bytes_to_base64 (question_dict ['images' ])
51
- return question_dict
52
-
53
-
54
- @app .get ("/questions/random" )
55
- def get_random_question_endpoint (
56
- difficulty : str = Query (..., description = "The difficulty level to filter by" ),
57
- topic : str = Query (..., description = "The topic to filter by" )
58
- ):
59
- """Get a random question by difficulty and topic"""
60
- try :
61
- question_dict = get_random_question_by_difficulty_and_topic (difficulty , topic )
62
- except QuestionNotFoundException as e :
63
- raise HTTPException (status_code = 404 , detail = str (e ))
64
- question_dict ['images' ] = batch_convert_bytes_to_base64 (question_dict ['images' ])
65
- return question_dict
66
-
67
-
68
- @app .put ("/questions/{qid}" )
69
- def update_question_endpoint (qid : str , q : QuestionBase64Images ):
70
- images_bytes = batch_convert_base64_to_bytes (q .images )
71
-
72
- try :
73
- override_question (
74
- qid = qid ,
75
- name = q .name ,
76
- description = q .description ,
77
- difficulty = q .difficulty ,
78
- topic = q .topic ,
79
- images = images_bytes
80
- )
81
- except QuestionNotFoundException as e :
82
- raise HTTPException (status_code = 404 , detail = str (e ))
83
-
84
- return {
85
- "message" : "Updated successfully"
86
- }
87
-
88
-
89
- @app .delete ("/questions/{qid}" )
90
- def delete_question_endpoint (qid : str ):
91
- try :
92
- delete_question (qid )
93
- except QuestionNotFoundException as e :
94
- raise HTTPException (status_code = 404 , detail = str (e ))
95
- return {
96
- "message" : "Deleted successfully"
97
- }
98
-
99
-
100
- @app .get ("/difficulties-topics" )
101
- def get_difficulties_topics ():
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
- }
9
+ # Include routers
10
+ app .include_router (questions_router )
11
+ app .include_router (metadata_router )
0 commit comments