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 add(final AccountsCompanion account) async { final int id = await _db.into(_db.accounts).insert(account); return find(id); } /// Entfernt ein Konto aus der Datenbank Future 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 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> 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(); } }