feat(init): 添加完整的认证和业务路由模块
- 实现微信登录、手机号绑定、token验证和登出功能 - 添加打卡系统,支持正常、迟到、请假等状态记录 - 集成课程投票功能,支持多课程选择和评分评论 - 开发反馈系统,支持图文反馈提交和管理员处理 - 构建首页页面模块,动态展示学习资源和个人信息 - 设计计划管理系统,支持周/月/年计划制定和跟踪 - 实现小组计划统计和历史记录查询功能 - 完善用户认证会话管理和数据库交互逻辑
This commit is contained in:
81
routes/survey.py
Normal file
81
routes/survey.py
Normal file
@@ -0,0 +1,81 @@
|
||||
import json
|
||||
import logging
|
||||
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from pydantic import BaseModel
|
||||
|
||||
from .. import config, db
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
router = APIRouter(prefix="/api/v1/survey", tags=["survey"])
|
||||
|
||||
|
||||
def _fmt_row(row):
|
||||
if "answers" in row and isinstance(row["answers"], str):
|
||||
try:
|
||||
row["answers"] = json.loads(row["answers"])
|
||||
except Exception:
|
||||
logger.exception("survey answers 解析失败")
|
||||
row["answers"] = {}
|
||||
for key in ("created_at", "submitted_at", "updated_at"):
|
||||
row[key] = str(row.get(key)) if row.get(key) else ""
|
||||
return row
|
||||
|
||||
|
||||
class SurveyResponseRequest(BaseModel):
|
||||
task_id: int
|
||||
group_id: str
|
||||
answers: dict = {}
|
||||
|
||||
|
||||
class AnswersRequest(BaseModel):
|
||||
answers: dict = {}
|
||||
|
||||
|
||||
@router.get("/tasks")
|
||||
def tasks():
|
||||
rows = db.fetch_all("SELECT * FROM survey_tasks WHERE status='active' ORDER BY task_no", db=config.SURVEY_DB)
|
||||
return [_fmt_row(r) for r in rows]
|
||||
|
||||
|
||||
@router.get("/responses")
|
||||
def responses(task_id: int):
|
||||
rows = db.fetch_all("SELECT * FROM survey_responses WHERE task_id=%s ORDER BY group_id", (task_id,), db=config.SURVEY_DB)
|
||||
return [_fmt_row(r) for r in rows]
|
||||
|
||||
|
||||
@router.post("/response")
|
||||
def response(req: SurveyResponseRequest):
|
||||
if not req.task_id or not req.group_id or not req.answers:
|
||||
raise HTTPException(status_code=400, detail="missing fields")
|
||||
answers_json = json.dumps(req.answers, ensure_ascii=False)
|
||||
existing = db.fetch_one(
|
||||
"SELECT id FROM survey_responses WHERE task_id=%s AND group_id=%s",
|
||||
(req.task_id, req.group_id), db=config.SURVEY_DB,
|
||||
)
|
||||
if existing:
|
||||
db.execute("UPDATE survey_responses SET answers=%s WHERE id=%s", (answers_json, existing["id"]), db=config.SURVEY_DB)
|
||||
rid = existing["id"]
|
||||
else:
|
||||
rid = db.execute(
|
||||
"INSERT INTO survey_responses (task_id,group_id,answers) VALUES (%s,%s,%s)",
|
||||
(req.task_id, req.group_id, answers_json), db=config.SURVEY_DB,
|
||||
)
|
||||
row = db.fetch_one("SELECT * FROM survey_responses WHERE id=%s", (rid,), db=config.SURVEY_DB)
|
||||
return _fmt_row(row)
|
||||
|
||||
|
||||
@router.put("/response/{rid}")
|
||||
def update_response(rid: int, req: AnswersRequest):
|
||||
db.execute("UPDATE survey_responses SET answers=%s WHERE id=%s", (json.dumps(req.answers, ensure_ascii=False), rid), db=config.SURVEY_DB)
|
||||
row = db.fetch_one("SELECT * FROM survey_responses WHERE id=%s", (rid,), db=config.SURVEY_DB)
|
||||
if not row:
|
||||
raise HTTPException(status_code=404, detail="not found")
|
||||
return _fmt_row(row)
|
||||
|
||||
|
||||
@router.post("/task")
|
||||
def task():
|
||||
return {"msg": "功能预留,后续开放"}
|
||||
Reference in New Issue
Block a user