58 lines
1.5 KiB
Dart
58 lines
1.5 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);
|
|
}
|
|
|
|
/// Entfernt ein Konto aus der Datenbank
|
|
Future<int> remove(final Account account) async {
|
|
return (_db.delete(_db.accounts)
|
|
..where((t) => t.id.equals(account.id))).go();
|
|
}
|
|
|
|
/// Sucht ein Konto anhand seiner Id aus der Datenbank
|
|
Future<Account?> find(final int id) async {
|
|
return (_db.select(_db.accounts)
|
|
..where((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,
|
|
}) async {
|
|
final query = _db.select(_db.accounts);
|
|
|
|
if (id != null) {
|
|
query.where((t) => t.id.equals(id));
|
|
}
|
|
|
|
if (name != null && name.isNotEmpty) {
|
|
query.where((t) => t.name.like('%$name%'));
|
|
}
|
|
|
|
if (orderBy != null) {
|
|
switch (orderBy) {
|
|
case 'nameAsc':
|
|
query.orderBy([(t) => OrderingTerm.asc(t.name)]);
|
|
break;
|
|
case 'nameDesc':
|
|
query.orderBy([(t) => OrderingTerm.desc(t.name)]);
|
|
break;
|
|
}
|
|
}
|
|
|
|
return query.get();
|
|
}
|
|
}
|