mirror of
https://github.com/R0m1k3/xtremobile.git
synced 2026-10-11 17:29:26 +02:00
64 lines
1.7 KiB
Dart
64 lines
1.7 KiB
Dart
import 'dart:convert';
|
|
import 'package:shelf/shelf.dart';
|
|
import 'package:shelf_router/shelf_router.dart';
|
|
import '../database/database.dart';
|
|
|
|
class SettingsHandler {
|
|
final AppDatabase db;
|
|
|
|
SettingsHandler(this.db);
|
|
|
|
Router get router {
|
|
final router = Router();
|
|
|
|
// GET /api/settings
|
|
router.get('/', (Request req) {
|
|
// Get userId from context (set by auth middleware)
|
|
final userId = req.context['userId'] as String?;
|
|
if (userId == null) {
|
|
return Response.forbidden(jsonEncode({'error': 'Authentication required'}));
|
|
}
|
|
|
|
final settingsJson = db.getUserSettings(userId);
|
|
|
|
// If no settings exist, return empty object or default
|
|
return Response.ok(
|
|
settingsJson ?? '{}',
|
|
headers: {'content-type': 'application/json'},
|
|
);
|
|
});
|
|
|
|
// POST /api/settings
|
|
router.post('/', (Request req) async {
|
|
// Get userId from context (set by auth middleware)
|
|
final userId = req.context['userId'] as String?;
|
|
if (userId == null) {
|
|
return Response.forbidden(jsonEncode({'error': 'Authentication required'}));
|
|
}
|
|
|
|
try {
|
|
final body = await req.readAsString();
|
|
// Validate JSON
|
|
try {
|
|
jsonDecode(body);
|
|
} catch (e) {
|
|
return Response.badRequest(body: jsonEncode({'error': 'Invalid JSON'}));
|
|
}
|
|
|
|
db.updateUserSettings(userId, body);
|
|
|
|
return Response.ok(
|
|
body,
|
|
headers: {'content-type': 'application/json'},
|
|
);
|
|
} catch (e) {
|
|
return Response.internalServerError(
|
|
body: jsonEncode({'error': 'Failed to save settings: $e'}),
|
|
);
|
|
}
|
|
});
|
|
|
|
return router;
|
|
}
|
|
}
|