133 lines
4.6 KiB
Dart
133 lines
4.6 KiB
Dart
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 tankStopRest = EnvDirectus.directusTankStopRest;
|
|
|
|
static Map<String, String> get headers => {
|
|
'Authorization': 'Bearer $apiTokenBearer',
|
|
'Content-Type': 'application/json',
|
|
};
|
|
|
|
Future<List<dynamic>> 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<String, dynamic>;
|
|
//print('$json}');
|
|
client.close();
|
|
return json['data'] as List<dynamic>;
|
|
}
|
|
|
|
Future<List<dynamic>> getTankGuruDataFromTable(String userIdentId) async {
|
|
var client = http.Client();
|
|
String apiUrl = '$endpoint/items/$tankStopRest?filter[UserIdentId][_eq]=$userIdentId';
|
|
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<String, dynamic>;
|
|
//print('$json}');
|
|
client.close();
|
|
return json['data'] as List<dynamic>;
|
|
}
|
|
|
|
Future<void> deleteDocumentFromTable(int iD) async {
|
|
var client = http.Client();
|
|
String apiUrl = '$endpoint/items/$tankStopRest/$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<bool> createDocumentInTable(Map<String, String> data) async {
|
|
return await http.post(
|
|
Uri.parse('$endpoint/items/$tankStopRest'),
|
|
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<bool> updateDocumentInTable(int? iD, Map<String, String> data) async {
|
|
return iD != null
|
|
? http.patch(
|
|
Uri.parse('$endpoint/items/$tankStopRest/$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<bool> register(String userName, String email, String password, String city, String country) async {
|
|
var client = http.Client();
|
|
String apiUrl = '$endpoint/items/$tankUsersRest';
|
|
Map<String, String> 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;
|
|
}
|
|
}
|
|
|
|
}
|