You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
3.1 KiB
Python
108 lines
3.1 KiB
Python
from fastapi import FastAPI, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from pydantic import BaseModel
|
|
from typing import List, Optional
|
|
import sys
|
|
import os
|
|
import random
|
|
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), "../../../shared"))
|
|
from database.connection import get_db
|
|
from database.models import Question, Theme
|
|
|
|
app = FastAPI(title="Question Service", version="1.0.0")
|
|
|
|
|
|
class QuestionResponse(BaseModel):
|
|
id: int
|
|
theme_id: int
|
|
question_text: str
|
|
hint: Optional[str] = None
|
|
difficulty_level: int
|
|
theme_name: str
|
|
|
|
|
|
class QuestionCreate(BaseModel):
|
|
theme_id: int
|
|
question_text: str
|
|
correct_answer: str
|
|
hint: Optional[str] = None
|
|
difficulty_level: int = 1
|
|
|
|
|
|
@app.get("/health")
|
|
def health_check():
|
|
return {"status": "healthy", "service": "question-service"}
|
|
|
|
|
|
@app.get("/random", response_model=QuestionResponse)
|
|
def get_random_question(db: Session = Depends(get_db)):
|
|
questions = db.query(Question).join(Theme).all()
|
|
if not questions:
|
|
raise HTTPException(status_code=404, detail="No questions available")
|
|
|
|
question = random.choice(questions)
|
|
|
|
return QuestionResponse(
|
|
id=question.id,
|
|
theme_id=question.theme_id,
|
|
question_text=question.question_text,
|
|
hint=question.hint,
|
|
difficulty_level=question.difficulty_level,
|
|
theme_name=question.theme.name
|
|
)
|
|
|
|
|
|
@app.get("/{question_id}", response_model=QuestionResponse)
|
|
def get_question(question_id: int, db: Session = Depends(get_db)):
|
|
question = db.query(Question).join(Theme).filter(Question.id == question_id).first()
|
|
if not question:
|
|
raise HTTPException(status_code=404, detail="Question not found")
|
|
|
|
return QuestionResponse(
|
|
id=question.id,
|
|
theme_id=question.theme_id,
|
|
question_text=question.question_text,
|
|
hint=question.hint,
|
|
difficulty_level=question.difficulty_level,
|
|
theme_name=question.theme.name
|
|
)
|
|
|
|
|
|
@app.post("/", response_model=QuestionResponse)
|
|
def create_question(question_data: QuestionCreate, db: Session = Depends(get_db)):
|
|
theme = db.query(Theme).filter(Theme.id == question_data.theme_id).first()
|
|
if not theme:
|
|
raise HTTPException(status_code=404, detail="Theme not found")
|
|
|
|
question = Question(
|
|
theme_id=question_data.theme_id,
|
|
question_text=question_data.question_text,
|
|
correct_answer=question_data.correct_answer,
|
|
hint=question_data.hint,
|
|
difficulty_level=question_data.difficulty_level
|
|
)
|
|
|
|
db.add(question)
|
|
db.commit()
|
|
db.refresh(question)
|
|
|
|
return QuestionResponse(
|
|
id=question.id,
|
|
theme_id=question.theme_id,
|
|
question_text=question.question_text,
|
|
hint=question.hint,
|
|
difficulty_level=question.difficulty_level,
|
|
theme_name=theme.name
|
|
)
|
|
|
|
|
|
@app.get("/themes/", response_model=List[dict])
|
|
def get_themes(db: Session = Depends(get_db)):
|
|
themes = db.query(Theme).all()
|
|
return [{"id": theme.id, "name": theme.name, "description": theme.description} for theme in themes]
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run(app, host="0.0.0.0", port=8002) |