fix(history): stabilize download history pagination

This commit is contained in:
jxxghp
2026-08-03 08:45:35 +08:00
parent d8adb4fbfe
commit 52ca375f3d
4 changed files with 104 additions and 4 deletions

View File

@@ -140,7 +140,7 @@ async def download_history(
_: schemas.TokenPayload = Depends(verify_token),
) -> Any:
"""
查询下载历史记录
按下载时间倒序查询下载历史记录
"""
return await DownloadHistory.async_list_by_page(db, page, count)

View File

@@ -148,14 +148,25 @@ class DownloadHistory(Base):
def list_by_page(
cls, db: Session, page: Optional[int] = 1, count: Optional[int] = 30
):
return db.query(DownloadHistory).offset((page - 1) * count).limit(count).all()
return (
db.query(DownloadHistory)
.order_by(DownloadHistory.date.desc(), DownloadHistory.id.desc())
.offset((page - 1) * count)
.limit(count)
.all()
)
@classmethod
@async_db_query
async def async_list_by_page(
cls, db: AsyncSession, page: Optional[int] = 1, count: Optional[int] = 30
):
result = await db.execute(select(cls).offset((page - 1) * count).limit(count))
result = await db.execute(
select(cls)
.order_by(cls.date.desc(), cls.id.desc())
.offset((page - 1) * count)
.limit(count)
)
return result.scalars().all()
@classmethod