Initial Commit, finally got some time to start this
This commit is contained in:
102
lib/repositories/databaserepository.dart
Normal file
102
lib/repositories/databaserepository.dart
Normal file
@@ -0,0 +1,102 @@
|
||||
import 'package:sqflite_common/sqlite_api.dart';
|
||||
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
|
||||
import 'package:simple_notes/models/db/dbnote.dart';
|
||||
import 'package:simple_notes/models/constants.dart';
|
||||
|
||||
class DatabaseRepository {
|
||||
final DatabaseFactory factory;
|
||||
Database? db;
|
||||
|
||||
DatabaseRepository(this.factory);
|
||||
|
||||
Future<void> open(String path) async {
|
||||
return _openPath(path);
|
||||
}
|
||||
|
||||
Future<void> saveNote(DatabaseExecutor? db, DBNote note) async {
|
||||
if (note.id.v != null) {
|
||||
await db!.update(
|
||||
tableNotes,
|
||||
note.toMap(),
|
||||
where: '$columnId = ?',
|
||||
whereArgs: <Object?>[note.id.v]
|
||||
);
|
||||
} else {
|
||||
note.id.v = await db!.insert(tableNotes, note.toMap());
|
||||
}
|
||||
}
|
||||
|
||||
Future<DBNote?> getNote(int? id) async {
|
||||
var list = (await db!.query(
|
||||
tableNotes,
|
||||
columns: [columnId, columnTitle, columnContent],
|
||||
where: "$columnId = ?",
|
||||
whereArgs: <Object?>[id]
|
||||
));
|
||||
|
||||
if(list.isNotEmpty) {
|
||||
return DBNote()..fromMap(list.first);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<List<DBNote>?> getNotes() async {
|
||||
var list = (await db!.query(
|
||||
tableNotes,
|
||||
columns: [columnId, columnTitle, columnContent]
|
||||
));
|
||||
|
||||
if(list.isNotEmpty) {
|
||||
return list.map((e) => DBNote()..fromMap(e)).toList();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<void> deleteNote(int? id) async {
|
||||
await db!.delete(
|
||||
tableNotes,
|
||||
where: "$columnId = ?",
|
||||
whereArgs: <Object?>[id]
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _openPath(String path) async {
|
||||
db = await factory.openDatabase(
|
||||
path,
|
||||
options: OpenDatabaseOptions(
|
||||
version: dbVersionNumber,
|
||||
onCreate: (db, version) async {
|
||||
await _createDB(db);
|
||||
},
|
||||
onUpgrade: (db, oldVersion, newVersion) async {
|
||||
if (oldVersion < dbVersionNumber) {
|
||||
await _createDB(db);
|
||||
}
|
||||
}
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _createDB(Database db) async {
|
||||
await db.execute("DROP TABLE IF EXISTS $tableNotes");
|
||||
await db.execute(
|
||||
"CREATE TABLE $tableNotes($columnId INTEGER PRIMARY KEY, $columnTitle TEXT, $columnContent TEXT)"
|
||||
);
|
||||
|
||||
await saveNote(
|
||||
db,
|
||||
DBNote()
|
||||
..title.v = "Simple Title"
|
||||
..content.v = "Simple Content"
|
||||
);
|
||||
|
||||
await saveNote(
|
||||
db,
|
||||
DBNote()
|
||||
..title.v = "Welcome to the terrible notes app"
|
||||
..content.v = "Enter some notes\nSee if it actually loads\nwill this even work?"
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user