refactor: split app structure
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||
|
||||
from ..api_utils import load_accounts, load_records
|
||||
from ..auth import verify_records_auth
|
||||
from ..config import ADMIN_TASKS_PATH
|
||||
from ..data import account_to_dict, query_records, submit_correction_tasks
|
||||
from ..schemas import CorrectionSubmitPayload
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/api/records")
|
||||
def records(
|
||||
q: str = Query(..., min_length=1, description="自然语言查询,例如:王鑫鹏5月数学课"),
|
||||
limit: int = Query(200, ge=1, le=1000),
|
||||
_user: str = Depends(verify_records_auth),
|
||||
):
|
||||
return query_records(load_records(), q, limit=limit)
|
||||
|
||||
|
||||
@router.get("/api/student-account/{student}")
|
||||
def record_student_account(student: str, _user: str = Depends(verify_records_auth)):
|
||||
for account in load_accounts():
|
||||
if account.student == student or account.student_id == student:
|
||||
return account_to_dict(account)
|
||||
raise HTTPException(status_code=404, detail=f"未找到学生账户: {student}")
|
||||
|
||||
|
||||
@router.post("/api/corrections")
|
||||
def submit_corrections(payload: CorrectionSubmitPayload, _user: str = Depends(verify_records_auth)):
|
||||
try:
|
||||
result = submit_correction_tasks(
|
||||
ADMIN_TASKS_PATH,
|
||||
[item.dict() for item in payload.items],
|
||||
)
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=400, detail=str(exc)) from exc
|
||||
return {"ok": True, **result}
|
||||
Reference in New Issue
Block a user