From 8b77d823d1df0a95cf30b3b42da15d3677a0fcaf Mon Sep 17 00:00:00 2001 From: arkohut <39525455+arkohut@users.noreply.github.com> Date: Fri, 7 Jun 2024 19:03:55 +0800 Subject: [PATCH] feat(plugin): 400 when add duplicated plugin name --- memos/crud.py | 8 ++++++++ memos/server.py | 6 ++++++ memos/test_server.py | 6 ++++++ 3 files changed, 20 insertions(+) diff --git a/memos/crud.py b/memos/crud.py index 7080d37..ce1db21 100644 --- a/memos/crud.py +++ b/memos/crud.py @@ -122,6 +122,14 @@ def create_plugin(newPlugin: NewPluginParam, db: Session) -> Plugin: return db_plugin +def get_plugin_by_name(plugin_name: str, db: Session) -> Plugin | None: + return ( + db.query(PluginModel) + .filter(func.lower(PluginModel.name) == plugin_name.lower()) + .first() + ) + + def add_plugin_to_library(library_id: int, plugin_id: int, db: Session): library_plugin = LibraryPluginModel(library_id=library_id, plugin_id=plugin_id) db.add(library_plugin) diff --git a/memos/server.py b/memos/server.py index 379be41..a7d602e 100644 --- a/memos/server.py +++ b/memos/server.py @@ -190,6 +190,12 @@ def remove_entity(library_id: int, entity_id: int, db: Session = Depends(get_db) @app.post("/plugins", response_model=Plugin) def new_plugin(new_plugin: NewPluginParam, db: Session = Depends(get_db)): + existing_plugin = crud.get_plugin_by_name(new_plugin.name, db) + if existing_plugin: + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail="Plugin with this name already exists", + ) plugin = crud.create_plugin(new_plugin, db) return plugin diff --git a/memos/test_server.py b/memos/test_server.py index 46e2816..9625f9f 100644 --- a/memos/test_server.py +++ b/memos/test_server.py @@ -423,4 +423,10 @@ def test_new_plugin(client): assert duplicate_response.status_code == 400 assert duplicate_response.json() == {"detail": "Plugin with this name already exists"} + # Test for another duplicate plugin name + another_duplicate_response = client.post("/plugins", json=new_plugin.model_dump(mode="json")) + # Check that the response indicates a failure due to duplicate name + assert another_duplicate_response.status_code == 400 + assert another_duplicate_response.json() == {"detail": "Plugin with this name already exists"} +