Skip to content

Commit 3607dbf

Browse files
committed
Refactor to not import whole package
1 parent d68c169 commit 3607dbf

File tree

1 file changed

+21
-8
lines changed
  • services/question-service/app

1 file changed

+21
-8
lines changed

services/question-service/app/main.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
from fastapi import FastAPI, HTTPException, Query
2-
from typing import Optional, List
2+
from dotenv import load_dotenv
33
from models.endpoint_models import QuestionBase64Images
44
from models.exceptions import QuestionNotFoundException
55
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()
717

818
app = FastAPI()
919

@@ -12,7 +22,7 @@ def create_question(q: QuestionBase64Images):
1222
# Convert images and call CRUD with primitive types
1323
images_bytes = batch_convert_base64_to_bytes(q.images)
1424

15-
new_qid = crud.create_question(
25+
new_qid = create_question(
1626
name=q.name,
1727
description=q.description,
1828
difficulty=q.difficulty,
@@ -30,19 +40,20 @@ def create_question(q: QuestionBase64Images):
3040
@app.get("/questions/{qid}", response_model=QuestionBase64Images)
3141
def read_question(qid: str):
3242
try:
33-
question_dict = crud.get_question(qid)
43+
question_dict = get_question(qid)
3444
except QuestionNotFoundException as e:
3545
raise HTTPException(status_code=404, detail=f"Question {e.question_id} not found")
3646
return batch_convert_bytes_to_base64(question_dict)
3747

48+
3849
@app.get("/questions/random")
3950
def get_random_question(
4051
difficulty: str = Query(..., description="The difficulty level to filter by"),
4152
topic: str = Query(..., description="The topic to filter by")
4253
):
4354
"""Get a random question by difficulty and topic"""
4455
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)
4657
except QuestionNotFoundException as e:
4758
raise HTTPException(status_code=404, detail=str(e))
4859
return batch_convert_bytes_to_base64(question_dict)
@@ -53,7 +64,7 @@ def update_question(qid: str, q: QuestionBase64Images):
5364
images_bytes = batch_convert_base64_to_bytes(q.images)
5465

5566
try:
56-
crud.override_question(
67+
override_question(
5768
qid=qid,
5869
name=q.name,
5970
description=q.description,
@@ -68,16 +79,18 @@ def update_question(qid: str, q: QuestionBase64Images):
6879
"message": "Updated successfully"
6980
}
7081

82+
7183
@app.delete("/questions/{qid}")
7284
def delete_question(qid: str):
7385
try:
74-
crud.delete_question(qid)
86+
delete_question(qid)
7587
except QuestionNotFoundException as e:
7688
raise HTTPException(status_code=404, detail=str(e))
7789
return {
7890
"message": "Deleted successfully"
7991
}
8092

93+
8194
@app.get("/metadata")
8295
def get_metadata():
83-
return crud.list_difficulties_and_topics()
96+
return list_difficulties_and_topics()

0 commit comments

Comments
 (0)