分页优化
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user