fix delete to use get by id

This commit is contained in:
phernandez
2024-12-21 23:52:08 -06:00
parent 3541c2873d
commit fcad7b074d
3 changed files with 51 additions and 14 deletions
+5 -5
View File
@@ -100,18 +100,18 @@ async def update_document(
raise HTTPException(status_code=400, detail=str(e))
@router.delete("/{path:path}", status_code=204)
@router.delete("/{id:int}", status_code=204)
async def delete_document(
path: str,
id: int,
service: DocumentServiceDep,
) -> None:
"""Delete a document."""
"""Delete a document by ID."""
try:
await service.delete_document(path)
await service.delete_document_by_id(id)
except DocumentNotFoundError:
raise HTTPException(
status_code=404,
detail=f"Document not found: {path}"
detail=f"Document not found: {id}"
)
except DocumentWriteError as e:
raise HTTPException(status_code=400, detail=str(e))
@@ -266,6 +266,35 @@ class DocumentService(BaseService[DocumentRepository]):
assert updated_document is not None, f"Could not update document {doc.id}"
return updated_document
async def delete_document_by_id(self, id: int) -> None:
"""
Delete a document by ID.
Args:
id: Document ID
Raises:
DocumentNotFoundError: If document doesn't exist
DocumentWriteError: If deletion fails
"""
logger.debug(f"Deleting document with ID {id}")
# Get document record first
query = select(Document).where(Document.id == id)
doc = await self.repository.find_one(query)
if not doc:
raise DocumentNotFoundError(f"Document not found: {id}")
# Delete file first since it's source of truth
try:
file_path = Path(doc.path)
file_path.unlink(missing_ok=True)
except Exception as e:
raise DocumentWriteError(f"Failed to delete document {id}: {e}")
# Delete database record
await self.repository.delete(doc.id)
async def delete_document(self, path: str) -> None:
"""
Delete a document.