cpd_2022_zi/lib/services/database_service.dart

135 lines
3.9 KiB
Dart
Raw Normal View History

2023-02-26 14:10:06 +01:00
import 'dart:async';
import 'package:path/path.dart';
import 'package:smoke_cess_app/models/mood.dart';
2023-03-02 13:19:08 +01:00
import 'package:smoke_cess_app/models/workout.dart';
2023-02-26 23:24:23 +01:00
import 'package:smoke_cess_app/models/relapse.dart';
2023-02-26 14:10:06 +01:00
import 'package:sqflite/sqflite.dart';
// ignore: depend_on_referenced_packages
import 'package:path_provider/path_provider.dart';
import 'dart:io';
import '../models/sleep.dart';
class DatabaseService {
DatabaseService._privateConstructor();
static final DatabaseService instance = DatabaseService._privateConstructor();
static Database? _database;
Future<Database> get database async => _database ??= await _initDatabase();
Future<Database> _initDatabase() async {
Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, 'database.db');
return await openDatabase(path,
version: 1, onCreate: _onCreate, onOpen: _createTablesIfNotExists);
}
Future _onCreate(Database db, int version) async {
await _createTablesIfNotExists(db);
}
Future _createTablesIfNotExists(Database db) async {
await db.execute(_createMoodTable);
await db.execute(_createSleepTable);
await db.execute(_createRelapseTable);
await db.execute(_createWorkoutTable);
}
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.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;
}
2023-02-26 23:24:23 +01:00
Future<List<Relapse>> getRelapseRecords() async {
Database db = await instance.database;
var relapseRecords = await db.query('relapse');
List<Relapse> relapseList = relapseRecords.isNotEmpty
? relapseRecords.map((e) => Relapse.fromDatabase(e)).toList()
: [];
return relapseList;
}
2023-03-02 18:35:03 +01:00
Future<List<Workout>> getWorkoutRecords() async {
Database db = await instance.database;
var workoutRecords = await db.query('workout');
List<Workout> workoutList = workoutRecords.isNotEmpty
? workoutRecords.map((e) => Workout.fromDatabase(e)).toList()
: [];
return workoutList;
}
2023-02-26 14:10:06 +01:00
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());
}
2023-03-02 13:19:08 +01:00
Future<int> addWorkout(Workout workout) async {
Database db = await instance.database;
return await db.insert('workout', workout.toMap());
}
2023-02-26 14:10:06 +01:00
2023-02-26 23:24:23 +01:00
Future<int> addRelapse(Relapse relapse) async {
Database db = await instance.database;
return await db.insert('relapse', relapse.toMap());
}
2023-02-26 14:10:06 +01:00
2023-03-02 16:12:54 +01:00
final String _createMoodTable = '''
2023-02-26 14:10:06 +01:00
CREATE TABLE IF NOT EXISTS mood(
id INTEGER PRIMARY KEY,
value INTEGER,
date TEXT,
comment TEXT
)
''';
2023-03-02 16:12:54 +01:00
final String _createSleepTable = '''
2023-02-26 14:10:06 +01:00
CREATE TABLE IF NOT EXISTS sleep(
id INTEGER PRIMARY KEY,
value INTEGER,
date TEXT,
comment TEXT,
2023-02-26 17:07:38 +01:00
sleptAtHour INTEGER,
sleptAtMinute INTEGER,
2023-02-26 14:10:06 +01:00
wokeUpAtHour INTEGER,
wokeUpAtMinute INTEGER
)
''';
2023-03-02 16:12:54 +01:00
final String _createRelapseTable = '''
2023-02-26 14:10:06 +01:00
CREATE TABLE IF NOT EXISTS relapse(
id INTEGER PRIMARY KEY,
date TEXT,
comment TEXT,
reason TEXT
)
''';
2023-03-02 16:12:54 +01:00
final String _createWorkoutTable = '''
2023-02-26 14:10:06 +01:00
CREATE TABLE IF NOT EXISTS workout(
id INTEGER PRIMARY KEY,
date TEXT,
motivationBefore INTEGER,
motivationAfter INTEGER,
)
''';
}