Skip to content

Commit c8dea72

Browse files
committed
New simple REST API
1 parent 800f134 commit c8dea72

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

include/api/retirement_api.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//=======================================================================
2+
// Copyright (c) 2013-2020 Baptiste Wicht.
3+
// Distributed under the terms of the MIT License.
4+
// (See accompanying file LICENSE or copy at
5+
// http://opensource.org/licenses/MIT)
6+
//=======================================================================
7+
8+
#pragma once
9+
10+
namespace httplib {
11+
struct Request;
12+
struct Response;
13+
}; // namespace httplib
14+
15+
namespace budget {
16+
17+
void retirement_countdown_api(const httplib::Request& req, httplib::Response& res);
18+
19+
} // end of namespace budget

src/api/retirement_api.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//=======================================================================
2+
// Copyright (c) 2013-2020 Baptiste Wicht.
3+
// Distributed under the terms of the MIT License.
4+
// (See accompanying file LICENSE or copy at
5+
// http://opensource.org/licenses/MIT)
6+
//=======================================================================
7+
8+
#include "api/server_api.hpp"
9+
#include "api/retirement_api.hpp"
10+
11+
#include "assets.hpp"
12+
#include "liabilities.hpp"
13+
#include "accounts.hpp"
14+
#include "data.hpp"
15+
#include "data_cache.hpp"
16+
#include "retirement.hpp"
17+
18+
#include "pages/web_config.hpp"
19+
20+
using namespace budget;
21+
22+
void budget::retirement_countdown_api(const httplib::Request& req, httplib::Response& res) {
23+
if (auto fi_expenses = budget::get_fi_expenses()){
24+
data_cache cache;
25+
26+
const auto nw = get_fi_net_worth(cache);
27+
const auto savings_rate = running_savings_rate(cache);
28+
const auto income = running_income(cache);
29+
30+
const auto currency = get_default_currency();
31+
const auto wrate = to_number<double>(internal_config_value("withdrawal_rate"));
32+
const auto roi = to_number<double>(internal_config_value("expected_roi"));
33+
const auto years = double(100.0 / wrate);
34+
const auto fi_goal = years * fi_expenses;
35+
36+
date_type fi_base_months = 0;
37+
{
38+
auto current_nw = nw;
39+
while (current_nw < fi_goal) {
40+
current_nw *= 1.0 + (roi / 100.0) / 12;
41+
current_nw += (savings_rate * income) / 12;
42+
43+
++fi_base_months;
44+
}
45+
}
46+
47+
if (nw > fi_goal) {
48+
api_success_content(req, res, "You are FI!");
49+
} else {
50+
// TODO Add days granularity
51+
52+
const auto missing_years = unsigned(fi_base_months / 12);
53+
const auto missing_months = unsigned(fi_base_months - (missing_years * 12));
54+
55+
auto result = std::format("{} years, {} months", missing_years, missing_months);
56+
57+
api_success_content(req, res, result);
58+
}
59+
} else {
60+
api_success_content(req, res, "No expenses");
61+
}
62+
}

src/api/server_api.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
// http://opensource.org/licenses/MIT)
66
//=======================================================================
77

8-
#include <set>
9-
108
#include "api/server_api.hpp"
119
#include "api/earnings_api.hpp"
1210
#include "api/expenses_api.hpp"
@@ -19,6 +17,7 @@
1917
#include "api/fortunes_api.hpp"
2018
#include "api/assets_api.hpp"
2119
#include "api/user_api.hpp"
20+
#include "api/retirement_api.hpp"
2221

2322
#include "pages/server_pages.hpp"
2423

@@ -195,6 +194,8 @@ void budget::load_api(httplib::Server& server) {
195194
server.Get("/api/assets/delete/", api_wrapper(&delete_assets_api));
196195
server.Get("/api/assets/list/", api_wrapper(&list_assets_api));
197196

197+
server.Get("/api/retirement/countdown/", api_wrapper(&retirement_countdown_api));
198+
198199
server.Post("/api/asset_values/add/", api_wrapper(&add_asset_values_api));
199200
server.Post("/api/asset_values/edit/", api_wrapper(&edit_asset_values_api));
200201
server.Post("/api/asset_values/batch/", api_wrapper(&batch_asset_values_api));

0 commit comments

Comments
 (0)