119 行
4.6 KiB
Python
119 行
4.6 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.destination import DestinationConfig, DestinationItem, DestinationDimension, DestinationHonestItem
|
|
from app.schemas.destination import (
|
|
DestinationConfigSchema, DestinationItemSchema, DestinationItemResponse,
|
|
DestinationDimensionSchema, DestinationDimensionResponse,
|
|
DestinationHonestItemSchema, DestinationHonestItemResponse,
|
|
)
|
|
|
|
router = APIRouter(prefix="/api/destinations", tags=["目的地对比"], dependencies=[Depends(get_current_user)])
|
|
|
|
|
|
def _get_config(db: Session) -> DestinationConfig:
|
|
config = db.query(DestinationConfig).first()
|
|
if not config:
|
|
config = DestinationConfig()
|
|
db.add(config)
|
|
db.flush()
|
|
return config
|
|
|
|
|
|
@router.get("/config", response_model=DestinationConfigSchema)
|
|
def get_config(db: Session = Depends(get_db)):
|
|
c = _get_config(db)
|
|
return DestinationConfigSchema(
|
|
title=c.title, subtitle=c.subtitle, intro=c.intro,
|
|
closing_title=c.closing_title, closing_text=c.closing_text,
|
|
data_sources=c.data_sources, honest_title=c.honest_title, honest_subtitle=c.honest_subtitle,
|
|
)
|
|
|
|
|
|
@router.put("/config")
|
|
def update_config(req: DestinationConfigSchema, db: Session = Depends(get_db)):
|
|
c = _get_config(db)
|
|
c.title = req.title
|
|
c.subtitle = req.subtitle
|
|
c.intro = req.intro
|
|
c.closing_title = req.closing_title
|
|
c.closing_text = req.closing_text
|
|
c.data_sources = req.data_sources
|
|
c.honest_title = req.honest_title
|
|
c.honest_subtitle = req.honest_subtitle
|
|
db.commit()
|
|
return {"message": "更新成功"}
|
|
|
|
|
|
# --- Destination items ---
|
|
@router.get("/items", response_model=list[DestinationItemResponse])
|
|
def list_items(db: Session = Depends(get_db)):
|
|
rows = db.query(DestinationItem).order_by(DestinationItem.sort_order).all()
|
|
return [DestinationItemResponse(id=d.id, dest_id=d.dest_id, name=d.name, tag=d.tag, highlight=d.highlight, sort_order=d.sort_order) for d in rows]
|
|
|
|
|
|
@router.put("/items")
|
|
def update_items(items: list[DestinationItemSchema], db: Session = Depends(get_db)):
|
|
db.query(DestinationItem).delete()
|
|
for i, d in enumerate(items):
|
|
db.add(DestinationItem(dest_id=d.dest_id, name=d.name, tag=d.tag, highlight=d.highlight, sort_order=i))
|
|
db.commit()
|
|
return {"message": "目的地更新成功"}
|
|
|
|
|
|
# --- Dimensions ---
|
|
@router.get("/dimensions", response_model=list[DestinationDimensionResponse])
|
|
def list_dimensions(db: Session = Depends(get_db)):
|
|
rows = db.query(DestinationDimension).order_by(DestinationDimension.sort_order).all()
|
|
return [DestinationDimensionResponse(id=d.id, label=d.label, icon=d.icon, values=d.values or [], sort_order=d.sort_order) for d in rows]
|
|
|
|
|
|
@router.post("/dimensions", response_model=DestinationDimensionResponse, status_code=201)
|
|
def create_dimension(req: DestinationDimensionSchema, db: Session = Depends(get_db)):
|
|
d = DestinationDimension(label=req.label, icon=req.icon, values=req.values)
|
|
db.add(d)
|
|
db.commit()
|
|
db.refresh(d)
|
|
return DestinationDimensionResponse(id=d.id, label=d.label, icon=d.icon, values=d.values or [], sort_order=d.sort_order)
|
|
|
|
|
|
@router.put("/dimensions/{d_id}")
|
|
def update_dimension(d_id: int, req: DestinationDimensionSchema, db: Session = Depends(get_db)):
|
|
d = db.query(DestinationDimension).get(d_id)
|
|
if not d:
|
|
raise HTTPException(status_code=404, detail="对比维度不存在")
|
|
d.label = req.label
|
|
d.icon = req.icon
|
|
d.values = req.values
|
|
db.commit()
|
|
return {"message": "更新成功"}
|
|
|
|
|
|
@router.delete("/dimensions/{d_id}")
|
|
def delete_dimension(d_id: int, db: Session = Depends(get_db)):
|
|
d = db.query(DestinationDimension).get(d_id)
|
|
if not d:
|
|
raise HTTPException(status_code=404, detail="对比维度不存在")
|
|
db.delete(d)
|
|
db.commit()
|
|
return {"message": "删除成功"}
|
|
|
|
|
|
# --- Honest items ---
|
|
@router.get("/honest-items", response_model=list[DestinationHonestItemResponse])
|
|
def list_honest_items(db: Session = Depends(get_db)):
|
|
rows = db.query(DestinationHonestItem).order_by(DestinationHonestItem.sort_order).all()
|
|
return [DestinationHonestItemResponse(id=h.id, text=h.text, sort_order=h.sort_order) for h in rows]
|
|
|
|
|
|
@router.put("/honest-items")
|
|
def update_honest_items(items: list[DestinationHonestItemSchema], db: Session = Depends(get_db)):
|
|
db.query(DestinationHonestItem).delete()
|
|
for i, h in enumerate(items):
|
|
db.add(DestinationHonestItem(text=h.text, sort_order=i))
|
|
db.commit()
|
|
return {"message": "坦诚说明更新成功"}
|