From a0549069704a49fb7b841ce37cac926ae1f40fa8 Mon Sep 17 00:00:00 2001 From: "k.mannweiler" <2012491@stud.hs-mannheim.de> Date: Sun, 26 Feb 2023 23:24:23 +0100 Subject: [PATCH] Add relapse model and database --- lib/models/relapse.dart | 27 +++++++++++++++++++++++++++ lib/services/database_service.dart | 15 +++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 lib/models/relapse.dart diff --git a/lib/models/relapse.dart b/lib/models/relapse.dart new file mode 100644 index 0000000..78eeb12 --- /dev/null +++ b/lib/models/relapse.dart @@ -0,0 +1,27 @@ +import 'package:smoke_cess_app/interface/db_record.dart'; + +class Relapse implements DatabaseRecord { + final String _comment; + final DateTime _date; + + Relapse(this._comment, this._date); + + @override + factory Relapse.fromDatabase(Map map) { + DateTime date = DateTime.parse(map['date']); + return Relapse(map['comment'], date); + } + + @override + String toCSV() { + return "${_date.toIso8601String()}, {_wokeUpAt.minute}, $_comment"; + } + + @override + Map toMap() { + return { + 'comment': _comment, + 'date': _date.toIso8601String(), + }; + } +} diff --git a/lib/services/database_service.dart b/lib/services/database_service.dart index 8dc8788..030a5af 100644 --- a/lib/services/database_service.dart +++ b/lib/services/database_service.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'package:path/path.dart'; import 'package:smoke_cess_app/models/mood.dart'; +import 'package:smoke_cess_app/models/relapse.dart'; import 'package:sqflite/sqflite.dart'; // ignore: depend_on_referenced_packages import 'package:path_provider/path_provider.dart'; @@ -53,6 +54,15 @@ class DatabaseService { return sleepList; } + Future> getRelapseRecords() async { + Database db = await instance.database; + var relapseRecords = await db.query('relapse'); + List relapseList = relapseRecords.isNotEmpty + ? relapseRecords.map((e) => Relapse.fromDatabase(e)).toList() + : []; + return relapseList; + } + Future addMood(Mood mood) async { Database db = await instance.database; return await db.insert('mood', mood.toMap()); @@ -62,6 +72,11 @@ class DatabaseService { Database db = await instance.database; return await db.insert('sleep', sleep.toMap()); } + + Future addRelapse(Relapse relapse) async { + Database db = await instance.database; + return await db.insert('relapse', relapse.toMap()); + } } String _createMoodTable = '''