from sqlalchemy.orm import Session from packages.adapters.sqlalchemy_impl.models import AssetLibraryModel from packages.domain import AssetLibrary, AssetLibraryKind class SQLAlchemyAssetLibraryRepository: def __init__(self, session: Session): self.session = session def get(self, library_id: str) -> AssetLibrary | None: model = self.session.query(AssetLibraryModel).filter(AssetLibraryModel.id == library_id).first() if model is None: return None return AssetLibrary( id=model.id, workspace_id=model.workspace_id, project_id=model.project_id, name=model.name, kind=AssetLibraryKind(model.kind), asset_count=int(model.asset_count or 0), total_size=int(model.total_size or 0), created_at=model.created_at, updated_at=model.updated_at, ) def list_by_project(self, project_id: str, kind: AssetLibraryKind | None = None) -> list[AssetLibrary]: query = self.session.query(AssetLibraryModel).filter(AssetLibraryModel.project_id == project_id) if kind is not None: query = query.filter(AssetLibraryModel.kind == kind.value) models = query.all() return [ AssetLibrary( id=model.id, workspace_id=model.workspace_id, project_id=model.project_id, name=model.name, kind=AssetLibraryKind(model.kind), asset_count=int(model.asset_count or 0), total_size=int(model.total_size or 0), created_at=model.created_at, updated_at=model.updated_at, ) for model in models ] def create(self, library: AssetLibrary) -> AssetLibrary: model = AssetLibraryModel( id=library.id, workspace_id=library.workspace_id, project_id=library.project_id, name=library.name, kind=library.kind.value, asset_count=library.asset_count, total_size=library.total_size, created_at=library.created_at, updated_at=library.updated_at, ) self.session.add(model) self.session.commit() return library