From c8a140678d3a1e667f2502309295205ee59d1fab Mon Sep 17 00:00:00 2001 From: Crondung <1922635@stud.hs-mannheim.de> Date: Thu, 2 Mar 2023 19:04:40 +0100 Subject: [PATCH] add workout get db function --- lib/mock/db_mock.dart | 7 ++++++- lib/models/workout.dart | 10 ++++++++-- lib/services/database_service.dart | 9 +++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/mock/db_mock.dart b/lib/mock/db_mock.dart index 313cece..dac8c9f 100644 --- a/lib/mock/db_mock.dart +++ b/lib/mock/db_mock.dart @@ -17,7 +17,7 @@ class DatabaseMock implements DatabaseService { final List _moodRecords = []; final List _sleepRecords = []; final List _relapseRecords = []; - final List _workoutRecords = []; + final List _workoutRecords = []; @override Future addMood(Mood mood) { @@ -60,4 +60,9 @@ class DatabaseMock implements DatabaseService { Future> getRelapseRecords() { return Future.value(_relapseRecords); } + + @override + Future> getWorkoutRecords() { + return Future.value(_workoutRecords); + } } diff --git a/lib/models/workout.dart b/lib/models/workout.dart index 0bba86a..67dd828 100644 --- a/lib/models/workout.dart +++ b/lib/models/workout.dart @@ -7,10 +7,16 @@ class Workout implements DatabaseRecord { Workout(this._motivationBefore, this._motivationAfter, this._workoutDate); + @override + factory Workout.fromDatabase(Map map) { + return Workout(map['motivationBefore'], map['motivationAfter'], + DateTime.parse(map['workoutDate'])); + } + @override factory Workout.fromMap(Map map) { - return Workout(map['_workoutDuration'], map['_motivationBefore'], - map['_motivationAfter']); + return Workout( + map['motivationBefore'], map['motivationAfter'], map['workoutDate']); } @override diff --git a/lib/services/database_service.dart b/lib/services/database_service.dart index 3996883..acc3ca0 100644 --- a/lib/services/database_service.dart +++ b/lib/services/database_service.dart @@ -63,6 +63,15 @@ class DatabaseService { return relapseList; } + Future> getWorkoutRecords() async { + Database db = await instance.database; + var workoutRecords = await db.query('workout'); + List workoutList = workoutRecords.isNotEmpty + ? workoutRecords.map((e) => Workout.fromDatabase(e)).toList() + : []; + return workoutList; + } + Future addMood(Mood mood) async { Database db = await instance.database; return await db.insert('mood', mood.toMap());