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