import 'dart:convert'; import 'package:http/http.dart' as http; import '../config/env_directus.dart'; import '../helper/password_helper.dart'; class DirectusService { static final String endpoint = EnvDirectus.directusPublicEndpoint; static final String apiTokenBearer = EnvDirectus.directusApiTokenBearer; static final String tankUsersRest = EnvDirectus.directusTankUsersRest; static final String filamentRest = EnvDirectus.directusFilamentRest; static Map get headers => { 'Authorization': 'Bearer $apiTokenBearer', 'Content-Type': 'application/json', }; Future> getTankUserList() async { var client = http.Client(); String apiUrl = '$endpoint/items/$tankUsersRest'; var response = await client.get(Uri.parse(apiUrl), headers: headers); if(response.statusCode != 200) { print('Fehler beim Abrufen der TankUser-Liste: ${response.statusCode}'); client.close(); return []; } final json = jsonDecode( utf8.decode(response.bodyBytes), ) as Map; //print('$json}'); client.close(); return json['data'] as List; } Future> getListByUserId(String identId) async { var client = http.Client(); //String apiUrl = '$endpoint/items/$filamentRest?filter[UserIdentId][_eq]=$userIdentId'; String apiUrl = '$endpoint/items/$filamentRest?filter[UserIdentId][_eq]=$identId'; var response = await client.get(Uri.parse(apiUrl), headers: headers); if(response.statusCode != 200) { print('Fehler beim Abrufen der Dokumente aus der Collection: ${response.statusCode}'); client.close(); return []; } final json = jsonDecode( utf8.decode(response.bodyBytes), ) as Map; //print('$json}'); client.close(); return json['data'] as List; } Future deleteDocumentFromTable(int iD) async { var client = http.Client(); String apiUrl = '$endpoint/items/$filamentRest/$iD'; var response = await client.delete(Uri.parse(apiUrl), headers: headers); if(response.statusCode < 200 || response.statusCode >= 300) { print('Fehler beim Löschen des Dokuments aus der Collection: ${response.statusCode}'); client.close(); throw Exception('Fehler beim Löschen des Dokuments aus der Collection: ${response.statusCode}'); } client.close(); } Future createDocumentInTable(Map data) async { return await http.post( Uri.parse('$endpoint/items/$filamentRest'), headers: headers, body: jsonEncode(data), ).then((response) { if (response.statusCode == 200 || response.statusCode == 201) { print('Dokument erfolgreich erstellt: ${response.body}'); return true; } else { print('Fehler beim Erstellen des Dokuments: ${response.statusCode} - ${response.body}'); return false; } }).catchError((error) { print('Fehler beim Erstellen des Dokuments: $error'); return false; }); } Future updateDocumentInTable(int? iD, Map data) async { return iD != null ? http.patch( Uri.parse('$endpoint/items/$filamentRest/$iD'), headers: headers, body: jsonEncode(data), ).then((response) { if (response.statusCode == 200) { print('Dokument erfolgreich aktualisiert: ${response.body}'); return true; } else { print('Fehler beim Aktualisieren des Dokuments: ${response.statusCode} - ${response.body}'); return false; } }).catchError((error) { print('Fehler beim Aktualisieren des Dokuments: $error'); return false; }) : Future.value(false); } Future register(String userName, String email, String password, String city, String country) async { var client = http.Client(); String apiUrl = '$endpoint/items/$tankUsersRest'; Map data = { 'UserIdentId': base64Encode(utf8.encode('$email:$password')), 'UserName': userName, 'City': city, 'Country': country, 'Email': email, 'PasswordHash': await PasswordHelper.hashPassword(password), }; var response = await client.post( Uri.parse(apiUrl), headers: headers, body: jsonEncode(data), ); if (response.statusCode == 200 || response.statusCode == 201) { print('Benutzer erfolgreich registriert: ${response.body}'); client.close(); return true; } else { print('Fehler bei der Registrierung des Benutzers: ${response.statusCode} - ${response.body}'); client.close(); return false; } } }