Skip to content

Commit

Permalink
added expense and aggregated expense service
Browse files Browse the repository at this point in the history
  • Loading branch information
praslnx8 committed Jul 2, 2024
1 parent 832b260 commit 4da6a12
Show file tree
Hide file tree
Showing 9 changed files with 357 additions and 119 deletions.
12 changes: 9 additions & 3 deletions lib/api/apis/aggregated_expense_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class AggregatedExpenseApi {
required final List<String> tags}) async {
return _db.into(_db.aggregatedExpenseTable).insert(
AggregatedExpenseTableCompanion.insert(
amount: Value(amount),
tags: Value(tags.join(',')),
createdMonthDate: Value(monthDate)));
amount: amount,
tags: tags.join(','),
createdMonthDate: monthDate));
}

Future<List<AggregatedExpenseDO>> get() async {
Expand All @@ -30,6 +30,12 @@ class AggregatedExpenseApi {
.getSingle();
}

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

Future<int> update(
{required final int id,
required final double amount,
Expand Down
19 changes: 14 additions & 5 deletions lib/api/apis/expense_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class ExpenseApi {
required final DateTime createdOn,
required final List<String> tags}) async {
return _db.into(_db.expenseTable).insert(ExpenseTableCompanion.insert(
amount: Value(amount),
amount: amount,
description: Value(description),
tags: Value(tags.join(',')),
createdOn: Value(createdOn)));
tags: tags.join(','),
createdOn: createdOn));
}

Future<List<ExpenseDO>> get() async {
Expand All @@ -24,6 +24,16 @@ class ExpenseApi {
.get();
}

Future<List<ExpenseDO>> getByMonth(
{required final DateTime monthDate}) async {
return (_db.select(_db.expenseTable)
..where((t) =>
t.createdOn.year.equals(monthDate.year) &
t.createdOn.month.equals(monthDate.month))
..orderBy([(t) => OrderingTerm(expression: t.createdOn)]))
.get();
}

Future<ExpenseDO> getBy({required final int id}) async {
return (_db.select(_db.expenseTable)..where((t) => t.id.equals(id)))
.getSingle();
Expand All @@ -44,7 +54,6 @@ class ExpenseApi {
}

Future<int> deleteBy({required final int id}) async {
return (_db.delete(_db.expenseTable)..where((t) => t.id.equals(id)))
.go();
return (_db.delete(_db.expenseTable)..where((t) => t.id.equals(id))).go();
}
}
12 changes: 6 additions & 6 deletions lib/api/db/app_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -260,24 +260,24 @@ class ExpenseTable extends Table {

TextColumn get description => text().nullable().named('DESCRIPTION')();

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

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

DateTimeColumn get createdOn =>
dateTime().nullable().named('CREATED_ON')();
dateTime().named('CREATED_ON')();
}

@DataClassName('AggregatedExpenseDO')
class AggregatedExpenseTable extends Table {
IntColumn get id => integer().named('ID').autoIncrement()();

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

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

DateTimeColumn get createdMonthDate =>
dateTime().nullable().named('CREATED_MONTH_DATE')();
dateTime().named('CREATED_MONTH_DATE')();
}

@DataClassName('ExpenseTagDO')
Expand Down
Loading

0 comments on commit 4da6a12

Please sign in to comment.