56 行
1.9 KiB
Python
56 行
1.9 KiB
Python
from __future__ import annotations
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.database import get_db
|
|
from app.auth import get_current_user
|
|
from app.models.guide import GuideConfig, GuideSection
|
|
from app.schemas.guide import GuideConfigSchema, GuideSectionSchema, GuideSectionResponse
|
|
|
|
router = APIRouter(prefix="/api/guides", tags=["出行指南"], dependencies=[Depends(get_current_user)])
|
|
|
|
|
|
def _get_config(db: Session) -> GuideConfig:
|
|
config = db.query(GuideConfig).first()
|
|
if not config:
|
|
config = GuideConfig()
|
|
db.add(config)
|
|
db.flush()
|
|
return config
|
|
|
|
|
|
@router.get("/config", response_model=GuideConfigSchema)
|
|
def get_config(db: Session = Depends(get_db)):
|
|
c = _get_config(db)
|
|
return GuideConfigSchema(page_intro=c.page_intro)
|
|
|
|
|
|
@router.put("/config")
|
|
def update_config(req: GuideConfigSchema, db: Session = Depends(get_db)):
|
|
c = _get_config(db)
|
|
c.page_intro = req.page_intro
|
|
db.commit()
|
|
return {"message": "更新成功"}
|
|
|
|
|
|
@router.get("/sections", response_model=list[GuideSectionResponse])
|
|
def list_sections(db: Session = Depends(get_db)):
|
|
c = _get_config(db)
|
|
sections = db.query(GuideSection).filter_by(config_id=c.id).order_by(GuideSection.sort_order).all()
|
|
return [GuideSectionResponse.model_validate(s) for s in sections]
|
|
|
|
|
|
@router.put("/sections/{section_id}")
|
|
def update_section(section_id: str, req: GuideSectionSchema, db: Session = Depends(get_db)):
|
|
c = _get_config(db)
|
|
section = db.query(GuideSection).filter_by(config_id=c.id, section_id=section_id).first()
|
|
if not section:
|
|
raise HTTPException(status_code=404, detail=f"章节 {section_id} 不存在")
|
|
section.title = req.title
|
|
section.subtitle = req.subtitle
|
|
section.icon = req.icon
|
|
section.content = req.content
|
|
section.data = req.data
|
|
db.commit()
|
|
return {"message": "更新成功"}
|