文件

34 行
1.1 KiB
Python

"""题目 API:列表与详情(含选择题、LaTeX 用法解释)。"""
from __future__ import annotations
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from app.core.db import get_session
from app.repositories.problem_repo import ProblemRepo
router = APIRouter(prefix="/problems", tags=["problems"])
@router.get("")
async def list_problems(course: str | None = None, session: Session = Depends(get_session)):
rows = ProblemRepo(session).list_by_course(course)
return [p.to_dict() for p in rows]
@router.get("/hydro/{hydro_pid}")
async def get_problem_by_hydro(hydro_pid: str, session: Session = Depends(get_session)):
"""按 HydroOJ 题号取题(教师公告打开题目用)。"""
row = ProblemRepo(session).get_by_hydro_pid(hydro_pid)
if not row:
raise HTTPException(404, "题目不存在")
return row.to_dict()
@router.get("/{pid}")
async def get_problem(pid: int, session: Session = Depends(get_session)):
row = ProblemRepo(session).get(pid)
if not row:
raise HTTPException(404, "题目不存在")
return row.to_dict()