Add controllers for models

- Add REST API controllers for all models
- Controllers use Microdot subroutes
- Support CRUD operations via HTTP endpoints
- Controllers: preset, profile, group, sequence, tab, palette
This commit is contained in:
2026-01-11 21:34:15 +13:00
parent 01f373f0bd
commit ac750a36e7
6 changed files with 303 additions and 0 deletions

52
src/controllers/tab.py Normal file
View File

@@ -0,0 +1,52 @@
from microdot import Microdot
from models.tab import Tab
import json
controller = Microdot()
tabs = Tab()
@controller.get('')
async def list_tabs(request):
"""List all tabs."""
return json.dumps(tabs), 200, {'Content-Type': 'application/json'}
@controller.get('/<id>')
async def get_tab(request, id):
"""Get a specific tab by ID."""
tab = tabs.read(id)
if tab:
return json.dumps(tab), 200, {'Content-Type': 'application/json'}
return json.dumps({"error": "Tab not found"}), 404
@controller.post('')
async def create_tab(request):
"""Create a new tab."""
try:
data = request.json or {}
name = data.get("name", "")
names = data.get("names", None)
preset_ids = data.get("presets", None)
tab_id = tabs.create(name, names, preset_ids)
if data:
tabs.update(tab_id, data)
return json.dumps(tabs.read(tab_id)), 201, {'Content-Type': 'application/json'}
except Exception as e:
return json.dumps({"error": str(e)}), 400
@controller.put('/<id>')
async def update_tab(request, id):
"""Update an existing tab."""
try:
data = request.json
if tabs.update(id, data):
return json.dumps(tabs.read(id)), 200, {'Content-Type': 'application/json'}
return json.dumps({"error": "Tab not found"}), 404
except Exception as e:
return json.dumps({"error": str(e)}), 400
@controller.delete('/<id>')
async def delete_tab(request, id):
"""Delete a tab."""
if tabs.delete(id):
return json.dumps({"message": "Tab deleted successfully"}), 200
return json.dumps({"error": "Tab not found"}), 404