2023-01-27 21:41:08 +01:00
|
|
|
from __future__ import annotations
|
2023-02-28 10:21:08 +01:00
|
|
|
|
|
|
|
from math import ceil
|
2023-01-27 21:41:08 +01:00
|
|
|
from typing import TypeVar, Generic, Sequence
|
|
|
|
|
|
|
|
from fastapi_pagination import Params
|
2023-02-28 10:21:08 +01:00
|
|
|
from fastapi_pagination.bases import AbstractParams, BasePage
|
2023-01-27 21:41:08 +01:00
|
|
|
from pydantic import conint
|
|
|
|
|
|
|
|
T = TypeVar("T")
|
|
|
|
|
|
|
|
class Page(BasePage[T], Generic[T]):
|
|
|
|
page: conint(ge=1) # type: ignore
|
|
|
|
size: conint(ge=1)
|
|
|
|
totalPage: conint(ge=0)
|
|
|
|
hasMore: bool
|
|
|
|
|
|
|
|
|
|
|
|
__params_type__ = Params # Set params related to Page
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create(
|
|
|
|
cls,
|
|
|
|
items: Sequence[T],
|
|
|
|
total: int,
|
|
|
|
params: AbstractParams,
|
|
|
|
) -> Page[T]:
|
2023-02-28 10:21:08 +01:00
|
|
|
|
2023-01-27 21:41:08 +01:00
|
|
|
totalPage = ceil(total/params.size)
|
|
|
|
return cls(
|
|
|
|
total=total,
|
|
|
|
items=items,
|
|
|
|
page=params.page,
|
|
|
|
size=params.size,
|
|
|
|
totalPage = totalPage,
|
|
|
|
hasMore= params.page < totalPage
|
|
|
|
)
|
|
|
|
return {
|
|
|
|
"items": items,
|
|
|
|
"total": total,
|
|
|
|
"size": params,
|
|
|
|
}
|
|
|
|
return cls(results=items)
|