50 lines
1.7 KiB
Dart
50 lines
1.7 KiB
Dart
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()
|
|
};
|
|
}
|
|
|
|
Future<int> exportData() async {
|
|
Map<String, List<String>> body = await _loadRecords();
|
|
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;
|
|
}
|
|
}
|
|
}
|