Skip to content

Commit

Permalink
save commit
Browse files Browse the repository at this point in the history
  • Loading branch information
praslnx8 committed Jan 17, 2024
1 parent 7057fff commit d759df8
Show file tree
Hide file tree
Showing 12 changed files with 335 additions and 83 deletions.
21 changes: 12 additions & 9 deletions lib/api/apis/goal_investment_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@ class GoalInvestmentApi {
throw Exception('Invalid getBy call');
}

Future<GoalInvestmentDO> getById({required final int id}) async {
return (_db.select(_db.goalInvestmentTable)..where((t) => t.id.equals(id)))
.getSingle();
}

Future<int> update(
{required final int id,
required final int goalId,
{required final int goalId,
required final int investmentId,
required final double split}) async {
return (_db.update(_db.goalInvestmentTable)..where((t) => t.id.equals(id)))
return (_db.update(_db.goalInvestmentTable)
..where((t) =>
t.goalId.equals(goalId) & t.investmentId.equals(investmentId)))
.write(GoalInvestmentTableCompanion(
investmentId: Value(investmentId),
goalId: Value(goalId),
Expand All @@ -47,13 +53,10 @@ class GoalInvestmentApi {
return (_db.delete(_db.goalInvestmentTable)
..where((t) => t.id.equals(id)))
.go();
} else if (goalId != null) {
return (_db.delete(_db.goalInvestmentTable)
..where((t) => t.goalId.equals(goalId)))
.go();
} else if (investmentId != null) {
} else if (goalId != null && investmentId != null) {
return (_db.delete(_db.goalInvestmentTable)
..where((t) => t.investmentId.equals(investmentId)))
..where((t) =>
t.goalId.equals(goalId) & t.investmentId.equals(investmentId)))
.go();
}
throw Exception('Invalid deleteBy call');
Expand Down
4 changes: 2 additions & 2 deletions lib/api/apis/sip_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class SipApi {
required final String? description,
required final double amount,
required final DateTime startDate,
required final DateTime endDate,
required final DateTime? endDate,
required final SipFrequency frequency}) async {
return _db.into(_db.sipTable).insert(SipTableCompanion.insert(
investmentId: investmentId,
Expand Down Expand Up @@ -48,7 +48,7 @@ class SipApi {
required final String? description,
required final double amount,
required final DateTime startDate,
required final DateTime endDate,
required final DateTime? endDate,
required final SipFrequency frequency}) async {
return (_db.update(_db.sipTable)..where((t) => t.id.equals(id))).write(
SipTableCompanion(
Expand Down
9 changes: 8 additions & 1 deletion lib/api/apis/transaction_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,25 @@ class TransactionApi {
throw Exception('Invalid getBy call');
}

Future<TransactionDO> getById({required final int id}) async {
return (_db.select(_db.transactionTable)..where((t) => t.id.equals(id)))
.getSingle();
}

Future<int> update(
{required final int id,
required final int investmentId,
required final String? description,
required final double amount,
required final DateTime createdOn}) async {
required final DateTime createdOn,
final int? sipId}) async {
return (_db.update(_db.transactionTable)..where((t) => t.id.equals(id)))
.write(TransactionTableCompanion(
investmentId: Value(investmentId),
description: Value(description),
amount: Value(amount),
createdOn: Value(createdOn),
sipId: Value(sipId),
));
}

Expand Down
51 changes: 51 additions & 0 deletions lib/domain/models/goal.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:ffi';

import 'package:wealth_wave/api/apis/goal_investment_api.dart';
import 'package:wealth_wave/api/apis/investment_api.dart';
import 'package:wealth_wave/api/db/app_database.dart';
Expand Down Expand Up @@ -44,6 +46,55 @@ class Goal {
.then((entries) => Map.fromEntries(entries));
}

Future<double> getInvestedAmount() async {
return _goalInvestmentApi
.getBy(goalId: id)
.then((goalInvestments) => Future.wait(goalInvestments.map(
(goalInvestment) => _investmentApi
.getById(id: goalInvestment.investmentId)
.then((investmentDO) =>
Investment.from(investmentDO: investmentDO))
.then((investment) => investment.getTotalInvestedAmount())
.then((amount) => amount * goalInvestment.split))))
.then((amounts) => amounts.reduce((value, element) => value + element));
}

Future<double> getValueOnMaturity() async {
return _goalInvestmentApi
.getBy(goalId: id)
.then((goalInvestments) => Future.wait(goalInvestments.map(
(goalInvestment) => _investmentApi
.getById(id: goalInvestment.investmentId)
.then((investmentDO) =>
Investment.from(investmentDO: investmentDO))
.then((investment) => investment.getValueOn(
date: maturityDate, considerFuturePayments: true))
.then((amount) => amount * goalInvestment.split))))
.then((amounts) => amounts.reduce((value, element) => value + element));
}

Future<MapEntry<Investment, double>> tagInvestment(
{required final Investment investment,
required final double split}) async {
return _goalInvestmentApi
.create(goalId: id, investmentId: investment.id, split: split)
.then((goalInvestmentDO) => MapEntry(investment, split));
}

Future<MapEntry<Investment, double>> updateTaggedInvestment(
{required final Investment investment,
required final double split}) async {
return _goalInvestmentApi
.update(goalId: id, investmentId: investment.id, split: split)
.then((goalInvestmentDO) => MapEntry(investment, split));
}

Future<void> deleteTaggedInvestment({required final Investment investment}) {
return _goalInvestmentApi
.deleteBy(goalId: id, investmentId: investment.id)
.then((count) => Void);
}

static Goal from({required final GoalDO goalDO}) {
return Goal(
id: goalDO.id,
Expand Down
101 changes: 101 additions & 0 deletions lib/domain/models/investment.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import 'dart:ffi';

import 'package:wealth_wave/api/apis/basket_api.dart';
import 'package:wealth_wave/api/apis/goal_api.dart';
import 'package:wealth_wave/api/apis/goal_investment_api.dart';
import 'package:wealth_wave/api/apis/sip_api.dart';
import 'package:wealth_wave/api/apis/transaction_api.dart';
import 'package:wealth_wave/api/db/app_database.dart';
import 'package:wealth_wave/contract/risk_level.dart';
import 'package:wealth_wave/contract/sip_frequency.dart';
import 'package:wealth_wave/domain/models/basket.dart';
import 'package:wealth_wave/domain/models/goal.dart';
import 'package:wealth_wave/domain/models/irr_calculator.dart';
Expand Down Expand Up @@ -127,6 +130,104 @@ class Investment {
.then((sips) => sips.map((sipDO) => SIP.from(sipDO: sipDO)).toList());
}

Future<SIP> createSip(
{required final String description,
required final double amount,
required final DateTime startDate,
required final DateTime? endDate,
required final SipFrequency frequency}) async {
return _sipApi
.create(
investmentId: id,
description: description,
amount: amount,
startDate: startDate,
endDate: endDate,
frequency: frequency)
.then((id) => _sipApi.getById(id: id))
.then((sipDO) => SIP.from(sipDO: sipDO));
}

Future<SIP> updateSip(
{required final int sipId,
required final String description,
required final double amount,
required final DateTime startDate,
required final DateTime? endDate,
required final SipFrequency frequency}) async {
return _sipApi
.update(
id: sipId,
investmentId: id,
description: description,
amount: amount,
startDate: startDate,
endDate: endDate,
frequency: frequency)
.then((count) => _sipApi.getById(id: sipId))
.then((sipDO) => SIP.from(sipDO: sipDO));
}

Future<void> deleteSIP({required final int sipId}) async {
return _sipApi.delete(id: sipId).then((count) => Void);
}

Future<Transaction> createTransaction(
{required final String description,
required final double amount,
required final DateTime createdOn}) async {
return _transactionApi
.create(
investmentId: id,
description: description,
amount: amount,
createdOn: createdOn)
.then((id) => _transactionApi.getById(id: id))
.then(
(transactionDO) => Transaction.from(transactionDO: transactionDO));
}

Future<Transaction> updateTransactions(
{required final int transactionId,
required final String description,
required final double amount,
required final DateTime createdOn}) async {
return _transactionApi
.update(
id: transactionId,
investmentId: id,
description: description,
amount: amount,
createdOn: createdOn)
.then((count) => _transactionApi.getById(id: transactionId))
.then(
(transactionDO) => Transaction.from(transactionDO: transactionDO));
}

Future<void> deleteTransaction({required final int transactionId}) async {
return _transactionApi.deleteBy(id: transactionId).then((count) => Void);
}

Future<MapEntry<Goal, double>> tagGoal(
{required final Goal goal, required final double split}) async {
return _goalInvestmentApi
.create(goalId: goal.id, investmentId: id, split: split)
.then((goalInvestmentDO) => MapEntry(goal, split));
}

Future<MapEntry<Goal, double>> updateTaggedGoal(
{required final Goal goal, required final double split}) async {
return _goalInvestmentApi
.update(goalId: goal.id, investmentId: id, split: split)
.then((goalInvestmentDO) => MapEntry(goal, split));
}

Future<void> deleteTaggedGoal({required final Goal goal}) {
return _goalInvestmentApi
.deleteBy(goalId: goal.id, investmentId: id)
.then((count) => Void);
}

static Investment from({required final InvestmentDO investmentDO}) {
return Investment(
id: investmentDO.id,
Expand Down
62 changes: 60 additions & 2 deletions lib/domain/models/sip.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import 'dart:ffi';

import 'package:wealth_wave/api/apis/investment_api.dart';
import 'package:wealth_wave/api/apis/transaction_api.dart';
import 'package:wealth_wave/api/db/app_database.dart';
import 'package:wealth_wave/contract/sip_frequency.dart';
import 'package:wealth_wave/domain/models/investment.dart';
import 'package:wealth_wave/domain/models/payment.dart';
import 'package:wealth_wave/domain/models/transaction.dart';

class SIP {
final int id;
final int investmentId;
final String? description;
final double amount;
final DateTime startDate;
Expand All @@ -14,17 +19,21 @@ class SIP {
final DateTime? executedTill;

final TransactionApi _transactionApi;
final InvestmentApi _investmentApi;

SIP(
{required this.id,
required this.investmentId,
required this.description,
required this.amount,
required this.startDate,
required this.endDate,
required this.frequency,
required this.executedTill,
final TransactionApi? transactionApi})
: _transactionApi = transactionApi ?? TransactionApi();
final TransactionApi? transactionApi,
final InvestmentApi? investmentApi})
: _transactionApi = transactionApi ?? TransactionApi(),
_investmentApi = investmentApi ?? InvestmentApi();

Future<List<Payment>> getFuturePayment({required final DateTime till}) {
return Future(() {
Expand All @@ -44,9 +53,58 @@ class SIP {
.toList());
}

Future<Investment> getInvestment() {
return _investmentApi
.getById(id: investmentId)
.then((investmentDO) => Investment.from(investmentDO: investmentDO));
}

Future<Transaction> createTransaction(
{required final String description,
required final double amount,
required final DateTime createdOn}) async {
return _transactionApi
.create(
investmentId: investmentId,
sipId: id,
description: description,
amount: amount,
createdOn: createdOn)
.then((id) => _transactionApi.getById(id: id))
.then(
(transactionDO) => Transaction.from(transactionDO: transactionDO));
}

Future<Transaction> updateTransactions(
{required final int transactionId,
required final String description,
required final double amount,
required final DateTime createdOn}) async {
return _transactionApi
.update(
id: transactionId,
investmentId: investmentId,
sipId: id,
description: description,
amount: amount,
createdOn: createdOn)
.then((count) => _transactionApi.getById(id: transactionId))
.then(
(transactionDO) => Transaction.from(transactionDO: transactionDO));
}

Future<void> deleteTransaction({required final int transactionId}) async {
return _transactionApi.deleteBy(id: transactionId).then((count) => Void);
}

Future<void> deleteTransactions() async {
return _transactionApi.deleteBy(sipId: id).then((count) => Void);
}

static SIP from({required final SipDO sipDO}) {
return SIP(
id: sipDO.id,
investmentId: sipDO.investmentId,
description: sipDO.description,
amount: sipDO.amount,
startDate: sipDO.startDate,
Expand Down
16 changes: 16 additions & 0 deletions lib/domain/services/backup_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:wealth_wave/api/apis/backup_api.dart';

class BackupService {
final BackupApi _backupApi;

BackupService({final BackupApi? backupApi})
: _backupApi = backupApi ?? BackupApi();

Future<void> import() async {
return _backupApi.import();
}

Future<void> export() async {
return _backupApi.export();
}
}
Loading

0 comments on commit d759df8

Please sign in to comment.