Add relapse model and database

main
Kai Mannweiler 2023-02-26 23:24:23 +01:00
parent 72674a3552
commit a054906970
2 changed files with 42 additions and 0 deletions

View File

@ -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<String, dynamic> map) {
DateTime date = DateTime.parse(map['date']);
return Relapse(map['comment'], date);
}
@override
String toCSV() {
return "${_date.toIso8601String()}, {_wokeUpAt.minute}, $_comment";
}
@override
Map<String, dynamic> toMap() {
return {
'comment': _comment,
'date': _date.toIso8601String(),
};
}
}

View File

@ -2,6 +2,7 @@ import 'dart:async';
import 'package:path/path.dart'; import 'package:path/path.dart';
import 'package:smoke_cess_app/models/mood.dart'; import 'package:smoke_cess_app/models/mood.dart';
import 'package:smoke_cess_app/models/relapse.dart';
import 'package:sqflite/sqflite.dart'; import 'package:sqflite/sqflite.dart';
// ignore: depend_on_referenced_packages // ignore: depend_on_referenced_packages
import 'package:path_provider/path_provider.dart'; import 'package:path_provider/path_provider.dart';
@ -53,6 +54,15 @@ class DatabaseService {
return sleepList; return sleepList;
} }
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;
}
Future<int> addMood(Mood mood) async { Future<int> addMood(Mood mood) async {
Database db = await instance.database; Database db = await instance.database;
return await db.insert('mood', mood.toMap()); return await db.insert('mood', mood.toMap());
@ -62,6 +72,11 @@ class DatabaseService {
Database db = await instance.database; Database db = await instance.database;
return await db.insert('sleep', sleep.toMap()); return await db.insert('sleep', sleep.toMap());
} }
Future<int> addRelapse(Relapse relapse) async {
Database db = await instance.database;
return await db.insert('relapse', relapse.toMap());
}
} }
String _createMoodTable = ''' String _createMoodTable = '''