ref #13: in-memory metadata repositories (S02)

Made-with: Cursor
This commit is contained in:
David Shvarts 2026-04-07 20:37:00 +03:00
parent 5381c997e2
commit a8ab0cfcbf
4 changed files with 236 additions and 0 deletions

24
repository/chat.py Normal file
View file

@ -0,0 +1,24 @@
from uuid import UUID
from domain.chat import Chat
from usecase.interface import ChatRepository
class InMemoryChatRepository(ChatRepository):
def __init__(self) -> None:
self._by_id: dict[UUID, Chat] = {}
def get(self, chat_id: UUID) -> Chat | None:
return self._by_id.get(chat_id)
def list_by_workspace_id(self, workspace_id: UUID) -> list[Chat]:
return sorted(
(c for c in self._by_id.values() if c.workspace_id == workspace_id),
key=lambda c: (c.created_at, c.chat_id),
)
def save(self, chat: Chat) -> None:
self._by_id[chat.chat_id] = chat
def delete(self, chat_id: UUID) -> None:
self._by_id.pop(chat_id, None)

57
repository/chat_file.py Normal file
View file

@ -0,0 +1,57 @@
from uuid import UUID
from domain.chat import ChatAttachmentName, ChatFile
from usecase.interface import ChatFileRepository
class InMemoryChatFileRepository(ChatFileRepository):
def __init__(self) -> None:
self._by_id: dict[UUID, ChatFile] = {}
self._by_chat_and_name: dict[tuple[UUID, str], UUID] = {}
def get(self, file_id: UUID) -> ChatFile | None:
return self._by_id.get(file_id)
def get_by_chat_id_and_name(
self,
chat_id: UUID,
name: ChatAttachmentName,
) -> ChatFile | None:
fid = self._by_chat_and_name.get((chat_id, name.value))
if fid is None:
return None
return self._by_id.get(fid)
def list_by_chat_id(self, chat_id: UUID) -> list[ChatFile]:
return sorted(
(f for f in self._by_id.values() if f.chat_id == chat_id),
key=lambda f: (f.created_at, f.file_id),
)
def save(self, chat_file: ChatFile) -> None:
key = (chat_file.chat_id, chat_file.name.value)
existing_at_key = self._by_chat_and_name.get(key)
if existing_at_key is not None and existing_at_key != chat_file.file_id:
self._by_id.pop(existing_at_key, None)
previous = self._by_id.get(chat_file.file_id)
if previous is not None:
prev_key = (previous.chat_id, previous.name.value)
if self._by_chat_and_name.get(prev_key) == previous.file_id:
del self._by_chat_and_name[prev_key]
self._by_id[chat_file.file_id] = chat_file
self._by_chat_and_name[key] = chat_file.file_id
def delete(self, file_id: UUID) -> None:
chat_file = self._by_id.pop(file_id, None)
if chat_file is None:
return
key = (chat_file.chat_id, chat_file.name.value)
if self._by_chat_and_name.get(key) == file_id:
del self._by_chat_and_name[key]
def delete_by_chat_id(self, chat_id: UUID) -> None:
file_ids = [f.file_id for f in self._by_id.values() if f.chat_id == chat_id]
for fid in file_ids:
self.delete(fid)

26
repository/workspace.py Normal file
View file

@ -0,0 +1,26 @@
from uuid import UUID
from domain.workspace import Workspace
from usecase.interface import WorkspaceRepository
class InMemoryWorkspaceRepository(WorkspaceRepository):
def __init__(self) -> None:
self._by_id: dict[UUID, Workspace] = {}
self._user_id_to_workspace_id: dict[UUID, UUID] = {}
def get(self, workspace_id: UUID) -> Workspace | None:
return self._by_id.get(workspace_id)
def get_by_user_id(self, user_id: UUID) -> Workspace | None:
wid = self._user_id_to_workspace_id.get(user_id)
if wid is None:
return None
return self._by_id.get(wid)
def save(self, workspace: Workspace) -> None:
existing_wid = self._user_id_to_workspace_id.get(workspace.user_id)
if existing_wid is not None and existing_wid != workspace.workspace_id:
self._by_id.pop(existing_wid, None)
self._by_id[workspace.workspace_id] = workspace
self._user_id_to_workspace_id[workspace.user_id] = workspace.workspace_id