add missing tables, functionality for sleep save and read
parent
c6075848cf
commit
be600f98c8
|
@ -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<Database> _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<List<Mood>> getMoodRecords() async {
|
||||
Database db = await instance.database;
|
||||
var moodRecords = await db.query('mood');
|
||||
List<Mood> moodList = moodRecords.isNotEmpty
|
||||
? moodRecords.map((e) => Mood(1, 'comment', DateTime.now())).toList()
|
||||
? moodRecords.map((e) => Mood.fromDatabase(e)).toList()
|
||||
: [];
|
||||
return moodList;
|
||||
}
|
||||
|
||||
Future<List<Sleep>> getSleepRecords() async {
|
||||
Database db = await instance.database;
|
||||
var sleepRecords = await db.query('sleep');
|
||||
List<Sleep> sleepList = sleepRecords.isNotEmpty
|
||||
? sleepRecords.map((e) => Sleep.fromDatabase(e)).toList()
|
||||
: [];
|
||||
return sleepList;
|
||||
}
|
||||
|
||||
Future<int> addMood(Mood mood) async {
|
||||
Database db = await instance.database;
|
||||
return await db.insert('mood', mood.toMap());
|
||||
}
|
||||
|
||||
Future<int> 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
|
||||
)
|
||||
''';
|
||||
|
|
Loading…
Reference in New Issue