Skip to content

Commit

Permalink
refactoring recurring transactions: db tables
Browse files Browse the repository at this point in the history
  • Loading branch information
mikev-cw committed Apr 22, 2024
1 parent 98e9e39 commit 35d0077
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 97 deletions.
36 changes: 17 additions & 19 deletions lib/database/sossoldi_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import '../model/bank_account.dart';
import '../model/budget.dart';
import '../model/category_transaction.dart';
import '../model/currency.dart';
import '../model/recurring_transaction_amount.dart';
import '../model/recurring_transaction.dart';
import '../model/transaction.dart';

class SossoldiDatabase {
Expand Down Expand Up @@ -75,25 +75,22 @@ class SossoldiDatabase {
`${TransactionFields.idBankAccount}` $integerNotNull,
`${TransactionFields.idBankAccountTransfer}` $integer,
`${TransactionFields.recurring}` $integerNotNull CHECK (${TransactionFields.recurring} IN (0, 1)),
`${TransactionFields.recurrencyType}` $text,
`${TransactionFields.recurrencyPayDay}` $integer,
`${TransactionFields.recurrencyFrom}` $text,
`${TransactionFields.recurrencyTo}` $text,
`${TransactionFields.idRecurringTransaction}` $integer,
`${TransactionFields.createdAt}` $textNotNull,
`${TransactionFields.updatedAt}` $textNotNull
)
''');

// Recurring Transactions Amount Table
await database.execute('''
CREATE TABLE `$recurringTransactionAmountTable`(
`${RecurringTransactionAmountFields.id}` $integerPrimaryKeyAutoincrement,
`${RecurringTransactionAmountFields.from}` $textNotNull,
`${RecurringTransactionAmountFields.to}` $textNotNull,
`${RecurringTransactionAmountFields.amount}` $realNotNull,
`${RecurringTransactionAmountFields.idTransaction}` $integerNotNull,
`${RecurringTransactionAmountFields.createdAt}` $textNotNull,
`${RecurringTransactionAmountFields.updatedAt}` $textNotNull
CREATE TABLE `$recurringTransactionTable`(
`${RecurringTransactionFields.id}` $integerPrimaryKeyAutoincrement,
`${RecurringTransactionFields.from}` $textNotNull,
`${RecurringTransactionFields.to}` $text,
`${RecurringTransactionFields.amount}` $realNotNull,
`${RecurringTransactionFields.lastInsertion}` $text,
`${RecurringTransactionFields.createdAt}` $textNotNull,
`${RecurringTransactionFields.updatedAt}` $textNotNull
)
''');

Expand Down Expand Up @@ -185,7 +182,7 @@ class SossoldiDatabase {
DateTime now = DateTime.now();

// start building mega-query
const insertDemoTransactionsQuery = '''INSERT INTO `transaction` (date, amount, type, note, idCategory, idBankAccount, idBankAccountTransfer, recurring, recurrencyType, recurrencyPayDay, recurrencyFrom, recurrencyTo, createdAt, updatedAt) VALUES ''';
const insertDemoTransactionsQuery = '''INSERT INTO `transaction` (date, amount, type, note, idCategory, idBankAccount, idBankAccountTransfer, recurring, idRecurringTransaction, createdAt, updatedAt) VALUES ''';

// init a List with transaction values
final List<String> demoTransactions = [];
Expand Down Expand Up @@ -224,20 +221,21 @@ class SossoldiDatabase {
}

// put generated transaction in our list
demoTransactions.add('''('$randomDate', ${randomAmount.toStringAsFixed(2)}, '$randomType', '$randomNote', $randomCategory, $randomAccount, $idBankAccountTransfer, 0, null, null, null, null, '$randomDate', '$randomDate')''');
demoTransactions.add('''('$randomDate', ${randomAmount.toStringAsFixed(2)}, '$randomType', '$randomNote', $randomCategory, $randomAccount, $idBankAccountTransfer, 0, null, '$randomDate', '$randomDate')''');
}

// add salary every month
for (int i = 1; i < dateInPastMaxRange/30; i++) {
DateTime randomDate = now.subtract(Duration(days: 30*i));
var time = randomDate.toLocal();
DateTime salaryDateTime = DateTime(time.year, time.month, 27, time.hour, time.minute, time.second, time.millisecond, time.microsecond);
demoTransactions.add('''('$salaryDateTime', $fakeSalary, 'IN', 'Salary', 15, 70, null, 0, null, null, null, null, '$salaryDateTime', '$salaryDateTime')''');
demoTransactions.add('''('$salaryDateTime', $fakeSalary, 'IN', 'Salary', 15, 70, null, 0, null, '$salaryDateTime', '$salaryDateTime')''');
}

// add some recurring payment too
demoTransactions.add('''(null, 7.99, 'OUT', 'Netflix', 14, 71, null, 1, 'monthly', 19, '2022-11-14', null, '2022-11-14 03:33:36.048611', '2022-11-14 03:33:36.048611')''');
demoTransactions.add('''(null, 292.39, 'OUT', 'Car Loan', 13, 70, null, 1, 'monthly', 27, '2019-10-03', '2024-10-02', '2022-10-04 03:33:36.048611', '2022-10-04 03:33:36.048611')''');
// TODO TODO TODO TODO TODO TODO TODO
// demoTransactions.add('''(null, 7.99, 'OUT', 'Netflix', 14, 71, null, 1, 'monthly', 19, '2022-11-14', null, '2022-11-14 03:33:36.048611', '2022-11-14 03:33:36.048611')''');
// demoTransactions.add('''(null, 292.39, 'OUT', 'Car Loan', 13, 70, null, 1, 'monthly', 27, '2019-10-03', '2024-10-02', '2022-10-04 03:33:36.048611', '2022-10-04 03:33:36.048611')''');

// finalize query and write!
await _database?.execute("$insertDemoTransactionsQuery ${demoTransactions.join(",")};");
Expand All @@ -249,7 +247,7 @@ class SossoldiDatabase {
var batch = txn.batch();
batch.delete(bankAccountTable);
batch.delete(transactionTable);
batch.delete(recurringTransactionAmountTable);
batch.delete(recurringTransactionTable);
batch.delete(categoryTransactionTable);
batch.delete(budgetTable);
batch.delete(currencyTable);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import '../database/sossoldi_database.dart';
import 'base_entity.dart';

const String recurringTransactionAmountTable = 'recurringTransactionAmount';
const String recurringTransactionTable = 'recurringTransaction';

class RecurringTransactionAmountFields extends BaseEntityFields {
class RecurringTransactionFields extends BaseEntityFields {
static String id = BaseEntityFields.getId;
static String from = 'from';
static String to = 'to';
static String amount = 'amount';
static String idTransaction = 'idTransaction'; // FK
static String lastInsertion = 'lastInsertion';
static String createdAt = BaseEntityFields.getCreatedAt;
static String updatedAt = BaseEntityFields.getUpdatedAt;

Expand All @@ -17,127 +17,125 @@ class RecurringTransactionAmountFields extends BaseEntityFields {
from,
to,
amount,
idTransaction,
lastInsertion,
BaseEntityFields.createdAt,
BaseEntityFields.updatedAt
];
}

class RecurringTransactionAmount extends BaseEntity {
class RecurringTransaction extends BaseEntity {
final DateTime from;
final DateTime to;
final num amount;
final int? idTransaction;
final DateTime? lastInsertion;

const RecurringTransactionAmount(
const RecurringTransaction(
{int? id,
required this.from,
required this.to,
required this.amount,
required this.idTransaction,
this.lastInsertion,
DateTime? createdAt,
DateTime? updatedAt})
: super(id: id, createdAt: createdAt, updatedAt: updatedAt);

RecurringTransactionAmount copy(
RecurringTransaction copy(
{int? id,
DateTime? from,
DateTime? to,
num? amount,
int? idTransaction,
DateTime? lastInsertion,
DateTime? createdAt,
DateTime? updatedAt}) =>
RecurringTransactionAmount(
RecurringTransaction(
id: id ?? this.id,
from: from ?? this.from,
to: to ?? this.to,
amount: amount ?? this.amount,
idTransaction:
idTransaction ?? this.idTransaction,
lastInsertion: lastInsertion ?? this.lastInsertion,
createdAt: createdAt ?? this.createdAt,
updatedAt: updatedAt ?? this.updatedAt);

static RecurringTransactionAmount fromJson(Map<String, Object?> json) =>
RecurringTransactionAmount(
static RecurringTransaction fromJson(Map<String, Object?> json) =>
RecurringTransaction(
id: json[BaseEntityFields.id] as int?,
from: DateTime.parse(
json[RecurringTransactionAmountFields.from] as String),
json[RecurringTransactionFields.from] as String),
to: DateTime.parse(
json[RecurringTransactionAmountFields.to] as String),
amount: json[RecurringTransactionAmountFields.amount] as num,
idTransaction:
json[RecurringTransactionAmountFields.idTransaction]
as int,
json[RecurringTransactionFields.to] as String),
amount: json[RecurringTransactionFields.amount] as num,
lastInsertion:
json[RecurringTransactionFields.lastInsertion]
as DateTime,
createdAt: DateTime.parse(json[BaseEntityFields.createdAt] as String),
updatedAt:
DateTime.parse(json[BaseEntityFields.updatedAt] as String));

Map<String, Object?> toJson() => {
BaseEntityFields.id: id,
RecurringTransactionAmountFields.from: from.toIso8601String(),
RecurringTransactionAmountFields.to: to.toIso8601String(),
RecurringTransactionAmountFields.amount: amount,
RecurringTransactionAmountFields.idTransaction:
idTransaction,
RecurringTransactionFields.from: from.toIso8601String(),
RecurringTransactionFields.to: to.toIso8601String(),
RecurringTransactionFields.amount: amount,
RecurringTransactionFields.lastInsertion: lastInsertion?.toIso8601String(),
BaseEntityFields.createdAt: createdAt?.toIso8601String(),
BaseEntityFields.updatedAt: updatedAt?.toIso8601String(),
};
}

class RecurringTransactionMethods extends SossoldiDatabase {
Future<RecurringTransactionAmount> insert(RecurringTransactionAmount item) async {
Future<RecurringTransaction> insert(RecurringTransaction item) async {
final db = await database;
final id = await db.insert(recurringTransactionAmountTable, item.toJson());
final id = await db.insert(recurringTransactionTable, item.toJson());
return item.copy(id: id);
}


Future<RecurringTransactionAmount> selectById(int id) async {
Future<RecurringTransaction> selectById(int id) async {
final db = await database;

final maps = await db.query(
recurringTransactionAmountTable,
columns: RecurringTransactionAmountFields.allFields,
where: '${RecurringTransactionAmountFields.id} = ?',
recurringTransactionTable,
columns: RecurringTransactionFields.allFields,
where: '${RecurringTransactionFields.id} = ?',
whereArgs: [id],
);

if (maps.isNotEmpty) {
return RecurringTransactionAmount.fromJson(maps.first);
return RecurringTransaction.fromJson(maps.first);
} else {
throw Exception('ID $id not found');
}
}

Future<List<RecurringTransactionAmount>> selectAll() async {
Future<List<RecurringTransaction>> selectAll() async {
final db = await database;

final orderByASC = '${RecurringTransactionAmountFields.createdAt} ASC';
final orderByASC = '${RecurringTransactionFields.createdAt} ASC';

final result = await db.query(recurringTransactionAmountTable, orderBy: orderByASC);
final result = await db.query(recurringTransactionTable, orderBy: orderByASC);

return result.map((json) => RecurringTransactionAmount.fromJson(json)).toList();
return result.map((json) => RecurringTransaction.fromJson(json)).toList();
}

Future<int> updateItem(RecurringTransactionAmount item) async {
Future<int> updateItem(RecurringTransaction item) async {
final db = await database;

// You can use `rawUpdate` to write the query in SQL
return db.update(
recurringTransactionAmountTable,
recurringTransactionTable,
item.toJson(),
where:
'${RecurringTransactionAmountFields.id} = ?',
'${RecurringTransactionFields.id} = ?',
whereArgs: [item.id],
);
}

Future<int> deleteById(int id) async {
final db = await database;

return await db.delete(recurringTransactionAmountTable,
return await db.delete(recurringTransactionTable,
where:
'${RecurringTransactionAmountFields.id} = ?',
'${RecurringTransactionFields.id} = ?',
whereArgs: [id]);
}

Expand Down
44 changes: 8 additions & 36 deletions lib/model/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ class TransactionFields extends BaseEntityFields {
static String idBankAccountTransfer = 'idBankAccountTransfer';
static String bankAccountTransferName = 'bankAccountTransferName';
static String recurring = 'recurring';
static String recurrencyType = 'recurrencyType';
static String recurrencyPayDay = 'recurrencyPayDay';
static String recurrencyFrom = 'recurrencyFrom';
static String recurrencyTo = 'recurrencyTo';
static String idRecurringTransaction = 'idRecurringTransaction';
static String createdAt = BaseEntityFields.getCreatedAt;
static String updatedAt = BaseEntityFields.getUpdatedAt;

Expand All @@ -37,10 +34,7 @@ class TransactionFields extends BaseEntityFields {
idBankAccount,
idBankAccountTransfer,
recurring,
recurrencyType,
recurrencyPayDay,
recurrencyFrom,
recurrencyTo,
idRecurringTransaction,
BaseEntityFields.createdAt,
BaseEntityFields.updatedAt
];
Expand Down Expand Up @@ -79,10 +73,7 @@ class Transaction extends BaseEntity {
final int? idBankAccountTransfer;
final String? bankAccountTransferName;
final bool recurring;
final String? recurrencyType;
final int? recurrencyPayDay;
final DateTime? recurrencyFrom;
final DateTime? recurrencyTo;
final int? idRecurringTransaction;

const Transaction(
{super.id,
Expand All @@ -99,10 +90,7 @@ class Transaction extends BaseEntity {
this.idBankAccountTransfer,
this.bankAccountTransferName,
required this.recurring,
this.recurrencyType,
this.recurrencyPayDay,
this.recurrencyFrom,
this.recurrencyTo,
this.idRecurringTransaction,
super.createdAt,
super.updatedAt});

Expand All @@ -116,10 +104,7 @@ class Transaction extends BaseEntity {
int? idBankAccount,
int? idBankAccountTransfer,
bool? recurring,
String? recurrencyType,
int? recurrencyPayDay,
DateTime? recurrencyFrom,
DateTime? recurrencyTo,
int? idRecurringTransaction,
DateTime? createdAt,
DateTime? updatedAt}) =>
Transaction(
Expand All @@ -132,10 +117,7 @@ class Transaction extends BaseEntity {
idBankAccount: idBankAccount ?? this.idBankAccount,
idBankAccountTransfer: idBankAccountTransfer ?? this.idBankAccountTransfer,
recurring: recurring ?? this.recurring,
recurrencyType: recurrencyType ?? this.recurrencyType,
recurrencyPayDay: recurrencyPayDay ?? this.recurrencyPayDay,
recurrencyFrom: recurrencyFrom ?? this.recurrencyFrom,
recurrencyTo: recurrencyTo ?? this.recurrencyTo,
idRecurringTransaction: idRecurringTransaction ?? this.idRecurringTransaction,
createdAt: createdAt ?? this.createdAt,
updatedAt: updatedAt ?? this.updatedAt);

Expand All @@ -154,14 +136,7 @@ class Transaction extends BaseEntity {
idBankAccountTransfer: json[TransactionFields.idBankAccountTransfer] as int?,
bankAccountTransferName: json[TransactionFields.bankAccountTransferName] as String?,
recurring: json[TransactionFields.recurring] == 1 ? true : false,
recurrencyType: json[TransactionFields.recurrencyType] as String?,
recurrencyPayDay: json[TransactionFields.recurrencyPayDay] as int?,
recurrencyFrom: json[TransactionFields.recurrencyFrom] != null
? DateTime.parse(TransactionFields.recurrencyFrom)
: null,
recurrencyTo: json[TransactionFields.recurrencyTo] != null
? DateTime.parse(TransactionFields.recurrencyTo)
: null,
idRecurringTransaction: json[TransactionFields.idRecurringTransaction] as int?,
createdAt: DateTime.parse(json[BaseEntityFields.createdAt] as String),
updatedAt: DateTime.parse(json[BaseEntityFields.updatedAt] as String));

Expand All @@ -175,10 +150,7 @@ class Transaction extends BaseEntity {
TransactionFields.idBankAccount: idBankAccount,
TransactionFields.idBankAccountTransfer: idBankAccountTransfer,
TransactionFields.recurring: recurring ? 1 : 0,
TransactionFields.recurrencyType: recurrencyType,
TransactionFields.recurrencyPayDay: recurrencyPayDay,
TransactionFields.recurrencyFrom: recurrencyFrom,
TransactionFields.recurrencyTo: recurrencyTo,
TransactionFields.idRecurringTransaction: idRecurringTransaction,
BaseEntityFields.createdAt:
update ? createdAt?.toIso8601String() : DateTime.now().toIso8601String(),
BaseEntityFields.updatedAt: DateTime.now().toIso8601String(),
Expand Down

0 comments on commit 35d0077

Please sign in to comment.