-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassets_pages.cpp
More file actions
95 lines (68 loc) · 3.03 KB
/
assets_pages.cpp
File metadata and controls
95 lines (68 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//=======================================================================
// Copyright (c) 2013-2020 Baptiste Wicht.
// Distributed under the terms of the MIT License.
// (See accompanying file LICENSE or copy at
// http://opensource.org/licenses/MIT)
//=======================================================================
#include "assets.hpp"
#include "pages/html_writer.hpp"
#include "pages/assets_pages.hpp"
#include "http.hpp"
using namespace budget;
namespace {
void add_currency_picker(budget::writer& w, std::string_view default_value = "") {
add_text_picker(w, "Currency", "input_currency", default_value);
}
void add_portfolio_picker(budget::writer& w, bool portfolio) {
add_yes_no_picker(w, "Part of the portfolio", "input_portfolio", portfolio);
}
void add_active_picker(budget::writer& w, bool active) {
add_yes_no_picker(w, "Active?", "input_active", active);
}
void add_share_based_picker(budget::writer& w, bool share_based) {
add_yes_no_picker(w, "Using shares?", "input_share_based", share_based);
}
} // namespace
void budget::assets_page(html_writer& w) {
budget::show_assets(w);
make_tables_sortable(w);
}
void budget::add_assets_page(html_writer& w) {
w << title_begin << "New asset" << title_end;
form_begin(w, "/api/assets/add/", "/assets/add/");
add_name_picker(w);
for (auto& clas : all_asset_classes()) {
add_money_picker(w, clas.name + " (%)", std::format("input_class_{}", clas.id), "");
}
add_currency_picker(w);
add_portfolio_picker(w, false);
add_money_picker(w, "Percent of portfolio (%)", "input_alloc", "", false);
add_share_based_picker(w, false);
add_text_picker(w, "Ticker", "input_ticker", "", false);
add_active_picker(w, true);
form_end(w);
}
void budget::edit_assets_page(html_writer& w, const httplib::Request& req) {
if (!req.has_param("input_id") || !req.has_param("back_page")) {
return display_error_message(w, "Invalid parameter for the request");
}
auto input_id = req.get_param_value("input_id");
if (!asset_exists(budget::to_number<size_t>(input_id))) {
return display_error_message(w, "The asset {} does not exist", input_id);
}
auto back_page = req.get_param_value("back_page");
w << title_begin << "Edit asset " << input_id << title_end;
form_begin_edit(w, "/api/assets/edit/", back_page, input_id);
auto asset = get_asset(budget::to_number<size_t>(input_id));
add_name_picker(w, asset.name);
for (auto& clas : all_asset_classes()) {
add_money_picker(w, clas.name + " (%)", std::format("input_class_{}", clas.id), budget::money_to_string(get_asset_class_allocation(asset, clas)));
}
add_currency_picker(w, asset.currency);
add_portfolio_picker(w, asset.portfolio);
add_money_picker(w, "Percent of portfolio (%)", "input_alloc", budget::money_to_string(asset.portfolio_alloc));
add_share_based_picker(w, asset.share_based);
add_text_picker(w, "Ticker", "input_ticker", asset.ticker, false);
add_active_picker(w, asset.active);
form_end(w);
}