sending data of records to api
parent
c8a140678d
commit
0e070d73a2
|
@ -1,25 +1,18 @@
|
||||||
import 'package:awesome_dialog/awesome_dialog.dart';
|
import 'package:awesome_dialog/awesome_dialog.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
import 'package:smoke_cess_app/models/mood.dart';
|
import 'package:smoke_cess_app/services/export_service.dart';
|
||||||
import 'package:smoke_cess_app/models/relapse.dart';
|
|
||||||
import 'package:smoke_cess_app/services/settings_service.dart';
|
import 'package:smoke_cess_app/services/settings_service.dart';
|
||||||
import 'package:smoke_cess_app/services/notification_service.dart';
|
import 'package:smoke_cess_app/services/notification_service.dart';
|
||||||
import 'package:smoke_cess_app/widgets/scanner.dart';
|
import 'package:smoke_cess_app/widgets/scanner.dart';
|
||||||
import '../models/sleep.dart';
|
|
||||||
import '../providers/settings_provider.dart';
|
import '../providers/settings_provider.dart';
|
||||||
import '../globals.dart' as globals;
|
|
||||||
|
|
||||||
class ScannerPage extends StatelessWidget {
|
class ScannerPage extends StatelessWidget {
|
||||||
const ScannerPage({super.key});
|
const ScannerPage({super.key});
|
||||||
|
|
||||||
void export() async {
|
void export() async {
|
||||||
List<Mood> moods = await globals.databaseService.getMoodRecords();
|
ExportService exportService = ExportService();
|
||||||
List<Sleep> sleeps = await globals.databaseService.getSleepRecords();
|
exportService.exportData();
|
||||||
List<Relapse> relapses = await globals.databaseService.getRelapseRecords();
|
|
||||||
moods;
|
|
||||||
sleeps;
|
|
||||||
relapses;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loadJSON(BuildContext context) async {
|
void loadJSON(BuildContext context) async {
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
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()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
void 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));
|
||||||
|
print(response.statusCode);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue