From 0e070d73a2a7a6f0f133e67b2c963c740b7fa5bf Mon Sep 17 00:00:00 2001 From: Crondung <1922635@stud.hs-mannheim.de> Date: Thu, 2 Mar 2023 19:41:13 +0100 Subject: [PATCH] sending data of records to api --- lib/pages/scanner_page.dart | 13 +++------- lib/services/export_service.dart | 43 ++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 lib/services/export_service.dart diff --git a/lib/pages/scanner_page.dart b/lib/pages/scanner_page.dart index de394f9..d809ff9 100644 --- a/lib/pages/scanner_page.dart +++ b/lib/pages/scanner_page.dart @@ -1,25 +1,18 @@ import 'package:awesome_dialog/awesome_dialog.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; -import 'package:smoke_cess_app/models/mood.dart'; -import 'package:smoke_cess_app/models/relapse.dart'; +import 'package:smoke_cess_app/services/export_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/widgets/scanner.dart'; -import '../models/sleep.dart'; import '../providers/settings_provider.dart'; -import '../globals.dart' as globals; class ScannerPage extends StatelessWidget { const ScannerPage({super.key}); void export() async { - List moods = await globals.databaseService.getMoodRecords(); - List sleeps = await globals.databaseService.getSleepRecords(); - List relapses = await globals.databaseService.getRelapseRecords(); - moods; - sleeps; - relapses; + ExportService exportService = ExportService(); + exportService.exportData(); } void loadJSON(BuildContext context) async { diff --git a/lib/services/export_service.dart b/lib/services/export_service.dart new file mode 100644 index 0000000..12afa12 --- /dev/null +++ b/lib/services/export_service.dart @@ -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>> _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() + }; + } + + void exportData() async { + Map> body = await _loadRecords(); + final response = await http.post(url, + headers: { + 'Content-Type': 'application/json; charset=UTF-8', + }, + body: jsonEncode(body)); + print(response.statusCode); + } +}