Files
life-echo/api/app/core/pagination.py
2026-03-19 14:36:40 +08:00

30 lines
687 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
通用分页offset/limit 参数与分页结果结构。
各 feature 的 router 按需使用,成功响应直接返回 Pydantic model不强制包装。
"""
from typing import Generic, TypeVar
from pydantic import BaseModel
T = TypeVar("T")
class PageParams(BaseModel):
"""常用分页查询参数router 中通过 Depends 注入)。"""
offset: int = 0
limit: int = 20
def clamp_limit(self, max_limit: int = 100) -> int:
return min(max(1, self.limit), max_limit)
class Page(BaseModel, Generic[T]):
"""分页结果items + total可选"""
items: list[T]
total: int | None = None
offset: int = 0
limit: int = 20