репозитории users, chats, messages

This commit is contained in:
2026-10-01 00:06:11 +03:00
parent 82e8fee7f9
commit c4fc878861
3 changed files with 145 additions and 0 deletions
+73
View File
@@ -0,0 +1,73 @@
from datetime import datetime
from uuid import UUID
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from ..db.models import Chat, ChatMember, User
async def create_atomic(
db: AsyncSession, title: str, member_ids: list[UUID]
) -> Chat:
async with db.begin():
found = (
(await db.execute(select(User.id).where(User.id.in_(member_ids))))
.scalars()
.all()
)
if len(set(found)) != len(set(member_ids)):
raise ValueError("member_not_found")
chat = Chat(title=title)
db.add(chat)
await db.flush()
for uid in member_ids:
db.add(ChatMember(chat_id=chat.id, user_id=uid))
await db.refresh(chat)
return chat
async def get_if_member(
db: AsyncSession, chat_id: UUID, user_id: UUID
) -> Chat | None:
stmt = (
select(Chat)
.join(ChatMember, ChatMember.chat_id == Chat.id)
.where(Chat.id == chat_id, ChatMember.user_id == user_id)
)
return (await db.execute(stmt)).scalar_one_or_none()
async def list_member_ids(db: AsyncSession, chat_id: UUID) -> list[UUID]:
stmt = select(ChatMember.user_id).where(ChatMember.chat_id == chat_id)
return list((await db.execute(stmt)).scalars().all())
async def list_for_user(
db: AsyncSession,
user_id: UUID,
limit: int,
cursor: tuple[datetime, UUID] | None,
) -> tuple[list[Chat], tuple[datetime, UUID] | None]:
stmt = (
select(Chat)
.join(ChatMember, ChatMember.chat_id == Chat.id)
.where(ChatMember.user_id == user_id)
.order_by(Chat.created_at.asc(), Chat.id.asc())
.limit(limit + 1)
)
if cursor is not None:
c_created, c_id = cursor
stmt = stmt.where(
(Chat.created_at > c_created)
| ((Chat.created_at == c_created) & (Chat.id > c_id))
)
rows = list((await db.execute(stmt)).scalars().all())
next_cursor = None
if len(rows) > limit:
rows = rows[:limit]
last = rows[-1]
next_cursor = (last.created_at, last.id)
return rows, next_cursor
+44
View File
@@ -0,0 +1,44 @@
from datetime import datetime
from uuid import UUID
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from ..db.models import Message
async def create(
db: AsyncSession, chat_id: UUID, sender_id: UUID, text: str
) -> Message:
msg = Message(chat_id=chat_id, sender_id=sender_id, text=text)
db.add(msg)
await db.commit()
await db.refresh(msg)
return msg
async def list_page(
db: AsyncSession,
chat_id: UUID,
limit: int,
cursor: tuple[datetime, UUID] | None,
) -> tuple[list[Message], tuple[datetime, UUID] | None]:
stmt = (
select(Message)
.where(Message.chat_id == chat_id)
.order_by(Message.created_at.asc(), Message.id.asc())
.limit(limit + 1)
)
if cursor is not None:
c_created, c_id = cursor
stmt = stmt.where(
(Message.created_at > c_created)
| ((Message.created_at == c_created) & (Message.id > c_id))
)
rows = list((await db.execute(stmt)).scalars().all())
next_cursor = None
if len(rows) > limit:
rows = rows[:limit]
last = rows[-1]
next_cursor = (last.created_at, last.id)
return rows, next_cursor
+28
View File
@@ -0,0 +1,28 @@
from uuid import UUID
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from ..db.models import User
async def get_by_username(db: AsyncSession, username: str) -> User | None:
return (
await db.execute(select(User).where(User.username == username))
).scalar_one_or_none()
async def get_by_id(db: AsyncSession, user_id: UUID) -> User | None:
return await db.get(User, user_id)
async def create(
db: AsyncSession, username: str, display_name: str, password_hash: str
) -> User:
user = User(
username=username, display_name=display_name, password_hash=password_hash
)
db.add(user)
await db.commit()
await db.refresh(user)
return user