2024-05-12 00:08:54 +02:00
|
|
|
import 'package:sqflite/sqflite.dart';
|
|
|
|
import 'package:cpd/database/db.dart';
|
|
|
|
import 'package:cpd/database/habit.dart';
|
|
|
|
|
|
|
|
class TodoDB {
|
|
|
|
final tableName = 'todos';
|
|
|
|
|
|
|
|
Future<void> 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,
|
2024-05-12 17:56:36 +02:00
|
|
|
subtitle TEXT
|
2024-05-12 00:08:54 +02:00
|
|
|
)""");
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<int?> create({required String title, required String subtitle}) async {
|
|
|
|
final database = await HabitDatabase().database;
|
|
|
|
return await database?.rawInsert(
|
2024-05-12 17:56:36 +02:00
|
|
|
'''insert into $tableName (isComplete, title, subtitle)
|
|
|
|
values (?,?,?)''',
|
|
|
|
[0 ,title, subtitle ?? '']);
|
2024-05-12 00:08:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<Habit>?> fetchAll() async {
|
|
|
|
final database = await HabitDatabase().database;
|
2024-05-12 17:56:36 +02:00
|
|
|
final todos = await database?.query(tableName);
|
2024-05-12 00:08:54 +02:00
|
|
|
return todos?.map((todo) => Habit.fromSqfliteDatabase(todo)).toList();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Habit> fetchById(int id) async {
|
|
|
|
final database = await HabitDatabase().database;
|
2024-05-12 17:56:36 +02:00
|
|
|
final todo = await database?.query(tableName, where: 'id = ?', whereArgs: [id]);
|
2024-05-12 00:08:54 +02:00
|
|
|
return Habit.fromSqfliteDatabase(todo!.first);
|
|
|
|
}
|
|
|
|
|
2024-05-12 22:55:54 +02:00
|
|
|
Future<int?> update({required int id, String? title, required String? subtitle}) async {
|
2024-05-12 00:08:54 +02:00
|
|
|
final database = await HabitDatabase().database;
|
|
|
|
return await database?.update(tableName,{
|
|
|
|
if (title != null) 'title': title,
|
2024-05-12 22:55:54 +02:00
|
|
|
if (subtitle != null) 'subtitle': subtitle,
|
2024-05-12 00:08:54 +02:00
|
|
|
},
|
|
|
|
where: 'id = ?',
|
|
|
|
conflictAlgorithm: ConflictAlgorithm.rollback,
|
|
|
|
whereArgs: [id],
|
|
|
|
);
|
|
|
|
}
|
2024-05-12 17:56:36 +02:00
|
|
|
|
2024-05-12 00:08:54 +02:00
|
|
|
Future<void> delete(int id) async {
|
|
|
|
final database = await HabitDatabase().database;
|
2024-05-12 17:56:36 +02:00
|
|
|
await database?.delete(tableName, where: 'id = ?', whereArgs: [id]);
|
2024-05-12 00:08:54 +02:00
|
|
|
}
|
2024-05-13 23:31:03 +02:00
|
|
|
|
|
|
|
Future<int?>? updateCompletionStatus(int id, bool isCompleted) async {
|
|
|
|
final database = await HabitDatabase().database;
|
|
|
|
return await database?.update(tableName,{
|
|
|
|
'isComplete': isCompleted,
|
|
|
|
},
|
|
|
|
where: 'id = ?',
|
|
|
|
conflictAlgorithm: ConflictAlgorithm.rollback,
|
|
|
|
whereArgs: [id],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|