diff --git a/app/repositories/chat_repository.py b/app/repositories/chat_repository.py new file mode 100644 index 00000000..e5cd838e --- /dev/null +++ b/app/repositories/chat_repository.py @@ -0,0 +1,71 @@ +import uuid + +from sqlalchemy import select +from sqlalchemy.orm import Session, selectinload + +from app.db.models import Chat, ChatMember + + +class ChatRepository: + + def __init__(self, db: Session): + self.db = db + + def get_by_id(self, chat_id: uuid.UUID) -> Chat | None: + statement = ( + select(Chat) + .options(selectinload(Chat.members)) + .where(Chat.id == chat_id) + ) + + return self.db.scalar(statement) + + def get_by_user( + self, + user_id: uuid.UUID, + limit: int = 50, + ) -> list[Chat]: + statement = ( + 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) + ) + + return list(self.db.scalars(statement).all()) + + def is_member( + self, + chat_id: uuid.UUID, + user_id: uuid.UUID, + ) -> bool: + statement = select(ChatMember).where( + ChatMember.chat_id == chat_id, + ChatMember.user_id == user_id, + ) + + return self.db.scalar(statement) is not None + + def create( + self, + title: str, + member_ids: list[uuid.UUID], + ) -> Chat: + chat = Chat(title=title) + + self.db.add(chat) + self.db.flush() + + memberships = [ + ChatMember( + chat_id=chat.id, + user_id=user_id, + ) + for user_id in member_ids + ] + + self.db.add_all(memberships) + self.db.flush() + + return chat \ No newline at end of file diff --git a/app/repositories/message_repository.py b/app/repositories/message_repository.py new file mode 100644 index 00000000..6698ed1d --- /dev/null +++ b/app/repositories/message_repository.py @@ -0,0 +1,71 @@ +import uuid + +from datetime import datetime + +from sqlalchemy import and_, or_, select +from sqlalchemy.orm import Session + +from app.db.models import Message + + +class MessageRepository: + + def __init__(self, db: Session): + self.db = db + + def get_by_id( + self, + message_id: uuid.UUID, + ) -> Message | None: + statement = select(Message).where( + Message.id == message_id + ) + + return self.db.scalar(statement) + + def create( + self, + chat_id: uuid.UUID, + sender_id: uuid.UUID, + text: str, + ) -> Message: + message = Message( + chat_id=chat_id, + sender_id=sender_id, + text=text, + ) + + self.db.add(message) + self.db.flush() + + return message + + def get_history( + self, + chat_id: uuid.UUID, + limit: int = 50, + cursor_created_at: datetime | None = None, + cursor_id: uuid.UUID | None = None, + ) -> list[Message]: + statement = select(Message).where( + Message.chat_id == chat_id + ) + + if cursor_created_at is not None and cursor_id is not None: + statement = statement.where( + or_( + Message.created_at > cursor_created_at, + and_( + Message.created_at == cursor_created_at, + Message.id > cursor_id, + ), + ) + ) + + statement = ( + statement + .order_by(Message.created_at.asc(), Message.id.asc()) + .limit(limit) + ) + + return list(self.db.scalars(statement).all()) \ No newline at end of file diff --git a/app/repositories/session_repository.py b/app/repositories/session_repository.py new file mode 100644 index 00000000..7366b170 --- /dev/null +++ b/app/repositories/session_repository.py @@ -0,0 +1,78 @@ +import uuid + +from datetime import datetime, timezone + +from sqlalchemy import delete, select +from sqlalchemy.orm import Session + +from app.db.models import Session as UserSession + + +class SessionRepository: + + def __init__(self, db: Session): + self.db = db + + def create( + self, + user_id: uuid.UUID, + token_hash: str, + expires_at: datetime, + ) -> UserSession: + session = UserSession( + user_id=user_id, + token_hash=token_hash, + expires_at=expires_at, + ) + + self.db.add(session) + self.db.flush() + + return session + + def get_by_token_hash( + self, + token_hash: str, + ) -> UserSession | None: + statement = select(UserSession).where( + UserSession.token_hash == token_hash + ) + + return self.db.scalar(statement) + + def get_valid_session( + self, + token_hash: str, + ) -> UserSession | None: + now = datetime.now(timezone.utc) + + statement = select(UserSession).where( + UserSession.token_hash == token_hash, + UserSession.expires_at > now, + ) + + return self.db.scalar(statement) + + def delete_by_token_hash( + self, + token_hash: str, + ) -> bool: + statement = ( + delete(UserSession) + .where(UserSession.token_hash == token_hash) + .returning(UserSession.id) + ) + + return self.db.scalar(statement) is not None + + def delete_expired(self) -> int: + now = datetime.now(timezone.utc) + + statement = ( + delete(UserSession) + .where(UserSession.expires_at <= now) + ) + + result = self.db.execute(statement) + + return result.rowcount \ No newline at end of file diff --git a/app/repositories/user_repository.py b/app/repositories/user_repository.py new file mode 100644 index 00000000..4569dcd1 --- /dev/null +++ b/app/repositories/user_repository.py @@ -0,0 +1,41 @@ +import uuid + +from sqlalchemy import select +from sqlalchemy.orm import Session + +from app.db.models import User + + +class UserRepository: + + def __init__(self, db: Session): + self.db = db + + def get_by_id(self, user_id: uuid.UUID) -> User | None: + statement = select(User).where(User.id == user_id) + + return self.db.scalar(statement) + + def get_by_username(self, username: str) -> User | None: + statement = select(User).where( + User.username == username + ) + + return self.db.scalar(statement) + + def create( + self, + username: str, + display_name: str, + password_hash: str, + ) -> User: + user = User( + username=username, + display_name=display_name, + password_hash=password_hash, + ) + + self.db.add(user) + self.db.flush() + + return user \ No newline at end of file