Refactor database layer: convert to DatabaseProvider class with initialization
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
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}));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user