Skip to content

Commit

Permalink
added boot strap operation
Browse files Browse the repository at this point in the history
  • Loading branch information
praslnx8 committed Jan 18, 2024
1 parent a1d674b commit d976f40
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 22 deletions.
6 changes: 6 additions & 0 deletions lib/api/apis/sip_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ class SipApi {
executedTill: const Value(null)));
}

Future<List<SipDO>> getAll() async {
return (_db.select(_db.sipTable)
..orderBy([(t) => OrderingTerm.desc(t.startDate)]))
.get();
}

Future<List<SipDO>> getBy({final int? investmentId}) async {
if (investmentId == null) {
return (_db.select(_db.sipTable)
Expand Down
14 changes: 9 additions & 5 deletions lib/domain/models/investment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,8 @@ class Investment {
}

Future<List<SIP>> getSips() async {
return _sipApi
.getBy(investmentId: id)
.then((sips) => sips.map((sipDO) => SIP.from(sipDO: sipDO)).toList());
return _sipApi.getBy(investmentId: id).then(
(sips) => Future.wait(sips.map((sipDO) => SIP.from(sipDO: sipDO))));
}

Future<SIP> createSip(
Expand All @@ -142,7 +141,8 @@ class Investment {
endDate: endDate,
frequency: frequency)
.then((id) => _sipApi.getById(id: id))
.then((sipDO) => SIP.from(sipDO: sipDO));
.then((sipDO) => SIP.from(sipDO: sipDO))
.then((sip) => sip.performSipTransactions().then((_) => sip));
}

Future<SIP> updateSip(
Expand All @@ -162,7 +162,11 @@ class Investment {
endDate: endDate,
frequency: frequency)
.then((count) => _sipApi.getById(id: sipId))
.then((sipDO) => SIP.from(sipDO: sipDO));
.then((sipDO) => SIP.from(sipDO: sipDO))
.then((sip) => sip
.deleteTransactions()
.then((_) => sip.performSipTransactions().then((_) => sip)));
;
}

Future<void> deleteSIP({required final int sipId}) async {
Expand Down
12 changes: 10 additions & 2 deletions lib/domain/models/sip.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class SIP {
Future<List<Payment>> getFuturePayment({required final DateTime till}) {
return Future(() {
List<Payment> payments = [];
for (var i = startDate;
for (var i = executedTill ?? startDate;
i.isBefore(endDate ?? till);
i = i.add(getDuration(frequency))) {
payments.add(Payment(amount: amount, createdOn: i));
Expand Down Expand Up @@ -99,7 +99,15 @@ class SIP {
return _transactionApi.deleteBy(sipId: id).then((count) => {});
}

static SIP from({required final SipDO sipDO}) {
Future<void> performSipTransactions() {
return getFuturePayment(till: DateTime.now()).then((payments) =>
Future.wait(payments.map((payment) => createTransaction(
description: 'SIP',
amount: payment.amount,
createdOn: payment.createdOn))));
}

static Future<SIP> from({required final SipDO sipDO}) async {
return SIP(
id: sipDO.id,
investmentId: sipDO.investmentId,
Expand Down
12 changes: 12 additions & 0 deletions lib/domain/services/boot_strap_service.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:wealth_wave/domain/services/sip_service.dart';

class BootStrapService {
final SipService _sipService;

BootStrapService({final SipService? sipService})
: _sipService = sipService ?? SipService();

Future<void> performBootStrapOperations() {
return _sipService.performSipTransactions();
}
}
9 changes: 9 additions & 0 deletions lib/domain/services/sip_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,13 @@ class SipService {
return SIP.from(sipDO: sipDO);
});
}

Future<void> performSipTransactions() {
return _sipApi
.getAll()
.then((sipDOs) =>
Future.wait(sipDOs.map((sipDO) => SIP.from(sipDO: sipDO))))
.then((sips) => sips.map((sip) => sip.performSipTransactions()))
.then((_) => {});
}
}
33 changes: 18 additions & 15 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:wealth_wave/api/db/app_database.dart';
import 'package:wealth_wave/app_router.dart';
import 'package:wealth_wave/domain/services/boot_strap_service.dart';

void main() {
runApp(Provider<AppDatabase>(
Expand All @@ -16,20 +17,22 @@ class WealthWaveApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Wealth Wave',
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
onGenerateRoute: (settings) {
return MaterialPageRoute(
settings: RouteSettings(
name: settings.name, arguments: settings.arguments),
maintainState: true,
builder: (context) => AppRouter.route(settings.name!));
},
);
return FutureBuilder(
future: BootStrapService().performBootStrapOperations(),
builder: (context, snapshot) => MaterialApp(
title: 'Wealth Wave',
debugShowCheckedModeBanner: false,
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
onGenerateRoute: (settings) {
return MaterialPageRoute(
settings: RouteSettings(
name: settings.name, arguments: settings.arguments),
maintainState: true,
builder: (context) => AppRouter.route(settings.name!));
},
));
}
}

0 comments on commit d976f40

Please sign in to comment.