import 'package:sqflite/sqflite.dart'; import 'package:cpd/database/db.dart'; import 'package:cpd/database/habit.dart'; class TodoDB { final tableName = 'todos'; Future createTable(Database database) async { await database.execute("""CREATE TABLE IF NOT EXISTS $tableName ( id INTEGER PRIMARY KEY AUTOINCREMENT, isComplete INTEGER NOT NULL, title TEXT NOT NULL, subtitle TEXT, icon TEXT )"""); } Future create({required String title, required String subtitle}) async { final database = await HabitDatabase().database; return await database?.rawInsert( '''insert into $tableName (id, isComplete, title, subtitle, icon) values (?,?,?,?,?)''', [title, subtitle, DateTime.now().millisecondsSinceEpoch],); //[title, 0, '', ''], // Dummy-Werte für die restlichen Spalten, falls sie nicht verwendet werden } Future?> fetchAll() async { final database = await HabitDatabase().database; final todos = await database?.rawQuery( '''select * from $tableName)'''); return todos?.map((todo) => Habit.fromSqfliteDatabase(todo)).toList(); } Future fetchById(int id) async { final database = await HabitDatabase().database; final todo = await database?.rawQuery( '''select * from $tableName where id = ?''', [id]); return Habit.fromSqfliteDatabase(todo!.first); } Future update({required int id, String? title}) async { final database = await HabitDatabase().database; return await database?.update(tableName,{ if (title != null) 'title': title, 'updated_at': DateTime.now().millisecondsSinceEpoch, }, where: 'id = ?', conflictAlgorithm: ConflictAlgorithm.rollback, whereArgs: [id], ); } Future delete(int id) async { final database = await HabitDatabase().database; await database?.rawDelete('''delete from $tableName where id = ? ''', [id]); } }