33 行
1.2 KiB
Python
33 行
1.2 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.seo import SeoPage
|
|
from app.schemas.seo import SeoPageSchema, SeoPageResponse
|
|
|
|
router = APIRouter(prefix="/api/seo", tags=["SEO配置"], dependencies=[Depends(get_current_user)])
|
|
|
|
|
|
@router.get("/pages", response_model=list[SeoPageResponse])
|
|
def list_pages(db: Session = Depends(get_db)):
|
|
pages = db.query(SeoPage).all()
|
|
return [SeoPageResponse.model_validate(p) for p in pages]
|
|
|
|
|
|
@router.put("/pages/{page_key}", response_model=SeoPageResponse)
|
|
def update_page(page_key: str, req: SeoPageSchema, db: Session = Depends(get_db)):
|
|
page = db.query(SeoPage).filter(SeoPage.page_key == page_key).first()
|
|
if not page:
|
|
raise HTTPException(status_code=404, detail=f"页面 {page_key} 不存在")
|
|
page.title = req.title
|
|
page.description = req.description
|
|
page.h1 = req.h1
|
|
page.keywords = req.keywords
|
|
page.og_image = req.og_image
|
|
page.tldr = req.tldr
|
|
db.commit()
|
|
db.refresh(page)
|
|
return SeoPageResponse.model_validate(page)
|