diff --git a/lib/api/api.dart b/lib/api/api.dart index fd9686e..d49aebf 100644 --- a/lib/api/api.dart +++ b/lib/api/api.dart @@ -123,7 +123,7 @@ class API { static Future getAllUsers(Request req) async { PostgreSQLResult res = await AccountsToPostgres.getAllUsers(); - String json = DB2API.map2Json(res); + String json = DB2API.allUsersToJson(res); return Response.ok(json); } } diff --git a/lib/database/accounts_to_postgres.dart b/lib/database/accounts_to_postgres.dart index 9126f11..5bb72d7 100644 --- a/lib/database/accounts_to_postgres.dart +++ b/lib/database/accounts_to_postgres.dart @@ -121,6 +121,7 @@ class AccountsToPostgres { static Future getAllUsers() async { PostgreSQLResult res = await connection.query("SELECT id, hash, salt from \"Account\""); + print("🟥 ADMIN: get all users"); return res; } } diff --git a/lib/db_to_api.dart b/lib/db_to_api.dart index ef53cf4..090aee8 100644 --- a/lib/db_to_api.dart +++ b/lib/db_to_api.dart @@ -1,8 +1,18 @@ -import 'dart:convert'; import 'package:postgres/postgres.dart'; class DB2API { - static String map2Json(PostgreSQLResult data) { - return jsonEncode(data); + static String allUsersToJson(PostgreSQLResult data) { + String body = ""; + int count = 0; + for (final row in data) { + body += userToString(row[0], row[1], row[2]); + count++; + if (count != data.length) body += ","; + } + return "[$body]"; + } + + static String userToString(String email, String hash, String salt) { + return """{"email" : "$email", "hash" : "$hash", "salt" : "$salt"}"""; } } diff --git a/test/db_remrem_test.dart b/test/db_remrem_test.dart index 55622ef..def164f 100644 --- a/test/db_remrem_test.dart +++ b/test/db_remrem_test.dart @@ -5,7 +5,7 @@ import 'package:passworld_api/db_to_api.dart'; void main() async { await AccountsToPostgres.createAccountTable(); - await AccountsToPostgres.create("remremc@gmail.com", "hehehe", "tameare"); + //await AccountsToPostgres.create("remremj@gmail.com", "hehehe", "tameare"); var res = await AccountsToPostgres.getAllUsers(); for (final row in res) { stdout.write(row[0]); @@ -13,7 +13,7 @@ void main() async { print(row[2]); } //print(res.runtimeType); - String json = DB2API.map2Json(res); + String json = DB2API.allUsersToJson(res); print(json); AccountsToPostgres.closeConnection(); return;