Skip to content

Commit 92660d9

Browse files
committed
Fix bugs
1 parent e408edb commit 92660d9

File tree

2 files changed

+63
-10
lines changed

2 files changed

+63
-10
lines changed

services/question-service/app/crud.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,26 @@ def list_difficulties_and_topics() -> Dict[str, List[str]]:
2121
topics = [row[0] for row in cur.fetchall()]
2222

2323
return {"difficulties": difficulties, "topics": topics}
24-
24+
25+
def add_difficulty(difficulty: str):
26+
with get_conn() as conn:
27+
with conn.cursor() as cur:
28+
cur.execute("INSERT INTO difficulties (name) VALUES (%s) ON CONFLICT DO NOTHING", (difficulty,))
29+
30+
def delete_difficulty(difficulty: str):
31+
with get_conn() as conn:
32+
with conn.cursor() as cur:
33+
cur.execute("DELETE FROM difficulties WHERE level = %s", (difficulty,))
34+
35+
def add_topic(topic: str):
36+
with get_conn() as conn:
37+
with conn.cursor() as cur:
38+
cur.execute("INSERT INTO topics (name) VALUES (%s) ON CONFLICT DO NOTHING", (topic,))
39+
40+
def delete_topic(topic: str):
41+
with get_conn() as conn:
42+
with conn.cursor() as cur:
43+
cur.execute("DELETE FROM topics WHERE name = %s", (topic,))
2544

2645
def create_question(
2746
name: str,

services/question-service/app/main.py

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
from app.models.exceptions import QuestionNotFoundException
55
from app.utils import batch_convert_base64_to_bytes, batch_convert_bytes_to_base64
66
from app.crud import (
7+
add_difficulty,
8+
delete_difficulty,
9+
add_topic,
10+
delete_topic,
711
create_question,
812
get_question,
913
get_random_question_by_difficulty_and_topic,
@@ -18,7 +22,7 @@
1822
app = FastAPI()
1923

2024
@app.post("/questions")
21-
def create_question(q: QuestionBase64Images):
25+
def create_question_endpoint(q: QuestionBase64Images):
2226
# Convert images and call CRUD with primitive types
2327
images_bytes = batch_convert_base64_to_bytes(q.images)
2428

@@ -38,16 +42,17 @@ def create_question(q: QuestionBase64Images):
3842

3943

4044
@app.get("/questions/{qid}", response_model=QuestionBase64Images)
41-
def read_question(qid: str):
45+
def get_question_endpoint(qid: str):
4246
try:
4347
question_dict = get_question(qid)
4448
except QuestionNotFoundException as e:
4549
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
4752

4853

4954
@app.get("/questions/random")
50-
def get_random_question(
55+
def get_random_question_endpoint(
5156
difficulty: str = Query(..., description="The difficulty level to filter by"),
5257
topic: str = Query(..., description="The topic to filter by")
5358
):
@@ -56,11 +61,12 @@ def get_random_question(
5661
question_dict = get_random_question_by_difficulty_and_topic(difficulty, topic)
5762
except QuestionNotFoundException as e:
5863
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
6066

6167

6268
@app.put("/questions/{qid}")
63-
def update_question(qid: str, q: QuestionBase64Images):
69+
def update_question_endpoint(qid: str, q: QuestionBase64Images):
6470
images_bytes = batch_convert_base64_to_bytes(q.images)
6571

6672
try:
@@ -81,7 +87,7 @@ def update_question(qid: str, q: QuestionBase64Images):
8187

8288

8389
@app.delete("/questions/{qid}")
84-
def delete_question(qid: str):
90+
def delete_question_endpoint(qid: str):
8591
try:
8692
delete_question(qid)
8793
except QuestionNotFoundException as e:
@@ -91,6 +97,34 @@ def delete_question(qid: str):
9197
}
9298

9399

94-
@app.get("/metadata")
95-
def get_metadata():
100+
@app.get("/difficulties-topics")
101+
def get_difficulties_topics():
96102
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

Comments
 (0)