mirror of
https://github.com/koloideal/Casha.git
synced 2026-06-10 10:25:28 +03:00
update
This commit is contained in:
@@ -24,7 +24,7 @@ class AccountRepository {
|
||||
// Fallback: insert default account if none exists
|
||||
await _db.into(_db.accounts).insert(
|
||||
AccountsCompanion.insert(
|
||||
name: 'Main',
|
||||
name: 'main',
|
||||
isMain: const Value(true),
|
||||
currency: const Value('USD'),
|
||||
sortOrder: const Value(0),
|
||||
@@ -69,7 +69,7 @@ class AccountRepository {
|
||||
try {
|
||||
await _db.into(_db.accounts).insert(
|
||||
AccountsCompanion.insert(
|
||||
name: 'Main',
|
||||
name: 'main',
|
||||
isMain: const Value(true),
|
||||
currency: const Value('USD'),
|
||||
sortOrder: const Value(0),
|
||||
@@ -103,15 +103,61 @@ class AccountRepository {
|
||||
Future<model.Account> getMain() async {
|
||||
final row = await (_db.select(_db.accounts)
|
||||
..where((a) => a.isMain.equals(true)))
|
||||
.getSingle();
|
||||
.getSingleOrNull();
|
||||
|
||||
if (row != null) {
|
||||
return model.Account(
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
isMain: row.isMain,
|
||||
sortOrder: row.sortOrder,
|
||||
currency: row.currency,
|
||||
createdAt: row.createdAt,
|
||||
);
|
||||
}
|
||||
|
||||
// Fallback if no main account is found
|
||||
final all = await getAll();
|
||||
if (all.isNotEmpty) return all.first;
|
||||
|
||||
// Absolute fallback: create and return a default account
|
||||
try {
|
||||
await _db.into(_db.accounts).insert(
|
||||
AccountsCompanion.insert(
|
||||
name: 'main',
|
||||
isMain: const Value(true),
|
||||
currency: const Value('USD'),
|
||||
sortOrder: const Value(0),
|
||||
),
|
||||
);
|
||||
|
||||
// Query the newly created account
|
||||
final newRow = await (_db.select(_db.accounts)
|
||||
..where((a) => a.isMain.equals(true)))
|
||||
.getSingleOrNull();
|
||||
|
||||
if (newRow != null) {
|
||||
return model.Account(
|
||||
id: newRow.id,
|
||||
name: newRow.name,
|
||||
isMain: newRow.isMain,
|
||||
sortOrder: newRow.sortOrder,
|
||||
currency: newRow.currency,
|
||||
createdAt: newRow.createdAt,
|
||||
);
|
||||
}
|
||||
} catch (_) {
|
||||
// Ignore insert errors
|
||||
}
|
||||
|
||||
// Final fallback to prevent crashes
|
||||
return model.Account(
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
isMain: row.isMain,
|
||||
sortOrder: row.sortOrder,
|
||||
currency: row.currency,
|
||||
createdAt: row.createdAt,
|
||||
id: 1,
|
||||
name: 'main',
|
||||
isMain: true,
|
||||
sortOrder: 0,
|
||||
currency: 'USD',
|
||||
createdAt: DateTime.now(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,12 +56,24 @@ class TransactionRepository {
|
||||
/// Add transaction
|
||||
Future<Result<void>> add(model.Transaction transaction) async {
|
||||
return asyncResultOf(() async {
|
||||
print('--- SAVING TRANSACTION: START ---');
|
||||
print('Transaction data: ID=${transaction.id}, Amount=${transaction.amount}, AccId=${transaction.accountId}');
|
||||
print('Category=${transaction.category}, Type=${transaction.type.name}');
|
||||
print('Date=${transaction.date}, Currency=${transaction.currencyCode}');
|
||||
|
||||
final companion = _toCompanion(transaction);
|
||||
print('Companion created successfully');
|
||||
print('Companion: $companion');
|
||||
|
||||
final result = await _db.insertTransaction(companion);
|
||||
print('DB Insert finished. Result Success: ${result.isSuccess}');
|
||||
|
||||
if (result.isFailure) {
|
||||
print('!!! DB INSERT FAILED: ${result.errorOrNull}');
|
||||
throw Exception(result.errorOrNull);
|
||||
}
|
||||
|
||||
print('--- SAVING TRANSACTION: END (SUCCESS) ---');
|
||||
});
|
||||
}
|
||||
|
||||
@@ -173,19 +185,29 @@ class TransactionRepository {
|
||||
|
||||
/// Convert app model to companion for insert
|
||||
TransactionsCompanion _toCompanion(model.Transaction transaction) {
|
||||
return TransactionsCompanion(
|
||||
id: Value(transaction.id),
|
||||
amount: Value(transaction.amount),
|
||||
category: Value(transaction.category),
|
||||
type: Value(transaction.type.name),
|
||||
date: Value(transaction.date),
|
||||
note: Value(transaction.note),
|
||||
recurrence: Value(transaction.recurrence.name),
|
||||
lastOccurrence: Value(transaction.lastOccurrence),
|
||||
currency: Value(transaction.currency),
|
||||
currencyCode: Value(transaction.currencyCode),
|
||||
accountId: Value(transaction.accountId),
|
||||
);
|
||||
try {
|
||||
print('_toCompanion: Creating companion for transaction ${transaction.id}');
|
||||
final companion = TransactionsCompanion(
|
||||
id: Value(transaction.id),
|
||||
amount: Value(transaction.amount),
|
||||
category: Value(transaction.category),
|
||||
type: Value(transaction.type.name),
|
||||
date: Value(transaction.date),
|
||||
note: Value(transaction.note),
|
||||
recurrence: Value(transaction.recurrence.name),
|
||||
lastOccurrence: Value(transaction.lastOccurrence),
|
||||
currency: Value(transaction.currency),
|
||||
currencyCode: Value(transaction.currencyCode),
|
||||
accountId: Value(transaction.accountId),
|
||||
);
|
||||
print('_toCompanion: Companion created successfully');
|
||||
return companion;
|
||||
} catch (e, stack) {
|
||||
print('!!! _toCompanion FAILED !!!');
|
||||
print('Error: $e');
|
||||
print('Stack: $stack');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse recurrence type
|
||||
|
||||
Reference in New Issue
Block a user