From abf04fe74ecf9b2375fc7e79d8f0a8d13d971dd7 Mon Sep 17 00:00:00 2001 From: arkohut <39525455+arkohut@users.noreply.github.com> Date: Mon, 3 Jun 2024 23:10:13 +0800 Subject: [PATCH] feat(library): get library by id --- memos/commands.py | 11 +++++++++++ memos/server.py | 14 ++++++++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/memos/commands.py b/memos/commands.py index 547cd62..200f177 100644 --- a/memos/commands.py +++ b/memos/commands.py @@ -52,5 +52,16 @@ def add(name: str, folders: List[str]): print(f"Failed to create library: {response.status_code} - {response.text}") +@lib_app.command("show") +def show(library_id: int): + response = httpx.get(f"http://localhost:8080/libraries/{library_id}") + if response.status_code == 200: + library = response.json() + display_libraries([library]) + else: + print(f"Failed to retrieve library: {response.status_code} - {response.text}") + + + if __name__ == "__main__": app() diff --git a/memos/server.py b/memos/server.py index 800b574..e7440db 100644 --- a/memos/server.py +++ b/memos/server.py @@ -51,6 +51,14 @@ def list_libraries(db: Session = Depends(get_db)): return libraries +@app.get("/libraries/{library_id}", response_model=Library) +def get_library_by_id(library_id: int, db: Session = Depends(get_db)): + library = crud.get_library_by_id(library_id, db) + if library is None: + raise HTTPException(status_code=404, detail="Library not found") + return library + + @app.post("/libraries/{library_id}/folders", response_model=Folder) def new_folder( library_id: int, @@ -75,7 +83,7 @@ def new_entity( library = crud.get_library_by_id(library_id, db) if library is None: raise HTTPException(status_code=404, detail="Library not found") - + entity = crud.create_entity(library_id, new_entity, db) return entity @@ -89,7 +97,9 @@ def get_entity_by_id(library_id: int, entity_id: int, db: Session = Depends(get_ @app.get("/libraries/{library_id}/entities", response_model=Entity) -def get_entity_by_filepath(library_id: int, filepath: str, db: Session = Depends(get_db)): +def get_entity_by_filepath( + library_id: int, filepath: str, db: Session = Depends(get_db) +): entity = crud.get_entity_by_filepath(filepath, db) if entity is None or entity.library_id != library_id: raise HTTPException(status_code=404, detail="Entity not found")