fix(api): stabilize palette and preset endpoints

Made-with: Cursor
This commit is contained in:
2026-03-21 23:15:08 +13:00
parent 98bbdcbb3d
commit 3ee7b74152
2 changed files with 34 additions and 24 deletions

View File

@@ -17,9 +17,9 @@ async def list_palettes(request):
@controller.get('/<id>')
async def get_palette(request, id):
"""Get a specific palette by ID."""
palette = palettes.read(id)
if palette:
return json.dumps({"colors": palette, "id": str(id)}), 200, {'Content-Type': 'application/json'}
if str(id) in palettes:
palette = palettes.read(id)
return json.dumps({"colors": palette or [], "id": str(id)}), 200, {'Content-Type': 'application/json'}
return json.dumps({"error": "Palette not found"}), 404
@controller.post('')
@@ -30,11 +30,8 @@ async def create_palette(request):
colors = data.get("colors", None)
# Palette no longer needs a name; only colors are stored.
palette_id = palettes.create("", colors)
palette = palettes.read(palette_id) or {}
# Include the ID in the response payload so clients can link it.
palette_with_id = {"id": str(palette_id)}
palette_with_id.update(palette)
return json.dumps(palette_with_id), 201, {'Content-Type': 'application/json'}
created_colors = palettes.read(palette_id) or []
return json.dumps({"id": str(palette_id), "colors": created_colors}), 201, {'Content-Type': 'application/json'}
except Exception as e:
return json.dumps({"error": str(e)}), 400
@@ -47,10 +44,8 @@ async def update_palette(request, id):
if "name" in data:
data.pop("name", None)
if palettes.update(id, data):
palette = palettes.read(id) or {}
palette_with_id = {"id": str(id)}
palette_with_id.update(palette)
return json.dumps(palette_with_id), 200, {'Content-Type': 'application/json'}
colors = palettes.read(id) or []
return json.dumps({"id": str(id), "colors": colors}), 200, {'Content-Type': 'application/json'}
return json.dumps({"error": "Palette not found"}), 404
except Exception as e:
return json.dumps({"error": str(e)}), 400