refactor: Improve code formatting and readability in database and note repository
This commit is contained in:
+43
-14
@@ -11,8 +11,10 @@ part 'app_database.g.dart';
|
||||
class Categories extends Table {
|
||||
TextColumn get uuid => text().unique()();
|
||||
TextColumn get encryptedName => text().named('encrypted_name')();
|
||||
IntColumn get serverVersion => integer().named('server_version').withDefault(const Constant(0))();
|
||||
BoolColumn get isDeleted => boolean().named('is_deleted').withDefault(const Constant(false))();
|
||||
IntColumn get serverVersion =>
|
||||
integer().named('server_version').withDefault(const Constant(0))();
|
||||
BoolColumn get isDeleted =>
|
||||
boolean().named('is_deleted').withDefault(const Constant(false))();
|
||||
DateTimeColumn get updatedAt => dateTime().named('updated_at')();
|
||||
|
||||
@override
|
||||
@@ -28,8 +30,10 @@ class Notes extends Table {
|
||||
DateTimeColumn get createdAt => dateTime().named('created_at')();
|
||||
DateTimeColumn get updatedAt => dateTime().named('updated_at')();
|
||||
IntColumn get sortIndex => integer().named('sort_index')();
|
||||
IntColumn get serverVersion => integer().named('server_version').withDefault(const Constant(0))();
|
||||
BoolColumn get isDeleted => boolean().named('is_deleted').withDefault(const Constant(false))();
|
||||
IntColumn get serverVersion =>
|
||||
integer().named('server_version').withDefault(const Constant(0))();
|
||||
BoolColumn get isDeleted =>
|
||||
boolean().named('is_deleted').withDefault(const Constant(false))();
|
||||
TextColumn get categoryId => text().nullable().named('category_id')();
|
||||
}
|
||||
|
||||
@@ -37,7 +41,8 @@ class Notes extends Table {
|
||||
class AppDatabase extends _$AppDatabase {
|
||||
@override
|
||||
int get schemaVersion => 1;
|
||||
AppDatabase({required String encryptionKey}) : super(_openConnection(encryptionKey));
|
||||
AppDatabase({required String encryptionKey})
|
||||
: super(_openConnection(encryptionKey));
|
||||
|
||||
// ========== Categories ==========
|
||||
Future<List<DbCategory>> getAllCategories() {
|
||||
@@ -49,8 +54,9 @@ class AppDatabase extends _$AppDatabase {
|
||||
}
|
||||
|
||||
Future<void> deleteCategory(String uuid) {
|
||||
return (update(categories)..where((c) => c.uuid.equals(uuid)))
|
||||
.write(CategoriesCompanion(isDeleted: Value(true)));
|
||||
return (update(categories)..where((c) => c.uuid.equals(uuid))).write(
|
||||
CategoriesCompanion(isDeleted: Value(true)),
|
||||
);
|
||||
}
|
||||
|
||||
// ========== Notes ==========
|
||||
@@ -63,7 +69,9 @@ class AppDatabase extends _$AppDatabase {
|
||||
|
||||
Future<int> insertNoteAtTop(NotesCompanion note) {
|
||||
return transaction(() async {
|
||||
await customStatement('UPDATE notes SET sort_index = sort_index + 1 WHERE is_deleted = 0');
|
||||
await customStatement(
|
||||
'UPDATE notes SET sort_index = sort_index + 1 WHERE is_deleted = 0',
|
||||
);
|
||||
return into(notes).insert(note.copyWith(sortIndex: const Value<int>(0)));
|
||||
});
|
||||
}
|
||||
@@ -83,7 +91,12 @@ class AppDatabase extends _$AppDatabase {
|
||||
}
|
||||
|
||||
Future<void> deleteNote(int id, int removedIndex) async {
|
||||
await (update(notes)..where((n) => n.id.equals(id))).write(NotesCompanion(isDeleted: Value(true)));
|
||||
await (update(notes)..where((n) => n.id.equals(id))).write(
|
||||
NotesCompanion(
|
||||
isDeleted: const Value(true),
|
||||
updatedAt: Value(DateTime.now()),
|
||||
),
|
||||
);
|
||||
|
||||
await customStatement(
|
||||
'UPDATE notes SET sort_index = sort_index - 1 WHERE sort_index > ? AND is_deleted = 0',
|
||||
@@ -121,19 +134,35 @@ class AppDatabase extends _$AppDatabase {
|
||||
}
|
||||
|
||||
await customStatement(
|
||||
'UPDATE notes SET sort_index = ? WHERE id = ?',
|
||||
[newIndex, id],
|
||||
'UPDATE notes SET sort_index = ?, updated_at = ? WHERE id = ?',
|
||||
[newIndex, DateTime.now().toIso8601String(), id],
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Future<List<DbNote>> getNotesChangedSince(DateTime since) {
|
||||
return (select(
|
||||
notes,
|
||||
)..where((n) => n.updatedAt.isBiggerThanValue(since))).get();
|
||||
}
|
||||
|
||||
Future<List<DbCategory>> getCategoriesChangedSince(DateTime since) {
|
||||
return (select(
|
||||
categories,
|
||||
)..where((c) => c.updatedAt.isBiggerThanValue(since))).get();
|
||||
}
|
||||
|
||||
// ========== Sync helpers ==========
|
||||
Future<List<DbNote>> getUnsyncedNotes() {
|
||||
return (select(notes)..where((n) => n.isDeleted.equals(true) | n.serverVersion.equals(0))).get();
|
||||
return (select(notes)
|
||||
..where((n) => n.isDeleted.equals(true) | n.serverVersion.equals(0)))
|
||||
.get();
|
||||
}
|
||||
|
||||
Future<List<DbCategory>> getUnsyncedCategories() {
|
||||
return (select(categories)..where((c) => c.isDeleted.equals(true) | c.serverVersion.equals(0))).get();
|
||||
return (select(categories)
|
||||
..where((c) => c.isDeleted.equals(true) | c.serverVersion.equals(0)))
|
||||
.get();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,4 +183,4 @@ LazyDatabase _openConnection(String encryptionKey) {
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user