47 lines
1.3 KiB
Dart
47 lines
1.3 KiB
Dart
import 'package:shelf/shelf.dart';
|
|
import 'package:shelf_router/shelf_router.dart';
|
|
import '../database/database_provider.dart';
|
|
import '../middleware/auth_middleware.dart';
|
|
import 'dart:convert';
|
|
|
|
class GeoRoutes {
|
|
final DatabaseProvider database;
|
|
|
|
GeoRoutes(this.database);
|
|
|
|
Router get routes {
|
|
final router = Router();
|
|
router.post('/geo', AuthMiddleware(_createPosition).call);
|
|
router.put('/geo', AuthMiddleware(_updatePosition).call);
|
|
router.post('/share', AuthMiddleware(_createShare).call);
|
|
return router;
|
|
}
|
|
|
|
Future<Response> _createPosition(Request request) async {
|
|
final body = await request.readAsString();
|
|
final data = jsonDecode(body);
|
|
|
|
final x = data['x'];
|
|
final y = data['y'];
|
|
|
|
final position = await database.createPosition(x, y);
|
|
return Response(201, body: position.toJson());
|
|
}
|
|
|
|
Future<Response> _updatePosition(Request request) async {
|
|
final body = await request.readAsString();
|
|
final data = jsonDecode(body);
|
|
|
|
final x = data['x'];
|
|
final y = data['y'];
|
|
|
|
final position = await database.updatePosition(x, y);
|
|
return Response(200, body: position.toJson());
|
|
}
|
|
|
|
Future<Response> _createShare(Request request) async {
|
|
final shareId = database.createShareId();
|
|
|
|
return Response(200, body: jsonEncode({'share_id': shareId}));
|
|
}
|
|
} |