Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
praslnx8 committed Jul 3, 2024
1 parent 6725fc8 commit 6d135b3
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 110 deletions.
19 changes: 6 additions & 13 deletions lib/api/apis/aggregated_expense_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ class AggregatedExpenseApi {
Future<int> create(
{required final double amount,
required final DateTime monthDate,
required final List<String> 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<List<AggregatedExpenseDO>> get() async {
Expand All @@ -29,25 +29,23 @@ class AggregatedExpenseApi {
}

Future<AggregatedExpenseDO?> getByMonthAndTag(
{required final DateTime monthDate,
required final List<String> 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();
}

Future<int> update(
{required final int id,
required final double amount,
required final DateTime createdOn,
required final List<String> 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)));
}

Expand All @@ -62,9 +60,4 @@ class AggregatedExpenseApi {
..where((t) => t.createdMonthDate.equals(monthDate)))
.go();
}

String processTag(final List<String> tags) {
tags.sort();
return tags.join(',');
}
}
15 changes: 9 additions & 6 deletions lib/api/apis/expense_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ class ExpenseApi {
{required final double amount,
required final String? description,
required final DateTime createdOn,
required final List<String> 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));
}

Expand Down Expand Up @@ -44,12 +44,12 @@ class ExpenseApi {
required final double amount,
required final String? description,
required final DateTime createdOn,
required final List<String> 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)));
}

Expand All @@ -58,7 +58,10 @@ class ExpenseApi {
}

Future<int> 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();
}
}
169 changes: 78 additions & 91 deletions lib/domain/services/expense_service.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -19,127 +20,113 @@ class ExpenseService {
: _expenseApi = expenseApi ?? ExpenseApi(),
_aggregatedExpenseApi = aggregatedExpenseApi ?? AggregatedExpenseApi();

Future<Expense> createExpense({
Future<void> createExpense({
required final String? description,
required final double amount,
required final List<String> 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<Expense> updateExpense({
Future<void> updateExpense({
required final int id,
required final String? description,
required final double amount,
required final List<String> 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<Expense> getById({required final int id}) => _expenseApi
.getBy(id: id)
.then((expenseDO) => Expense.from(expenseDO: expenseDO));
Future<Expense> getById({required final int id}) async {
ExpenseDO expenseDO = await _expenseApi.getBy(id: id);
return Expense.from(expenseDO: expenseDO);
}

Future<List<Expense>> 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<ExpenseDO> expenseDOs =
await _expenseApi.getByMonth(monthDate: monthDate);
return expenseDOs
.map((expenseDO) => Expense.from(expenseDO: expenseDO))
.toList();
}

Future<void> 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<void> 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<List<AggregatedExpense>> getAggregatedExpenses() async {
List<AggregatedExpenseDO> aggregatedExpenseDOs =
await _aggregatedExpenseApi.get();
return aggregatedExpenseDOs
.map((aggregatedExpenseDO) =>
AggregatedExpense.from(expenseDO: aggregatedExpenseDO))
.toList();
}

Future<void> _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<List<AggregatedExpense>> getAggregatedExpenses() =>
_aggregatedExpenseApi.get().then((aggregatedExpenseDOs) =>
aggregatedExpenseDOs
.map((aggregatedExpenseDO) =>
AggregatedExpense.from(expenseDO: aggregatedExpenseDO))
.toList());
String _processTags({required final List<String> tags}) {
tags.sort();
return tags.join(',');
}
}

0 comments on commit 6d135b3

Please sign in to comment.