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>> _loadRecords() async { List moodRecords = await _databaseService.getMoodRecords(); List sleepRecords = await _databaseService.getSleepRecords(); List relapseRecords = await _databaseService.getRelapseRecords(); List 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 exportData() async { Map> body = await _loadRecords(); try { final response = await http.post(url, headers: { '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; } } }