cpd_2022_zi/lib/services/export_service.dart

50 lines
1.7 KiB
Dart
Raw Normal View History

2023-03-02 19:41:13 +01:00
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:smoke_cess_app/models/mood.dart';
import 'package:smoke_cess_app/models/relapse.dart';
import 'package:smoke_cess_app/models/sleep.dart';
import 'package:smoke_cess_app/models/workout.dart';
import 'package:smoke_cess_app/services/database_service.dart';
import '../globals.dart' as globals;
class ExportService {
Uri url = Uri.parse('http://localhost:3000/data');
final DatabaseService _databaseService = globals.databaseService;
Future<Map<String, List<String>>> _loadRecords() async {
List<Mood> moodRecords = await _databaseService.getMoodRecords();
List<Sleep> sleepRecords = await _databaseService.getSleepRecords();
List<Relapse> relapseRecords = await _databaseService.getRelapseRecords();
List<Workout> workoutRecords = await _databaseService.getWorkoutRecords();
return {
'Stimmung':
moodRecords.map((Mood mood) => jsonEncode(mood.toMap())).toList(),
'Schlaf':
sleepRecords.map((Sleep sleep) => jsonEncode(sleep.toMap())).toList(),
'Rückfall': relapseRecords
.map((Relapse relapse) => jsonEncode(relapse.toMap()))
.toList(),
'Workout': workoutRecords
.map((Workout workout) => jsonEncode(workout.toMap()))
.toList()
};
}
2023-03-02 19:47:28 +01:00
Future<int> exportData() async {
2023-03-02 19:41:13 +01:00
Map<String, List<String>> body = await _loadRecords();
2023-03-06 18:39:36 +01:00
try {
final response = await http.post(url,
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(body));
return response.statusCode >= 400 ? 0 : 1;
} catch (e) {
// ignore: avoid_print
print(jsonEncode(body));
return 0;
}
2023-03-02 19:41:13 +01:00
}
}