mirror of
https://github.com/tcsenpai/pensieve.git
synced 2025-06-06 03:05:25 +00:00
feat(plugin): 400 when add duplicated plugin name
This commit is contained in:
parent
22fc6a4e27
commit
8b77d823d1
@ -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)
|
||||
|
@ -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
|
||||
|
||||
|
@ -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"}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user