import 'package:cpd/database/todo_interface.dart'; import 'package:flutter/material.dart'; import 'package:sqflite/sqflite.dart'; import 'package:cpd/database/db.dart'; import 'package:cpd/database/habit.dart'; import 'package:logging/logging.dart'; final Logger _logger = Logger('TodoDB'); class TodoDB implements ToDoInterface { final tableName = 'todos'; @override Future createTable(Database database) async { try { await database.execute("""CREATE TABLE IF NOT EXISTS $tableName ( id INTEGER PRIMARY KEY AUTOINCREMENT, isComplete INTEGER NOT NULL, title TEXT NOT NULL, subtitle TEXT, iconCodePoint INTEGER NOT NULL, iconFontFamily TEXT NOT NULL )"""); } on DatabaseException catch (e, s) { _logger.severe('Failed to create table $tableName', e, s); return Future.error(e); } catch (e, s) { _logger.severe('Unexpected error creating table $tableName', e, s); return Future.error(e); } } @override Future insert({ required String title, required String subtitle, required IconData icon}) async { try { final database = await HabitDatabase().database; return await database.rawInsert( '''INSERT INTO $tableName (isComplete, title, subtitle, iconCodePoint, iconFontFamily) VALUES (?, ?, ?, ?, ?)''', [0, title, subtitle, icon.codePoint, icon.fontFamily], ); } on DatabaseException catch (e, s) { _logger.severe('Failed to insert into $tableName', e, s); return Future.error(e); } catch (e, s) { _logger.severe('Unexpected error inserting into $tableName', e, s); return Future.error(e); } } @override Future> fetchAll() async { try { final database = await HabitDatabase().database; final todos = await database.query(tableName); return todos.map((todo) => Habit.fromSqfliteDatabase(todo)).toList(); } on DatabaseException catch (e, s) { _logger.severe('Failed to fetch all from $tableName', e, s); return Future.error(e); } catch (e, s) { _logger.severe('Unexpected error fetching all from $tableName', e, s); return Future.error(e); } } @override Future fetchById(int id) async { try { final database = await HabitDatabase().database; final todo = await database.query(tableName, where: 'id = ?', whereArgs: [id]); if (todo.isNotEmpty) { return Habit.fromSqfliteDatabase(todo.first); } else { return null; } } on DatabaseException catch (e, s) { _logger.severe('Failed to fetch by ID from $tableName', e, s); return Future.error(e); } catch (e, s) { _logger.severe('Unexpected error fetching by ID from $tableName', e, s); return Future.error(e); } } @override Future update({ required int id, required String title, required String subtitle, required IconData icon}) async { try { final database = await HabitDatabase().database; return await database.update(tableName, { 'title': title, 'subtitle': subtitle, 'iconCodePoint': icon.codePoint, 'iconFontFamily': icon.fontFamily, }, where: 'id = ?', conflictAlgorithm: ConflictAlgorithm.rollback, whereArgs: [id], ); } on DatabaseException catch (e, s) { _logger.severe('Failed to update $tableName', e, s); return Future.error(e); } catch (e, s) { _logger.severe('Unexpected error updating $tableName', e, s); return Future.error(e); } } @override Future delete(int id) async { try { final database = await HabitDatabase().database; await database.delete(tableName, where: 'id = ?', whereArgs: [id]); } on DatabaseException catch (e, s) { _logger.severe('Failed to delete from $tableName', e, s); return Future.error(e); } catch (e, s) { _logger.severe('Unexpected error deleting from $tableName', e, s); return Future.error(e); } } @override Future updateCompletionStatus(int id, bool isCompleted) async { try { final database = await HabitDatabase().database; return await database.update(tableName, { 'isComplete': isCompleted ? 1 : 0, }, where: 'id = ?', conflictAlgorithm: ConflictAlgorithm.rollback, whereArgs: [id], ); } on DatabaseException catch (e, s) { _logger.severe('Failed to update completion status in $tableName', e, s); return Future.error(e); } catch (e, s) { _logger.severe('Unexpected error updating completion status in $tableName', e, s); return Future.error(e); } } }