backend
This commit is contained in:
commit
d7666f7b2c
44 changed files with 2246 additions and 0 deletions
38
backend/crud/document.py
Normal file
38
backend/crud/document.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
# backend/crud/document.py
|
||||
from sqlalchemy.orm import Session
|
||||
# Importations ABSOLUES
|
||||
from models import document as models_document
|
||||
from schemas import document as schemas_document
|
||||
from typing import Optional
|
||||
|
||||
def create_document(db: Session, document: schemas_document.DocumentCreate, filepath: str, owner_id: int):
|
||||
db_document = models_document.Document(
|
||||
filename=document.filename,
|
||||
filepath=filepath,
|
||||
owner_id=owner_id
|
||||
)
|
||||
db.add(db_document)
|
||||
db.commit()
|
||||
db.refresh(db_document)
|
||||
return db_document
|
||||
|
||||
def get_documents_by_owner(db: Session, owner_id: int):
|
||||
return db.query(models_document.Document).filter(models_document.Document.owner_id == owner_id).all()
|
||||
|
||||
# Ceci est la définition correcte et finale de get_document_by_id
|
||||
def get_document_by_id(db: Session, document_id: int, owner_id: int) -> Optional[models_document.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.
|
||||
"""
|
||||
return db.query(models_document.Document).filter(
|
||||
models_document.Document.id == document_id,
|
||||
models_document.Document.owner_id == owner_id
|
||||
).first()
|
||||
|
||||
def delete_document(db: Session, document_id: int):
|
||||
db_document = db.query(models_document.Document).filter(models_document.Document.id == document_id).first()
|
||||
if db_document:
|
||||
db.delete(db_document)
|
||||
db.commit()
|
||||
return db_document
|
Loading…
Add table
Add a link
Reference in a new issue