分页优化

This commit is contained in:
2026-08-26 13:09:32 +08:00
parent 9224ac9bbf
commit d6656fedc5
11 changed files with 464 additions and 45 deletions
+22 -5
View File
@@ -1,7 +1,9 @@
"""
公司黑名单路由:拉黑后 RPA 不再投递该公司的任何职位
"""
from fastapi import APIRouter, Depends, HTTPException
from datetime import timedelta
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.orm import Session
from ..auth import get_current_user
@@ -12,15 +14,30 @@ from ..schemas import BlacklistIn, BlacklistOut, Message
router = APIRouter(prefix="/api/blacklist", tags=["公司黑名单"])
@router.get("", response_model=list[BlacklistOut])
@router.get("", response_model=dict)
def list_blacklist(
page: int = Query(1, ge=1, description="页码(1 起)"),
page_size: int = Query(20, ge=1, le=500, description="每页条数"),
current_user: User = Depends(get_current_user),
db: Session = Depends(get_db),
):
"""查询当前用户的黑名单列表(按拉黑时间倒序)"""
return db.query(CompanyBlacklist).filter(
"""查询当前用户的黑名单列表(按拉黑时间倒序,分页返回)"""
query = db.query(CompanyBlacklist).filter(
CompanyBlacklist.user_id == current_user.id,
).order_by(CompanyBlacklist.created_at.desc()).all()
)
total = query.count()
rows = query.order_by(CompanyBlacklist.created_at.desc()) \
.offset((page - 1) * page_size).limit(page_size).all()
items = [{
"id": r.id,
"company": r.company,
"created_at": (r.created_at + timedelta(hours=8)).strftime("%Y-%m-%d %H:%M:%S"),
} for r in rows]
return {
"items": items, "total": total,
"page": page, "page_size": page_size,
"pages": (total + page_size - 1) // page_size if page_size else 1,
}
@router.post("", response_model=BlacklistOut)