30 lines
No EOL
1.2 KiB
Python
30 lines
No EOL
1.2 KiB
Python
# backend/repositories/document_repository.py
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy.future import select
|
|
from models.document import Document
|
|
from typing import Optional, List
|
|
|
|
class DocumentRepository:
|
|
def __init__(self, db: AsyncSession):
|
|
self.db = db
|
|
|
|
async def get_document_by_id(self, document_id: int, owner_id: int) -> Optional[Document]:
|
|
"""
|
|
Récupère un document par son ID et l'ID de son propriétaire.
|
|
Cela garantit qu'un utilisateur ne peut accéder qu'à ses propres documents.
|
|
"""
|
|
result = await self.db.execute(
|
|
select(Document).where(Document.id == document_id, Document.owner_id == owner_id)
|
|
)
|
|
return result.scalars().first()
|
|
|
|
async def get_all_documents_by_owner_id(self, owner_id: int) -> List[Document]:
|
|
"""
|
|
Récupère tous les documents pour un propriétaire donné.
|
|
"""
|
|
result = await self.db.execute(
|
|
select(Document).where(Document.owner_id == owner_id)
|
|
)
|
|
return result.scalars().all()
|
|
|
|
# Vous pourriez ajouter ici d'autres méthodes comme create_document, delete_document, etc. |