Skip to content

Commit

Permalink
fix expense month issue
Browse files Browse the repository at this point in the history
  • Loading branch information
praslnx8 committed Aug 8, 2024
1 parent 33d4f92 commit 3545705
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 109 deletions.
21 changes: 12 additions & 9 deletions lib/api/apis/aggregated_expense_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ class AggregatedExpenseApi {

Future<int> create(
{required final double amount,
required final DateTime monthDate,
required final int year,
required final int month,
required final String tags}) async {
return _db.into(_db.aggregatedExpenseTable).insert(
AggregatedExpenseTableCompanion.insert(
amount: amount, tags: tags, createdMonthDate: monthDate));
amount: amount, tags: tags, year: year, month: month));
}

Future<List<AggregatedExpenseDO>> get() async {
return (_db.select(_db.aggregatedExpenseTable)
..orderBy([(t) => OrderingTerm(expression: t.createdMonthDate)]))
..orderBy([(t) => OrderingTerm(expression: t.year)]))
.get();
}

Expand All @@ -29,24 +30,26 @@ class AggregatedExpenseApi {
}

Future<AggregatedExpenseDO?> getByMonthAndTag(
{required final DateTime monthDate, required final String tags}) async {
{required final int year, required final int month, required final String tags}) async {
return (_db.select(_db.aggregatedExpenseTable)
..where((t) =>
t.createdMonthDate.equals(monthDate) & t.tags.equals(tags)))
t.year.equals(year) & t.month.equals(month) & t.tags.equals(tags)))
.getSingleOrNull();
}

Future<int> update(
{required final int id,
required final double amount,
required final DateTime createdOn,
required final int year,
required final int month,
required final String tags}) async {
return (_db.update(_db.aggregatedExpenseTable)
..where((t) => t.id.equals(id)))
.write(AggregatedExpenseTableCompanion(
amount: Value(amount),
tags: Value(tags),
createdMonthDate: Value(createdOn)));
year: Value(year),
month: Value(month)));
}

Future<int> deleteBy({required final int id}) async {
Expand All @@ -55,9 +58,9 @@ class AggregatedExpenseApi {
.go();
}

Future<int> deleteByMonthDate({required final DateTime monthDate}) async {
Future<int> deleteByMonthDate({required final int year, required final int month}) async {
return (_db.delete(_db.aggregatedExpenseTable)
..where((t) => t.createdMonthDate.equals(monthDate)))
..where((t) => t.year.equals(year) & t.month.equals(month)))
.go();
}
}
12 changes: 6 additions & 6 deletions lib/api/apis/expense_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ class ExpenseApi {
}

Future<List<ExpenseDO>> getByMonth(
{required final DateTime monthDate}) async {
{required final int year, required final int month}) async {
return (_db.select(_db.expenseTable)
..where((t) =>
t.createdOn.year.equals(monthDate.year) &
t.createdOn.month.equals(monthDate.month))
t.createdOn.year.equals(year) &
t.createdOn.month.equals(month))
..orderBy([(t) => OrderingTerm(expression: t.createdOn)]))
.get();
}
Expand Down Expand Up @@ -57,11 +57,11 @@ class ExpenseApi {
return (_db.delete(_db.expenseTable)..where((t) => t.id.equals(id))).go();
}

Future<int> deleteByMonthDate({required final DateTime monthDate}) async {
Future<int> deleteByMonthDate({required final int year, required final int month}) async {
return (_db.delete(_db.expenseTable)
..where((t) =>
t.createdOn.year.equals(monthDate.year) &
t.createdOn.month.equals(monthDate.month)))
t.createdOn.year.equals(year) &
t.createdOn.month.equals(month)))
.go();
}
}
5 changes: 2 additions & 3 deletions lib/api/db/app_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,10 @@ class AggregatedExpenseTable extends Table {
IntColumn get id => integer().named('ID').autoIncrement()();

RealColumn get amount => real().named('AMOUNT')();

TextColumn get tags => text().named('TAGS')();

DateTimeColumn get createdMonthDate =>
dateTime().named('CREATED_MONTH_DATE')();
IntColumn get month => integer().named('MONTH')();
IntColumn get year => integer().named('YEAR')();
}

@DataClassName('ExpenseTagDO')
Expand Down
104 changes: 66 additions & 38 deletions lib/api/db/app_database.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions lib/domain/models/aggregated_expense.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ import 'package:wealth_wave/api/db/app_database.dart';
class AggregatedExpense {
final double amount;
final List<String> tags;
final DateTime createdMonthDate;
final int year;
final int month;

AggregatedExpense._({
required this.amount,
required this.createdMonthDate,
required this.year,
required this.month,
required this.tags,
});

factory AggregatedExpense.from({required AggregatedExpenseDO expenseDO}) =>
AggregatedExpense._(
amount: expenseDO.amount,
createdMonthDate: expenseDO.createdMonthDate,
year: expenseDO.year,
month: expenseDO.month,
tags: expenseDO.tags.split(','));
}
20 changes: 20 additions & 0 deletions lib/domain/models/month.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Month {
final int year;
final int month;

Month({required this.year, required this.month});

int getValue() {
return year * 100 + month;
}

@override
bool operator ==(Object other) {
if (identical(this, other)) return true;

return other is Month && other.year == year && other.month == month;
}

@override
int get hashCode => year.hashCode ^ month.hashCode;
}
Loading

0 comments on commit 3545705

Please sign in to comment.