From faaa5d3f8ae23b52580591d44bb931740114abb5 Mon Sep 17 00:00:00 2001 From: "dmit.b" Date: Fri, 8 May 2026 13:45:00 +0300 Subject: [PATCH] Change /share to create geoposition, /geo?id to update by id --- bin/database/database_provider.dart | 7 +++++-- bin/routes/geo_routes.dart | 15 +++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/bin/database/database_provider.dart b/bin/database/database_provider.dart index 86bda3c..40f45cb 100644 --- a/bin/database/database_provider.dart +++ b/bin/database/database_provider.dart @@ -231,6 +231,7 @@ class DatabaseProvider { } Future updatePosition( + int id, double x, double y, ) async { @@ -238,11 +239,13 @@ class DatabaseProvider { final results = await _dbConnection.execute( Sql.named(''' - INSERT INTO geopositions (x_value, y_value, last_update, expires_at) - VALUES (@xValue, @yValue, NOW(), @expiresAt) + UPDATE geopositions + SET x_value = @xValue, y_value = @yValue, last_update = NOW(), expires_at = @expiresAt + WHERE id = @id RETURNING id, x_value, y_value, last_update, expires_at '''), parameters: { + 'id': id, 'xValue': x, 'yValue': y, 'expiresAt': expiresAt.toIso8601String(), diff --git a/bin/routes/geo_routes.dart b/bin/routes/geo_routes.dart index ddd8337..cb12b91 100644 --- a/bin/routes/geo_routes.dart +++ b/bin/routes/geo_routes.dart @@ -29,19 +29,30 @@ class GeoRoutes { } Future _updatePosition(Request request) async { + final id = int.parse(request.url.queryParameters['id']!); final body = await request.readAsString(); final data = jsonDecode(body); final x = data['x']; final y = data['y']; - final position = await database.updatePosition(x, y); + final position = await database.updatePosition(id, x, y); return Response(200, body: position.toJson()); } Future _createShare(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); final shareId = database.createShareId(); - return Response(200, body: jsonEncode({'share_id': shareId})); + return Response(201, body: jsonEncode({ + 'geo_id': position.id, + 'share_id': shareId + })); } } \ No newline at end of file