cpd_2022_zi/lib/services/export_service.dart

44 lines
1.6 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();
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;
}
}