117 lines
3.4 KiB
Dart
117 lines
3.4 KiB
Dart
import 'package:drift/drift.dart';
|
|
|
|
import '../Entities/drift_database.dart';
|
|
import '../Entities/time_frame_enum.dart';
|
|
import '../Services/database_service.dart';
|
|
|
|
/// Funktionen zum interagieren mit der Datenbank für die Transaktionen
|
|
class RecurringTransactionRepository {
|
|
final AppDatabase _db = DatabaseService().database;
|
|
|
|
/// Fügt eine neue wiederkehrende Transaktion zur Datenbank hinzu
|
|
Future<RecurringTransaction?> add(
|
|
final RecurringTransactionsCompanion recurringTransaction,
|
|
) async {
|
|
final int id = await _db.into(_db.recurringTransactions).insert(
|
|
recurringTransaction);
|
|
return find(id);
|
|
}
|
|
|
|
/// Entfernt eine wiederkehrende Transaktion aus der Datenbank
|
|
Future<int> remove(final RecurringTransaction recurringTransaction) async {
|
|
return (_db.delete(_db.recurringTransactions)
|
|
..where((t) => t.id.equals(recurringTransaction.id))).go();
|
|
}
|
|
|
|
/// Sucht eine wiederkehrende Transaktion anhand seiner Id aus der Datenbank
|
|
Future<RecurringTransaction?> find(final int id) async {
|
|
return (_db.select(_db.recurringTransactions)
|
|
..where((t) => t.id.equals(id))).getSingleOrNull();
|
|
}
|
|
|
|
/// Sucht wiederkehrende Transaktionen anhand der gegebenen Parameter
|
|
/// aus der Datenbank
|
|
Future<List<RecurringTransaction>> findBy({
|
|
final int? id,
|
|
final String? name,
|
|
final DateTime? startDate,
|
|
final DateTime? startDateBefore,
|
|
final DateTime? startDateAfter,
|
|
final DateTime? startDateFrom,
|
|
final DateTime? startDateTo,
|
|
final TimeFrameEnum? timeFrame,
|
|
final double? amount,
|
|
final double? amountMin,
|
|
final double? amountMax,
|
|
final String? orderBy,
|
|
}) async {
|
|
final query = _db.select(_db.recurringTransactions);
|
|
|
|
if (id != null) {
|
|
query.where((t) => t.id.equals(id));
|
|
}
|
|
|
|
if (name != null && name.isNotEmpty) {
|
|
query.where((t) => t.name.like('%$name%'));
|
|
}
|
|
|
|
if (startDate != null) {
|
|
query.where((t) => t.startDate.equals(startDate));
|
|
}
|
|
|
|
if (startDateAfter != null) {
|
|
query.where((t) => t.startDate.isBiggerThanValue(startDateAfter));
|
|
}
|
|
|
|
if (startDateBefore != null) {
|
|
query.where((t) => t.startDate.isSmallerThanValue(startDateBefore));
|
|
}
|
|
|
|
if (startDateFrom != null && startDateTo != null) {
|
|
query.where((t) =>
|
|
t.startDate.isBetweenValues(startDateFrom, startDateTo));
|
|
}
|
|
|
|
if (timeFrame != null) {
|
|
query.where((t) => t.timeFrame.equals(timeFrame.index));
|
|
}
|
|
|
|
if (amount != null) {
|
|
query.where((t) => t.amount.equals(amount));
|
|
}
|
|
|
|
if (amountMin != null) {
|
|
query.where((t) => t.amount.isBiggerThanValue(amountMin));
|
|
}
|
|
|
|
if (amountMax != null) {
|
|
query.where((t) => t.amount.isSmallerThanValue(amountMax));
|
|
}
|
|
|
|
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;
|
|
case 'amountAsc':
|
|
query.orderBy([(t) => OrderingTerm.asc(t.amount)]);
|
|
break;
|
|
case 'amountDesc':
|
|
query.orderBy([(t) => OrderingTerm.desc(t.amount)]);
|
|
break;
|
|
case 'startDateAsc':
|
|
query.orderBy([(t) => OrderingTerm.asc(t.startDate)]);
|
|
break;
|
|
case 'startDateDesc':
|
|
query.orderBy([(t) => OrderingTerm.desc(t.startDate)]);
|
|
break;
|
|
}
|
|
}
|
|
|
|
return query.get();
|
|
}
|
|
}
|