85 lines
2.0 KiB
Dart
85 lines
2.0 KiB
Dart
|
import 'package:sqflite/sqflite.dart';
|
||
|
import 'package:path/path.dart';
|
||
|
import 'package:todo/database/DatabaseInterface.dart';
|
||
|
import '../buisness/ToDoItem.dart';
|
||
|
|
||
|
class Sqlite implements DatabaseInterface {
|
||
|
static final Sqlite _instance = Sqlite._internal();
|
||
|
factory Sqlite() => _instance;
|
||
|
|
||
|
static Database? _database;
|
||
|
|
||
|
Sqlite._internal();
|
||
|
|
||
|
Future<Database> get database async {
|
||
|
if (_database != null) return _database!;
|
||
|
_database = await _initDatabase();
|
||
|
return _database!;
|
||
|
}
|
||
|
|
||
|
Future<Database> _initDatabase() async {
|
||
|
String path = join(await getDatabasesPath(), 'todo_database.db');
|
||
|
print(path);
|
||
|
return await openDatabase(
|
||
|
path,
|
||
|
version: 1,
|
||
|
onCreate: (db, version) async {
|
||
|
await db.execute(
|
||
|
'''
|
||
|
CREATE TABLE todo_items(
|
||
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||
|
name TEXT,
|
||
|
description TEXT,
|
||
|
dueDate TEXT,
|
||
|
status TEXT
|
||
|
)
|
||
|
''',
|
||
|
);
|
||
|
},
|
||
|
);
|
||
|
}
|
||
|
|
||
|
// ToDoItem hinzufügen
|
||
|
Future<void> insertToDoItem(ToDoItem item) async {
|
||
|
final db = await database;
|
||
|
final id = await db.insert(
|
||
|
'todo_items',
|
||
|
item.toMap(),
|
||
|
conflictAlgorithm: ConflictAlgorithm.replace,
|
||
|
);
|
||
|
item.id = id;
|
||
|
}
|
||
|
|
||
|
Future<List<ToDoItem>> getToDoItems() async {
|
||
|
final db = await database;
|
||
|
final List<Map<String, dynamic>> maps = await db.query('todo_items');
|
||
|
|
||
|
return List.generate(maps.length, (i) {
|
||
|
return ToDoItem.fromMap(maps[i]);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
Future<void> deleteToDoItem(int id) async {
|
||
|
final db = await database;
|
||
|
await db.delete(
|
||
|
'todo_items',
|
||
|
where: 'id = ?',
|
||
|
whereArgs: [id],
|
||
|
);
|
||
|
}
|
||
|
|
||
|
Future<void> updateToDoItem(ToDoItem item) async {
|
||
|
final db = await database;
|
||
|
await db.update(
|
||
|
'todo_items',
|
||
|
item.toMap(),
|
||
|
where: 'id = ?',
|
||
|
whereArgs: [item.id],
|
||
|
);
|
||
|
}
|
||
|
|
||
|
Future<void> closeDatabase() async {
|
||
|
final db = await database;
|
||
|
await db.close();
|
||
|
}
|
||
|
}
|