fix(api): stabilize palette and preset endpoints
Made-with: Cursor
This commit is contained in:
@@ -17,9 +17,9 @@ async def list_palettes(request):
|
|||||||
@controller.get('/<id>')
|
@controller.get('/<id>')
|
||||||
async def get_palette(request, id):
|
async def get_palette(request, id):
|
||||||
"""Get a specific palette by ID."""
|
"""Get a specific palette by ID."""
|
||||||
|
if str(id) in palettes:
|
||||||
palette = palettes.read(id)
|
palette = palettes.read(id)
|
||||||
if palette:
|
return json.dumps({"colors": palette or [], "id": str(id)}), 200, {'Content-Type': 'application/json'}
|
||||||
return json.dumps({"colors": palette, "id": str(id)}), 200, {'Content-Type': 'application/json'}
|
|
||||||
return json.dumps({"error": "Palette not found"}), 404
|
return json.dumps({"error": "Palette not found"}), 404
|
||||||
|
|
||||||
@controller.post('')
|
@controller.post('')
|
||||||
@@ -30,11 +30,8 @@ async def create_palette(request):
|
|||||||
colors = data.get("colors", None)
|
colors = data.get("colors", None)
|
||||||
# Palette no longer needs a name; only colors are stored.
|
# Palette no longer needs a name; only colors are stored.
|
||||||
palette_id = palettes.create("", colors)
|
palette_id = palettes.create("", colors)
|
||||||
palette = palettes.read(palette_id) or {}
|
created_colors = palettes.read(palette_id) or []
|
||||||
# Include the ID in the response payload so clients can link it.
|
return json.dumps({"id": str(palette_id), "colors": created_colors}), 201, {'Content-Type': 'application/json'}
|
||||||
palette_with_id = {"id": str(palette_id)}
|
|
||||||
palette_with_id.update(palette)
|
|
||||||
return json.dumps(palette_with_id), 201, {'Content-Type': 'application/json'}
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return json.dumps({"error": str(e)}), 400
|
return json.dumps({"error": str(e)}), 400
|
||||||
|
|
||||||
@@ -47,10 +44,8 @@ async def update_palette(request, id):
|
|||||||
if "name" in data:
|
if "name" in data:
|
||||||
data.pop("name", None)
|
data.pop("name", None)
|
||||||
if palettes.update(id, data):
|
if palettes.update(id, data):
|
||||||
palette = palettes.read(id) or {}
|
colors = palettes.read(id) or []
|
||||||
palette_with_id = {"id": str(id)}
|
return json.dumps({"id": str(id), "colors": colors}), 200, {'Content-Type': 'application/json'}
|
||||||
palette_with_id.update(palette)
|
|
||||||
return json.dumps(palette_with_id), 200, {'Content-Type': 'application/json'}
|
|
||||||
return json.dumps({"error": "Palette not found"}), 404
|
return json.dumps({"error": "Palette not found"}), 404
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return json.dumps({"error": str(e)}), 400
|
return json.dumps({"error": str(e)}), 400
|
||||||
|
|||||||
@@ -36,11 +36,11 @@ async def list_presets(request, session):
|
|||||||
}
|
}
|
||||||
return json.dumps(scoped), 200, {'Content-Type': 'application/json'}
|
return json.dumps(scoped), 200, {'Content-Type': 'application/json'}
|
||||||
|
|
||||||
@controller.get('/<id>')
|
@controller.get('/<preset_id>')
|
||||||
@with_session
|
@with_session
|
||||||
async def get_preset(request, id, session):
|
async def get_preset(request, session, preset_id):
|
||||||
"""Get a specific preset by ID (current profile only)."""
|
"""Get a specific preset by ID (current profile only)."""
|
||||||
preset = presets.read(id)
|
preset = presets.read(preset_id)
|
||||||
current_profile_id = get_current_profile_id(session)
|
current_profile_id = get_current_profile_id(session)
|
||||||
if preset and str(preset.get("profile_id")) == str(current_profile_id):
|
if preset and str(preset.get("profile_id")) == str(current_profile_id):
|
||||||
return json.dumps(preset), 200, {'Content-Type': 'application/json'}
|
return json.dumps(preset), 200, {'Content-Type': 'application/json'}
|
||||||
@@ -70,12 +70,12 @@ async def create_preset(request, session):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
return json.dumps({"error": str(e)}), 400
|
return json.dumps({"error": str(e)}), 400
|
||||||
|
|
||||||
@controller.put('/<id>')
|
@controller.put('/<preset_id>')
|
||||||
@with_session
|
@with_session
|
||||||
async def update_preset(request, id, session):
|
async def update_preset(request, session, preset_id):
|
||||||
"""Update an existing preset (current profile only)."""
|
"""Update an existing preset (current profile only)."""
|
||||||
try:
|
try:
|
||||||
preset = presets.read(id)
|
preset = presets.read(preset_id)
|
||||||
current_profile_id = get_current_profile_id(session)
|
current_profile_id = get_current_profile_id(session)
|
||||||
if not preset or str(preset.get("profile_id")) != str(current_profile_id):
|
if not preset or str(preset.get("profile_id")) != str(current_profile_id):
|
||||||
return json.dumps({"error": "Preset not found"}), 404
|
return json.dumps({"error": "Preset not found"}), 404
|
||||||
@@ -87,21 +87,36 @@ async def update_preset(request, id, session):
|
|||||||
data = {}
|
data = {}
|
||||||
data = dict(data)
|
data = dict(data)
|
||||||
data["profile_id"] = str(current_profile_id)
|
data["profile_id"] = str(current_profile_id)
|
||||||
if presets.update(id, data):
|
if presets.update(preset_id, data):
|
||||||
return json.dumps(presets.read(id)), 200, {'Content-Type': 'application/json'}
|
return json.dumps(presets.read(preset_id)), 200, {'Content-Type': 'application/json'}
|
||||||
return json.dumps({"error": "Preset not found"}), 404
|
return json.dumps({"error": "Preset not found"}), 404
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return json.dumps({"error": str(e)}), 400
|
return json.dumps({"error": str(e)}), 400
|
||||||
|
|
||||||
@controller.delete('/<id>')
|
@controller.delete('/<preset_id>')
|
||||||
@with_session
|
@with_session
|
||||||
async def delete_preset(request, id, session):
|
async def delete_preset(request, *args, **kwargs):
|
||||||
"""Delete a preset (current profile only)."""
|
"""Delete a preset (current profile only)."""
|
||||||
preset = presets.read(id)
|
# Be tolerant of wrapper/arg-order variations.
|
||||||
|
session = None
|
||||||
|
preset_id = None
|
||||||
|
if len(args) > 0:
|
||||||
|
session = args[0]
|
||||||
|
if len(args) > 1:
|
||||||
|
preset_id = args[1]
|
||||||
|
if 'session' in kwargs and kwargs.get('session') is not None:
|
||||||
|
session = kwargs.get('session')
|
||||||
|
if 'preset_id' in kwargs and kwargs.get('preset_id') is not None:
|
||||||
|
preset_id = kwargs.get('preset_id')
|
||||||
|
if 'id' in kwargs and kwargs.get('id') is not None and preset_id is None:
|
||||||
|
preset_id = kwargs.get('id')
|
||||||
|
if preset_id is None:
|
||||||
|
return json.dumps({"error": "Preset ID is required"}), 400
|
||||||
|
preset = presets.read(preset_id)
|
||||||
current_profile_id = get_current_profile_id(session)
|
current_profile_id = get_current_profile_id(session)
|
||||||
if not preset or str(preset.get("profile_id")) != str(current_profile_id):
|
if not preset or str(preset.get("profile_id")) != str(current_profile_id):
|
||||||
return json.dumps({"error": "Preset not found"}), 404
|
return json.dumps({"error": "Preset not found"}), 404
|
||||||
if presets.delete(id):
|
if presets.delete(preset_id):
|
||||||
return json.dumps({"message": "Preset deleted successfully"}), 200
|
return json.dumps({"message": "Preset deleted successfully"}), 200
|
||||||
return json.dumps({"error": "Preset not found"}), 404
|
return json.dumps({"error": "Preset not found"}), 404
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user