-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccounts_api.cpp
More file actions
95 lines (69 loc) · 3.15 KB
/
accounts_api.cpp
File metadata and controls
95 lines (69 loc) · 3.15 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 <set>
#include "api/server_api.hpp"
#include "api/accounts_api.hpp"
#include "accounts.hpp"
#include "guid.hpp"
#include "http.hpp"
#include "data.hpp"
using namespace budget;
void budget::add_accounts_api(const httplib::Request& req, httplib::Response& res) {
if (!parameters_present(req, {"input_name", "input_amount"})) {
return api_error(req, res, "Invalid parameters");
}
account account;
account.guid = budget::generate_guid();
account.name = req.get_param_value("input_name");
account.amount = budget::money_from_string(req.get_param_value("input_amount"));
account.since = find_new_since();
account.until = budget::date(2099, 12, 31);
auto id = add_account(std::move(account));
api_success(req, res, "Account " + to_string(id) + " has been created", to_string(id));
}
void budget::edit_accounts_api(const httplib::Request& req, httplib::Response& res) {
if (!parameters_present(req, {"input_id", "input_name", "input_amount"})) {
return api_error(req, res, "Invalid parameters");
}
auto id = req.get_param_value("input_id");
if (!budget::account_exists(budget::to_number<size_t>(id))) {
return api_error(req, res, "account " + id + " does not exist");
}
account account = get_account(budget::to_number<size_t>(id));
account.name = req.get_param_value("input_name");
account.amount = budget::money_from_string(req.get_param_value("input_amount"));
edit_account(account);
api_success(req, res, "Account " + to_string(account.id) + " has been modified");
}
void budget::delete_accounts_api(const httplib::Request& req, httplib::Response& res) {
if (!parameters_present(req, {"input_id"})) {
return api_error(req, res, "Invalid parameters");
}
auto id = req.get_param_value("input_id");
if (!budget::account_exists(budget::to_number<size_t>(id))) {
return api_error(req, res, "The account " + id + " does not exit");
}
budget::account_delete(budget::to_number<size_t>(id));
api_success(req, res, "Account " + id + " has been deleted");
}
void budget::list_accounts_api(const httplib::Request& req, httplib::Response& res) {
std::stringstream ss;
for (auto& account : all_accounts()) {
data_writer writer;
account.save(writer);
ss << writer.to_string() << std::endl;
}
api_success_content(req, res, ss.str());
}
void budget::archive_accounts_month_api(const httplib::Request& req, httplib::Response& res) {
budget::archive_accounts_impl(true);
api_success(req, res, "Accounts have been migrated from the beginning of the month");
}
void budget::archive_accounts_year_api(const httplib::Request& req, httplib::Response& res) {
budget::archive_accounts_impl(false);
api_success(req, res, "Accounts have been migrated from the beginning of the year");
}