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:
50
src/controllers/profile.py
Normal file
50
src/controllers/profile.py
Normal file
@@ -0,0 +1,50 @@
|
||||
from microdot import Microdot
|
||||
from models.profile import Profile
|
||||
import json
|
||||
|
||||
controller = Microdot()
|
||||
profiles = Profile()
|
||||
|
||||
@controller.get('')
|
||||
async def list_profiles(request):
|
||||
"""List all profiles."""
|
||||
return json.dumps(profiles), 200, {'Content-Type': 'application/json'}
|
||||
|
||||
@controller.get('/<id>')
|
||||
async def get_profile(request, id):
|
||||
"""Get a specific profile by ID."""
|
||||
profile = profiles.read(id)
|
||||
if profile:
|
||||
return json.dumps(profile), 200, {'Content-Type': 'application/json'}
|
||||
return json.dumps({"error": "Profile not found"}), 404
|
||||
|
||||
@controller.post('')
|
||||
async def create_profile(request):
|
||||
"""Create a new profile."""
|
||||
try:
|
||||
data = request.json or {}
|
||||
name = data.get("name", "")
|
||||
profile_id = profiles.create(name)
|
||||
if data:
|
||||
profiles.update(profile_id, data)
|
||||
return json.dumps(profiles.read(profile_id)), 201, {'Content-Type': 'application/json'}
|
||||
except Exception as e:
|
||||
return json.dumps({"error": str(e)}), 400
|
||||
|
||||
@controller.put('/<id>')
|
||||
async def update_profile(request, id):
|
||||
"""Update an existing profile."""
|
||||
try:
|
||||
data = request.json
|
||||
if profiles.update(id, data):
|
||||
return json.dumps(profiles.read(id)), 200, {'Content-Type': 'application/json'}
|
||||
return json.dumps({"error": "Profile not found"}), 404
|
||||
except Exception as e:
|
||||
return json.dumps({"error": str(e)}), 400
|
||||
|
||||
@controller.delete('/<id>')
|
||||
async def delete_profile(request, id):
|
||||
"""Delete a profile."""
|
||||
if profiles.delete(id):
|
||||
return json.dumps({"message": "Profile deleted successfully"}), 200
|
||||
return json.dumps({"error": "Profile not found"}), 404
|
||||
Reference in New Issue
Block a user