From be600f98c8ee20719e6b61fade20e6ff183e0a71 Mon Sep 17 00:00:00 2001 From: Crondung <1922635@stud.hs-mannheim.de> Date: Sat, 25 Feb 2023 18:55:49 +0100 Subject: [PATCH] add missing tables, functionality for sleep save and read --- lib/service/database_service.dart | 85 ++++++++++++++++++++++++++----- 1 file changed, 71 insertions(+), 14 deletions(-) diff --git a/lib/service/database_service.dart b/lib/service/database_service.dart index 9fe8dba..26973f4 100644 --- a/lib/service/database_service.dart +++ b/lib/service/database_service.dart @@ -7,6 +7,8 @@ import 'package:sqflite/sqflite.dart'; import 'package:path_provider/path_provider.dart'; import 'dart:io'; +import '../models/sleep.dart'; + class DatabaseService { DatabaseService._privateConstructor(); static final DatabaseService instance = DatabaseService._privateConstructor(); @@ -17,35 +19,90 @@ class DatabaseService { Future _initDatabase() async { Directory documentsDirectory = await getApplicationDocumentsDirectory(); String path = join(documentsDirectory.path, 'database.db'); - return await openDatabase( - path, - version: 1, - onCreate: _onCreate, - ); + return await openDatabase(path, + version: 1, onCreate: _onCreate, onOpen: _createTablesIfNotExists); } Future _onCreate(Database db, int version) async { - await db.execute(''' - CREATE TABLE mood( - id INTEGER PRIMARY KEY, - value INTEGER, - date TEXT, - comment TEXT - ) - '''); + await _createTablesIfNotExists(db); } + Future _createTablesIfNotExists(Database db) async { + await db.execute(_createMoodTable); + await db.execute(_createSleepTable); + await db.execute(_createRelapseTable); + await db.execute(_createWorkoutTable); + } + + //TODO use generic function? Future> getMoodRecords() async { Database db = await instance.database; var moodRecords = await db.query('mood'); List moodList = moodRecords.isNotEmpty - ? moodRecords.map((e) => Mood(1, 'comment', DateTime.now())).toList() + ? moodRecords.map((e) => Mood.fromDatabase(e)).toList() : []; return moodList; } + Future> getSleepRecords() async { + Database db = await instance.database; + var sleepRecords = await db.query('sleep'); + List sleepList = sleepRecords.isNotEmpty + ? sleepRecords.map((e) => Sleep.fromDatabase(e)).toList() + : []; + return sleepList; + } + Future addMood(Mood mood) async { Database db = await instance.database; return await db.insert('mood', mood.toMap()); } + + Future addSleep(Sleep sleep) async { + Database db = await instance.database; + return await db.insert('sleep', sleep.toMap()); + } } + +String _createMoodTable = ''' + CREATE TABLE IF NOT EXISTS mood( + id INTEGER PRIMARY KEY, + value INTEGER, + date TEXT, + comment TEXT + ) + '''; + +String _createSleepTable = ''' + CREATE TABLE IF NOT EXISTS sleep( + id INTEGER PRIMARY KEY, + value INTEGER, + date TEXT, + comment TEXT, + sleepedAtHour INTEGER, + sleepedAtMinute INTEGER, + wokeUpAtHour INTEGER, + wokeUpAtMinute INTEGER + ) + '''; + +String _createRelapseTable = ''' + CREATE TABLE IF NOT EXISTS relapse( + id INTEGER PRIMARY KEY, + date TEXT, + comment TEXT, + reason TEXT + ) + '''; + +String _createWorkoutTable = ''' + CREATE TABLE IF NOT EXISTS workout( + id INTEGER PRIMARY KEY, + date TEXT, + motivationBefore INTEGER, + commentBefore TEXT, + motivationAfter INTEGER, + commentAfter TEXT, + completed INTEGER + ) + ''';