first commit
This commit is contained in:
62
app/chats/repository.py
Normal file
62
app/chats/repository.py
Normal file
@@ -0,0 +1,62 @@
|
||||
from sqlalchemy import Select, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.chats.models import Chat, ChatMember, ChatMemberRole, ChatType
|
||||
|
||||
|
||||
async def create_chat(db: AsyncSession, *, chat_type: ChatType, title: str | None) -> Chat:
|
||||
chat = Chat(type=chat_type, title=title)
|
||||
db.add(chat)
|
||||
await db.flush()
|
||||
return chat
|
||||
|
||||
|
||||
async def add_chat_member(db: AsyncSession, *, chat_id: int, user_id: int, role: ChatMemberRole) -> ChatMember:
|
||||
member = ChatMember(chat_id=chat_id, user_id=user_id, role=role)
|
||||
db.add(member)
|
||||
await db.flush()
|
||||
return member
|
||||
|
||||
|
||||
def _user_chats_query(user_id: int) -> Select[tuple[Chat]]:
|
||||
return (
|
||||
select(Chat)
|
||||
.join(ChatMember, ChatMember.chat_id == Chat.id)
|
||||
.where(ChatMember.user_id == user_id)
|
||||
.order_by(Chat.id.desc())
|
||||
)
|
||||
|
||||
|
||||
async def list_user_chats(db: AsyncSession, *, user_id: int, limit: int = 50, before_id: int | None = None) -> list[Chat]:
|
||||
query = _user_chats_query(user_id).limit(limit)
|
||||
if before_id is not None:
|
||||
query = query.where(Chat.id < before_id)
|
||||
result = await db.execute(query)
|
||||
return list(result.scalars().all())
|
||||
|
||||
|
||||
async def get_chat_by_id(db: AsyncSession, chat_id: int) -> Chat | None:
|
||||
result = await db.execute(select(Chat).where(Chat.id == chat_id))
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
|
||||
async def get_chat_member(db: AsyncSession, *, chat_id: int, user_id: int) -> ChatMember | None:
|
||||
result = await db.execute(
|
||||
select(ChatMember).where(
|
||||
ChatMember.chat_id == chat_id,
|
||||
ChatMember.user_id == user_id,
|
||||
)
|
||||
)
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
|
||||
async def list_chat_members(db: AsyncSession, *, chat_id: int) -> list[ChatMember]:
|
||||
result = await db.execute(select(ChatMember).where(ChatMember.chat_id == chat_id).order_by(ChatMember.id.asc()))
|
||||
return list(result.scalars().all())
|
||||
|
||||
|
||||
async def list_user_chat_ids(db: AsyncSession, *, user_id: int) -> list[int]:
|
||||
result = await db.execute(
|
||||
select(ChatMember.chat_id).where(ChatMember.user_id == user_id).order_by(ChatMember.chat_id.asc())
|
||||
)
|
||||
return list(result.scalars().all())
|
||||
Reference in New Issue
Block a user