67 lines
1.9 KiB
Dart
67 lines
1.9 KiB
Dart
import 'package:drift/drift.dart';
|
|
|
|
import '../Entities/drift_database.dart';
|
|
import '../Services/database_service.dart';
|
|
|
|
/// Funktionen zum interagieren mit der Datenbank für die Konten
|
|
class AccountRepository {
|
|
final AppDatabase _db = DatabaseService().database;
|
|
|
|
/// Fügt ein neues Konto zur Datenbank hinzu
|
|
Future<Account?> add(final AccountsCompanion account) async {
|
|
final int id = await _db.into(_db.accounts).insert(account);
|
|
return find(id);
|
|
}
|
|
|
|
/// Aktualisiert ein Konto in der Datenbank
|
|
Future<bool> update(final AccountsCompanion account) {
|
|
final AccountsCompanion accountToUpdate = AccountsCompanion(
|
|
id: account.id,
|
|
name: account.name,
|
|
updatedAt: Value(DateTime.now()),
|
|
);
|
|
|
|
return _db.update(_db.accounts).replace(accountToUpdate);
|
|
}
|
|
|
|
/// Entfernt ein Konto aus der Datenbank
|
|
Future<int> remove(final Account account) => (_db.delete(
|
|
_db.accounts,
|
|
)..where((final t) => t.id.equals(account.id))).go();
|
|
|
|
/// Sucht ein Konto anhand seiner Id aus der Datenbank
|
|
Future<Account?> find(final int id) => (_db.select(
|
|
_db.accounts,
|
|
)..where((final t) => t.id.equals(id))).getSingleOrNull();
|
|
|
|
/// Sucht Konten anhand der gegebenen Parameter aus der Datenbank
|
|
Future<List<Account>> findBy({
|
|
final int? id,
|
|
final String? name,
|
|
final String? orderBy,
|
|
}) {
|
|
final SimpleSelectStatement<$AccountsTable, Account> query = _db.select(
|
|
_db.accounts,
|
|
);
|
|
|
|
if (id != null) {
|
|
query.where((final t) => t.id.equals(id));
|
|
}
|
|
|
|
if (name != null && name.isNotEmpty) {
|
|
query.where((final t) => t.name.like('%$name%'));
|
|
}
|
|
|
|
if (orderBy != null) {
|
|
switch (orderBy) {
|
|
case 'nameAsc':
|
|
query.orderBy([(final t) => OrderingTerm.asc(t.name)]);
|
|
case 'nameDesc':
|
|
query.orderBy([(final t) => OrderingTerm.desc(t.name)]);
|
|
}
|
|
}
|
|
|
|
return query.get();
|
|
}
|
|
}
|