forked from mtusi2026/mtusi2026lab2
init commit: create skeleton from lab1
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
from uuid import UUID
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.domain.pagination import (
|
||||
KIND_CHATS,
|
||||
build_next_cursor,
|
||||
decode_cursor,
|
||||
split_page,
|
||||
)
|
||||
from app.errors.exceptions import invalid_request
|
||||
from app.models.chat import Chat as ChatModel
|
||||
from app.schemas.chat import Chat, ChatPage, CreateChat
|
||||
from app.schemas.user import User
|
||||
from app.storage.types import Position
|
||||
|
||||
|
||||
def to_chat_schema(chat: ChatModel) -> Chat:
|
||||
"""ORM -> схема. member_ids собираются из chat.members (relationship).
|
||||
|
||||
TODO: Chat(id=chat.id, title=chat.title,
|
||||
member_ids=[m.user_id for m in chat.members], created_at=chat.created_at)
|
||||
"""
|
||||
raise NotImplementedError("TODO")
|
||||
|
||||
|
||||
def validate_new_chat(creator: User, new_chat: CreateChat) -> None:
|
||||
"""Проверки, не требующие БД (перенесено из lab1 как есть)."""
|
||||
member_ids = new_chat.member_ids
|
||||
|
||||
if creator.id not in member_ids:
|
||||
raise invalid_request("Создатель чата должен входить в member_ids")
|
||||
|
||||
if len(set(member_ids)) != len(member_ids):
|
||||
raise invalid_request("member_ids содержит дубликаты")
|
||||
|
||||
|
||||
def create_chat(session: Session, creator: User, new_chat: CreateChat) -> Chat:
|
||||
"""POST /chats.
|
||||
|
||||
TODO:
|
||||
1. validate_new_chat(creator, new_chat)
|
||||
2. known = storage.users.get_existing_user_ids(session, new_chat.member_ids);
|
||||
если known != set(member_ids) -> invalid_request("member_ids содержит неизвестного пользователя")
|
||||
3. storage.chats.create_chat(...) + session.commit() — одна транзакция:
|
||||
чат и все участники появляются вместе или не появляются вовсе.
|
||||
4. return to_chat_schema(chat)
|
||||
"""
|
||||
raise NotImplementedError("TODO")
|
||||
|
||||
|
||||
def get_chat(session: Session, user: User, chat_id: UUID) -> Chat:
|
||||
"""GET /chats/{id}.
|
||||
|
||||
TODO: storage.chats.get_chat_for_member(...); None -> raise not_found()
|
||||
(и для чужого, и для несуществующего чата — одинаково).
|
||||
"""
|
||||
raise NotImplementedError("TODO")
|
||||
|
||||
|
||||
def list_chats(
|
||||
session: Session,
|
||||
user: User,
|
||||
cursor: str | None,
|
||||
limit: int,
|
||||
) -> ChatPage:
|
||||
"""GET /chats — keyset-пагинация (логика курсора перенесена из lab1).
|
||||
|
||||
TODO: раскомментируйте и подключите storage.
|
||||
"""
|
||||
after: Position | None = None
|
||||
if cursor is not None:
|
||||
after = decode_cursor(cursor, KIND_CHATS, user.id)
|
||||
|
||||
# rows = storage.chats.list_chats_for_user(session, user.id, after, limit + 1)
|
||||
# items, has_next = split_page(rows, limit)
|
||||
# next_cursor = build_next_cursor(KIND_CHATS, user.id, items, has_next)
|
||||
# return ChatPage(items=[to_chat_schema(c) for c in items], next_cursor=next_cursor)
|
||||
_ = (split_page, build_next_cursor, after)
|
||||
raise NotImplementedError("TODO")
|
||||
Reference in New Issue
Block a user