mirror of
https://github.com/basicmachines-co/basic-memory
synced 2026-06-21 13:47:35 +00:00
use FilePath for documents
This commit is contained in:
@@ -5,8 +5,7 @@ from typing import List
|
||||
from fastapi import APIRouter, HTTPException
|
||||
|
||||
from basic_memory.deps import DocumentServiceDep
|
||||
from basic_memory.schemas.base import PathId
|
||||
from basic_memory.schemas.request import DocumentRequest
|
||||
from basic_memory.schemas.request import DocumentRequest, FilePath
|
||||
from basic_memory.schemas.response import DocumentResponse, DocumentCreateResponse
|
||||
from basic_memory.services.document_service import (
|
||||
DocumentNotFoundError,
|
||||
@@ -33,7 +32,7 @@ async def create_document(
|
||||
"""
|
||||
try:
|
||||
document = await service.create_document(
|
||||
path=doc.path,
|
||||
doc_path=doc.path,
|
||||
content=doc.content,
|
||||
metadata=doc.doc_metadata,
|
||||
)
|
||||
@@ -51,39 +50,39 @@ async def list_documents(
|
||||
return [DocumentCreateResponse.model_validate(doc.__dict__) for doc in documents]
|
||||
|
||||
|
||||
@router.get("/{path_id:path}", response_model=DocumentResponse)
|
||||
@router.get("/{doc_path:path}", response_model=DocumentResponse)
|
||||
async def get_document(
|
||||
path_id: PathId,
|
||||
doc_path: FilePath,
|
||||
service: DocumentServiceDep,
|
||||
) -> DocumentResponse:
|
||||
"""Get a document by ID."""
|
||||
try:
|
||||
document, content = await service.read_document_by_path(path_id)
|
||||
document, content = await service.read_document_by_path(doc_path)
|
||||
doc_dict = document.__dict__ | {"content": content}
|
||||
response = DocumentResponse.model_validate(doc_dict)
|
||||
return response
|
||||
except DocumentNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=f"Document not found: {path_id}")
|
||||
raise HTTPException(status_code=404, detail=f"Document not found: {doc_path}")
|
||||
except DocumentWriteError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
|
||||
@router.put("/{path_id:path}", response_model=DocumentResponse)
|
||||
@router.put("/{doc_path:path}", response_model=DocumentResponse)
|
||||
async def update_document(
|
||||
path_id: PathId,
|
||||
doc_path: FilePath,
|
||||
doc: DocumentRequest,
|
||||
service: DocumentServiceDep,
|
||||
) -> DocumentResponse:
|
||||
"""Update a document by ID."""
|
||||
# Verify PathIds match
|
||||
if doc.path != path_id:
|
||||
# Verify FilePaths match
|
||||
if doc.path != doc_path:
|
||||
raise HTTPException(
|
||||
status_code=400, detail="Document path in URL must match path in request body"
|
||||
)
|
||||
|
||||
try:
|
||||
document = await service.update_document_by_path(
|
||||
path_id=path_id,
|
||||
path_id=doc_path,
|
||||
content=doc.content,
|
||||
metadata=doc.doc_metadata,
|
||||
)
|
||||
@@ -95,14 +94,14 @@ async def update_document(
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
|
||||
@router.delete("/{path_id:path}", status_code=204)
|
||||
@router.delete("/{doc_path:path}", status_code=204)
|
||||
async def delete_document(
|
||||
path_id: PathId,
|
||||
doc_path: FilePath,
|
||||
service: DocumentServiceDep,
|
||||
) -> None:
|
||||
"""Delete a document by ID."""
|
||||
try:
|
||||
await service.delete_document_by_path(path_id)
|
||||
await service.delete_document_by_path(doc_path)
|
||||
except DocumentNotFoundError:
|
||||
raise HTTPException(status_code=404, detail=f"Document not found: {id}")
|
||||
except DocumentWriteError as e:
|
||||
|
||||
Reference in New Issue
Block a user