diff --git a/lib/api/apis/aggregated_expense_api.dart b/lib/api/apis/aggregated_expense_api.dart index ef8d340..15482b5 100644 --- a/lib/api/apis/aggregated_expense_api.dart +++ b/lib/api/apis/aggregated_expense_api.dart @@ -10,10 +10,10 @@ class AggregatedExpenseApi { Future create( {required final double amount, required final DateTime monthDate, - required final List tags}) async { + required final String tags}) async { return _db.into(_db.aggregatedExpenseTable).insert( AggregatedExpenseTableCompanion.insert( - amount: amount, tags: processTag(tags), createdMonthDate: monthDate)); + amount: amount, tags: tags, createdMonthDate: monthDate)); } Future> get() async { @@ -29,12 +29,10 @@ class AggregatedExpenseApi { } Future getByMonthAndTag( - {required final DateTime monthDate, - required final List tags}) async { + {required final DateTime monthDate, required final String tags}) async { return (_db.select(_db.aggregatedExpenseTable) ..where((t) => - t.createdMonthDate.equals(monthDate) & - t.tags.equals(processTag(tags)))) + t.createdMonthDate.equals(monthDate) & t.tags.equals(tags))) .getSingleOrNull(); } @@ -42,12 +40,12 @@ class AggregatedExpenseApi { {required final int id, required final double amount, required final DateTime createdOn, - required final List tags}) async { + required final String tags}) async { return (_db.update(_db.aggregatedExpenseTable) ..where((t) => t.id.equals(id))) .write(AggregatedExpenseTableCompanion( amount: Value(amount), - tags: Value(processTag(tags)), + tags: Value(tags), createdMonthDate: Value(createdOn))); } @@ -62,9 +60,4 @@ class AggregatedExpenseApi { ..where((t) => t.createdMonthDate.equals(monthDate))) .go(); } - - String processTag(final List tags) { - tags.sort(); - return tags.join(','); - } } diff --git a/lib/api/apis/expense_api.dart b/lib/api/apis/expense_api.dart index 01103c6..bbda193 100644 --- a/lib/api/apis/expense_api.dart +++ b/lib/api/apis/expense_api.dart @@ -10,11 +10,11 @@ class ExpenseApi { {required final double amount, required final String? description, required final DateTime createdOn, - required final List tags}) async { + required final String tags}) async { return _db.into(_db.expenseTable).insert(ExpenseTableCompanion.insert( amount: amount, description: Value(description), - tags: tags.join(','), + tags: tags, createdOn: createdOn)); } @@ -44,12 +44,12 @@ class ExpenseApi { required final double amount, required final String? description, required final DateTime createdOn, - required final List tags}) async { + required final String tags}) async { return (_db.update(_db.expenseTable)..where((t) => t.id.equals(id))).write( ExpenseTableCompanion( amount: Value(amount), description: Value(description), - tags: Value(tags.join(',')), + tags: Value(tags), createdOn: Value(createdOn))); } @@ -58,7 +58,10 @@ class ExpenseApi { } Future deleteByMonthDate({required final DateTime monthDate}) async { - return (_db.delete(_db.expenseTable)..where((t) => t.createdOn.year.equals(monthDate.year) & - t.createdOn.month.equals(monthDate.month))).go(); + return (_db.delete(_db.expenseTable) + ..where((t) => + t.createdOn.year.equals(monthDate.year) & + t.createdOn.month.equals(monthDate.month))) + .go(); } } diff --git a/lib/domain/services/expense_service.dart b/lib/domain/services/expense_service.dart index 92952a4..b3b7ca3 100644 --- a/lib/domain/services/expense_service.dart +++ b/lib/domain/services/expense_service.dart @@ -1,5 +1,6 @@ import 'package:wealth_wave/api/apis/aggregated_expense_api.dart'; import 'package:wealth_wave/api/apis/expense_api.dart'; +import 'package:wealth_wave/api/db/app_database.dart'; import 'package:wealth_wave/domain/models/aggregated_expense.dart'; import 'package:wealth_wave/domain/models/expense.dart'; @@ -19,127 +20,113 @@ class ExpenseService { : _expenseApi = expenseApi ?? ExpenseApi(), _aggregatedExpenseApi = aggregatedExpenseApi ?? AggregatedExpenseApi(); - Future createExpense({ + Future createExpense({ required final String? description, required final double amount, required final List tags, required final DateTime createdOn, }) async { - final int id = await _expenseApi.create( + final tagsString = _processTags(tags: tags); + await _updateAggregatedExpense( + dateTime: createdOn, tags: tagsString, amount: amount); + await _expenseApi.create( description: description, amount: amount, - tags: tags, + tags: tagsString, createdOn: createdOn, ); - final expenseDO = await _expenseApi.getBy(id: id); - - final DateTime monthDate = DateTime(createdOn.year, createdOn.month); - final aggregatedExpense = await _aggregatedExpenseApi.getByMonthAndTag( - monthDate: monthDate, - tags: tags, - ); - - if (aggregatedExpense == null) { - await _aggregatedExpenseApi.create( - amount: amount, - monthDate: monthDate, - tags: tags, - ); - } else { - await _aggregatedExpenseApi.update( - id: aggregatedExpense.id, - amount: aggregatedExpense.amount + amount, - createdOn: monthDate, - tags: tags, - ); - } - - return Expense.from(expenseDO: expenseDO); } - Future updateExpense({ + Future updateExpense({ required final int id, required final String? description, required final double amount, required final List tags, required final DateTime createdOn, }) async { - final expenseDO = await _expenseApi.getBy(id: id); - DateTime monthDate = - DateTime(expenseDO.createdOn.year, expenseDO.createdOn.month); - - final aggregatedExpense = await _aggregatedExpenseApi.getByMonthAndTag( - monthDate: monthDate, tags: tags); - if (aggregatedExpense != null) { - await _aggregatedExpenseApi.update( - id: aggregatedExpense.id, - amount: aggregatedExpense.amount - - expenseDO.amount + - amount, // Adjust the amount correctly - createdOn: monthDate, - tags: tags, - ); - } else { - await _aggregatedExpenseApi.create( - amount: amount, monthDate: monthDate, tags: tags); - } - + final existingExpenseDO = await _expenseApi.getBy(id: id); + await _updateAggregatedExpense( + dateTime: existingExpenseDO.createdOn, + tags: existingExpenseDO.tags, + amount: -existingExpenseDO.amount); + + final tagsString = _processTags(tags: tags); + await _updateAggregatedExpense( + dateTime: createdOn, tags: tagsString, amount: amount); await _expenseApi.update( - id: id, - description: description, - amount: amount, - tags: tags, - createdOn: createdOn, - ); - - return await _expenseApi - .getBy(id: id) - .then((value) => Expense.from(expenseDO: value)); + id: id, + amount: amount, + description: description, + createdOn: createdOn, + tags: tagsString); } - Future getById({required final int id}) => _expenseApi - .getBy(id: id) - .then((expenseDO) => Expense.from(expenseDO: expenseDO)); + Future getById({required final int id}) async { + ExpenseDO expenseDO = await _expenseApi.getBy(id: id); + return Expense.from(expenseDO: expenseDO); + } Future> getExpensesForMonthDate( - {required final DateTime monthDate}) { - return _expenseApi.getByMonth(monthDate: monthDate).then((expenseDOs) => - expenseDOs - .map((expenseDO) => Expense.from(expenseDO: expenseDO)) - .toList()); + {required final DateTime monthDate}) async { + List expenseDOs = + await _expenseApi.getByMonth(monthDate: monthDate); + return expenseDOs + .map((expenseDO) => Expense.from(expenseDO: expenseDO)) + .toList(); } Future deleteBy({required final int id}) async { - final expenseDO = await _expenseApi.getBy(id: id); - DateTime monthDate = - DateTime(expenseDO.createdOn.year, expenseDO.createdOn.month); - final aggregatedExpense = await _aggregatedExpenseApi.getByMonthAndTag( - monthDate: monthDate, tags: expenseDO.tags.split(',')); - - if (aggregatedExpense != null) { - await _aggregatedExpenseApi.update( - id: aggregatedExpense.id, - amount: aggregatedExpense.amount - expenseDO.amount, - createdOn: monthDate, - tags: expenseDO.tags.split(',')); - } - + final existingExpenseDO = await _expenseApi.getBy(id: id); + await _updateAggregatedExpense( + dateTime: existingExpenseDO.createdOn, + tags: existingExpenseDO.tags, + amount: -existingExpenseDO.amount); await _expenseApi.deleteBy(id: id); } Future deleteAggregatedExpense( {required final DateTime monthDate}) async { - return _expenseApi - .deleteByMonthDate(monthDate: monthDate) - .then((value) => - _aggregatedExpenseApi.deleteByMonthDate(monthDate: monthDate)) - .then((value) => null); + await _expenseApi.deleteByMonthDate(monthDate: monthDate); + await _aggregatedExpenseApi.deleteByMonthDate(monthDate: monthDate); + } + + Future> getAggregatedExpenses() async { + List aggregatedExpenseDOs = + await _aggregatedExpenseApi.get(); + return aggregatedExpenseDOs + .map((aggregatedExpenseDO) => + AggregatedExpense.from(expenseDO: aggregatedExpenseDO)) + .toList(); + } + + Future _updateAggregatedExpense( + {required final DateTime dateTime, + required final String tags, + required final double amount}) async { + final monthDate = DateTime(dateTime.year, dateTime.month); + final aggregatedExpense = await _aggregatedExpenseApi.getByMonthAndTag( + monthDate: monthDate, + tags: tags, + ); + + if (aggregatedExpense == null) { + await _aggregatedExpenseApi.create( + amount: amount, + monthDate: monthDate, + tags: tags, + ); + } else { + await _aggregatedExpenseApi.update( + id: aggregatedExpense.id, + amount: aggregatedExpense.amount + amount, + createdOn: monthDate, + tags: tags, + ); + } } - Future> getAggregatedExpenses() => - _aggregatedExpenseApi.get().then((aggregatedExpenseDOs) => - aggregatedExpenseDOs - .map((aggregatedExpenseDO) => - AggregatedExpense.from(expenseDO: aggregatedExpenseDO)) - .toList()); + String _processTags({required final List tags}) { + tags.sort(); + return tags.join(','); + } }