- 后端: 新增 dislike_count 字段(模型/Schema/数据库迁移)
- 后端: 新增 POST /api/jokes/{id}/dislike 端点
- 后端: 管理后台统计新增 total_dislikes
- 前端: 新增 useVote 组合函数(localStorage 持久化防重复)
- 前端: JokeCard 列表卡片新增 👍/👎 可点击投票按钮
- 前端: 详情页 ❤️ 改为 👍 赞一下 / 👎 踩一脚 双按钮
58 lines
1.9 KiB
Python
58 lines
1.9 KiB
Python
from fastapi import FastAPI
|
||
from fastapi.middleware.cors import CORSMiddleware
|
||
|
||
from app.config import API_TITLE, API_VERSION
|
||
from app.routers import jokes_router, categories_router, auth_router, admin_router, settings_router, links_router, feedback_router, generate_router
|
||
from app.routers.submit import router as submit_router
|
||
from app.database import Base, engine
|
||
from app.models.setting import AiSetting
|
||
from app.models.link import Link
|
||
from app.models.feedback import Feedback # 注册模型,确保 create_all 能看到
|
||
|
||
# 创建新表(如果不存在)
|
||
Base.metadata.create_all(bind=engine)
|
||
|
||
# 对已有表新增字段的兼容迁移(SQLite 不支持 ALTER TABLE ADD COLUMN IF NOT EXISTS)
|
||
def _migrate_db():
|
||
from sqlalchemy import inspect, text
|
||
inspector = inspect(engine)
|
||
columns = [c["name"] for c in inspector.get_columns("jokes")]
|
||
if "dislike_count" not in columns:
|
||
with engine.connect() as conn:
|
||
conn.execute(text("ALTER TABLE jokes ADD COLUMN dislike_count INTEGER DEFAULT 0"))
|
||
conn.commit()
|
||
|
||
_migrate_db()
|
||
|
||
# Create FastAPI application
|
||
app = FastAPI(title=API_TITLE, version=API_VERSION)
|
||
|
||
# Configure CORS
|
||
app.add_middleware(
|
||
CORSMiddleware,
|
||
allow_origins=["*"],
|
||
allow_credentials=True,
|
||
allow_methods=["*"],
|
||
allow_headers=["*"],
|
||
)
|
||
|
||
# Mount routers
|
||
app.include_router(jokes_router, prefix="/api")
|
||
app.include_router(categories_router, prefix="/api")
|
||
app.include_router(auth_router, prefix="/api")
|
||
app.include_router(admin_router, prefix="/api")
|
||
app.include_router(settings_router, prefix="/api")
|
||
app.include_router(links_router, prefix="/api")
|
||
app.include_router(feedback_router, prefix="/api")
|
||
app.include_router(generate_router, prefix="/api")
|
||
app.include_router(submit_router, prefix="/api/jokes") # 公开提交接口
|
||
|
||
|
||
@app.get("/")
|
||
def root():
|
||
return {"message": "欢迎使用笑话大全 API"}
|
||
|
||
|
||
@app.get("/health")
|
||
def health_check():
|
||
return {"status": "healthy"} |