Skip to content

Commit

Permalink
Add SIP
Browse files Browse the repository at this point in the history
  • Loading branch information
praslnx8 committed Jan 6, 2024
1 parent fa39a71 commit 6df456d
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 1 deletion.
13 changes: 13 additions & 0 deletions lib/api/apis/investment_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ class InvestmentApi {
}
}

Future<List<SipDO>> getSips({final int? investmentId}) async {
if (investmentId == null) {
return (_db.select(_db.sipTable)
..orderBy([(t) => OrderingTerm.desc(t.startDate)]))
.get();
} else {
return (_db.select(_db.sipTable)
..where((t) => t.investmentId.equals(investmentId))
..orderBy([(t) => OrderingTerm.desc(t.startDate)]))
.get();
}
}

Future<int> createInvestment({
required final String name,
required final String? description,
Expand Down
21 changes: 21 additions & 0 deletions lib/api/db/app_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,24 @@ class TransactionTable extends Table {
dateTime().named('AMOUNT_INVESTED_ON')();
}

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

IntColumn get investmentId =>
integer().named('INVESTMENT_ID').references(InvestmentTable, #id)();

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

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

DateTimeColumn get startDate => dateTime().named('START_DATE')();

DateTimeColumn get endDate => dateTime().named('END_DATE')();

RealColumn get frequency => real().named('FREQUENCY')();
}

@DataClassName('GoalDO')
class GoalTable extends Table {
IntColumn get id => integer().named('ID').autoIncrement()();
Expand Down Expand Up @@ -142,6 +160,7 @@ abstract class GoalInvestmentEnrichedMappingView extends View {
InvestmentTable,
TransactionTable,
GoalTable,
SipTable,
GoalInvestmentTable,
], views: [
InvestmentEnrichedView,
Expand Down Expand Up @@ -169,13 +188,15 @@ class AppDatabase extends _$AppDatabase {
final goalBackup = await executor.runSelect('SELECT * FROM goal_table', []);
final goalInvestmentBackup =
await executor.runSelect('SELECT * FROM goal_investment_table', []);
final sipBackup = await executor.runSelect('SELECT * FROM sip_table', []);

return {
'basket_table': basketBackup,
'investment_table': investmentBackup,
'transaction_table': transactionBackup,
'goal_table': goalBackup,
'goal_investment_table': goalInvestmentBackup,
'sip_table': sipBackup,
};
}

Expand Down
146 changes: 145 additions & 1 deletion lib/api/db/app_database.g.dart

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

5 changes: 5 additions & 0 deletions lib/domain/models/investment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:math';
import 'package:wealth_wave/api/db/app_database.dart';
import 'package:wealth_wave/contract/risk_level.dart';
import 'package:wealth_wave/domain/irr_calculator.dart';
import 'package:wealth_wave/domain/models/sip.dart';
import 'package:wealth_wave/domain/models/transaction.dart';

class Investment {
Expand All @@ -17,6 +18,7 @@ class Investment {
final double totalInvestedAmount;
final int totalTransactions;
final List<Transaction> transactions;
final List<SIP> sips;
final List<GoalInvestmentEnrichedMappingDO> taggedGoals;

Investment(
Expand All @@ -31,6 +33,7 @@ class Investment {
required this.totalInvestedAmount,
required this.totalTransactions,
required this.transactions,
required this.sips,
required this.taggedGoals});

double? getIrr() {
Expand All @@ -52,6 +55,7 @@ class Investment {
static Investment from(
{required final InvestmentEnrichedDO investment,
required final List<TransactionDO> transactions,
required final List<SipDO> sips,
required final List<GoalInvestmentEnrichedMappingDO>
goalInvestmentMappings}) {
return Investment(
Expand All @@ -68,6 +72,7 @@ class Investment {
transactions: transactions
.map((transaction) => Transaction.from(transaction: transaction))
.toList(),
sips: sips.map((sip) => SIP.from(sip: sip)).toList(),
taggedGoals: goalInvestmentMappings);
}
}
28 changes: 28 additions & 0 deletions lib/domain/models/sip.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import 'package:wealth_wave/api/db/app_database.dart';

class SIP {
final int id;
final String? description;
final double amount;
final DateTime startDate;
final DateTime endDate;
final double frequency;

SIP(
{required this.id,
required this.description,
required this.amount,
required this.startDate,
required this.endDate,
required this.frequency});

static SIP from({required final SipDO sip}) {
return SIP(
id: sip.id,
description: sip.description,
amount: sip.amount,
startDate: sip.startDate,
endDate: sip.endDate,
frequency: sip.frequency);
}
}
4 changes: 4 additions & 0 deletions lib/domain/usecases/fetch_investments_use_case.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ class FetchInvestmentsUseCase {
List<InvestmentEnrichedDO> investments =
await _investmentApi.getEnrichedInvestments();
List<TransactionDO> transactions = await _investmentApi.getTransactions();
List<SipDO> sips = await _investmentApi.getSips();
List<GoalInvestmentEnrichedMappingDO> goalInvestmentMappings =
await _investmentApi.getGoalInvestmentMappings();

return investments
.map((investment) => Investment.from(
investment: investment,
sips: sips
.where((sip) => sip.investmentId == investment.id)
.toList(),
transactions: transactions
.where(
(transaction) => transaction.investmentId == investment.id)
Expand Down

0 comments on commit 6df456d

Please sign in to comment.