forked from mtusi2026/mtusi2026lab2
add migrations, create domain and crud layers
This commit is contained in:
@@ -15,7 +15,7 @@ T = TypeVar("T")
|
||||
|
||||
|
||||
class Positioned(Protocol):
|
||||
"""Всё, у чего есть позиция в порядке (created_at, id): ORM-модели и схемы."""
|
||||
"""Объект с позицией в порядке (created_at, id)."""
|
||||
|
||||
@property
|
||||
def id(self) -> UUID: ...
|
||||
@@ -30,6 +30,7 @@ def encode_cursor(
|
||||
created_at: datetime,
|
||||
item_id: UUID,
|
||||
) -> str:
|
||||
"""Кодирует позицию и ресурс, к которому она привязана, в непрозрачный cursor."""
|
||||
payload = {
|
||||
"kind": kind,
|
||||
"resource_id": str(resource_id),
|
||||
@@ -41,6 +42,7 @@ def encode_cursor(
|
||||
|
||||
|
||||
def decode_cursor(cursor: str, kind: str, resource_id: UUID) -> Position:
|
||||
"""Возвращает позицию из cursor; битый или чужой cursor даёт 400."""
|
||||
try:
|
||||
payload = json.loads(base64.urlsafe_b64decode(cursor))
|
||||
cursor_kind = payload["kind"]
|
||||
@@ -61,6 +63,7 @@ def decode_cursor(cursor: str, kind: str, resource_id: UUID) -> Position:
|
||||
|
||||
|
||||
def split_page(rows: list[T], limit: int) -> tuple[list[T], bool]:
|
||||
"""Отделяет страницу от лишней строки limit + 1, по которой видно продолжение."""
|
||||
has_next = len(rows) > limit
|
||||
items = rows[:limit]
|
||||
return items, has_next
|
||||
@@ -72,6 +75,7 @@ def build_next_cursor(
|
||||
items: Sequence[Positioned],
|
||||
has_next: bool,
|
||||
) -> str | None:
|
||||
"""Возвращает cursor на последний элемент страницы или None, если она последняя."""
|
||||
if not has_next:
|
||||
return None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user