🐛🐎 noexcept and fixed DelUser

This commit is contained in:
EntireTwix 2021-07-03 17:43:46 -07:00
parent 7c0b4f48bd
commit b90552d0d4

View file

@ -1,21 +1,24 @@
#include "bank_api.h"
#define CACHE_FOREVER resp->setExpiredTime(0)
#define CORS resp->addHeader("Access-Control-Allow-Origin", "*")
#define GEN_BODY \
const auto temp_req = req->getJsonObject(); \
const auto body = temp_req ? *temp_req : Json::Value();
#define RESPONSE_PARSE(R) \
const auto r = R; \
const auto r(R); \
auto resp = HttpResponse::newHttpJsonResponse(JsonCast(std::move(r.second))); \
resp->setStatusCode(r.first); \
resp->addHeader("Access-Control-Allow-Origin", "*"); \
CORS; \
callback(resp);
#define RESPOND_TRUE \
auto resp = HttpResponse::newHttpJsonResponse(JsonCast(true)); \
resp->setStatusCode(k200OK); \
resp->setExpiredTime(0); \
resp->addHeader("Access-Control-Allow-Origin", "*"); \
CACHE_FOREVER; \
CORS; \
callback(resp);
#define NAME_PARAM req->getBody().data()
@ -41,18 +44,16 @@ constexpr Json::Value JsonCast(T &&val)
}
}
api::api(Bank &b) : bank(b)
{
}
api::api(Bank &b) noexcept : bank(b) {}
#if API_VERSION >= 1
//Usage
void api::GetBal(req_args, const std::string &name) const
void api::GetBal(req_args, const std::string &name) const noexcept
{
RESPONSE_PARSE(bank.GetBal(name));
}
void api::GetLog(req_args)
void api::GetLog(req_args) noexcept
{
if constexpr (MAX_LOG_SIZE > 0)
{
@ -62,8 +63,8 @@ void api::GetLog(req_args)
{
auto resp = HttpResponse::newHttpJsonResponse("Logs are Disabled");
resp->setStatusCode(k404NotFound);
resp->addHeader("Access-Control-Allow-Origin", "*");
resp->setExpiredTime(0); //cached forever
CORS;
CACHE_FOREVER;
callback(resp);
}
}
@ -72,17 +73,14 @@ void api::SendFunds(req_args) const
GEN_BODY
RESPONSE_PARSE(bank.SendFunds(NAME_PARAM, body["to"].asCString(), body["amount"].asUInt()));
}
void api::VerifyPassword(req_args) const
{
RESPOND_TRUE //as we know the user exists and is verified
}
void api::VerifyPassword(req_args) const noexcept { RESPOND_TRUE }
//Meta Usage
void api::ChangePassword(req_args) const
{
GEN_BODY
bank.ChangePassword(NAME_PARAM, std::move(body["new_pass"].asCString()));
RESPOND_TRUE //as we know the user exists and is verified
RESPOND_TRUE
}
void api::AdminChangePassword(req_args) const
{
@ -97,45 +95,45 @@ void api::SetBal(req_args) const
}
//System Usage
void api::Help(req_args) const
void api::Help(req_args) const noexcept
{
auto resp = HttpResponse::newHttpResponse();
resp->setBody(""); //will be filled in with docs
resp->addHeader("Access-Control-Allow-Origin", "*");
resp->setExpiredTime(0);
CORS;
CACHE_FOREVER;
callback(resp);
}
void api::Ping(req_args) const
void api::Ping(req_args) const noexcept
{
auto resp = HttpResponse::newHttpResponse();
resp->setBody("pong");
resp->addHeader("Access-Control-Allow-Origin", "*");
resp->setExpiredTime(0);
CORS;
CACHE_FOREVER;
callback(resp);
}
void api::Close(req_args) const
void api::Close(req_args) const noexcept
{
bank.Save();
app().quit();
RESPOND_TRUE //filter handles admin creds
}
void api::Contains(req_args, const std::string &name) const
void api::Contains(req_args, const std::string &name) const noexcept
{
auto resp = HttpResponse::newHttpJsonResponse(JsonCast(bank.Contains(name)));
resp->setStatusCode(k200OK);
resp->addHeader("Access-Control-Allow-Origin", "*");
CORS;
callback(resp);
}
void api::AdminVerifyAccount(req_args)
void api::AdminVerifyAccount(req_args) const noexcept
{
RESPOND_TRUE //filter handles admin creds
}
void api::ApiVersion(req_args) const
void api::ApiVersion(req_args) const noexcept
{
auto resp = HttpResponse::newHttpJsonResponse(API_VERSION);
resp->setStatusCode(k200OK);
resp->addHeader("Access-Control-Allow-Origin", "*");
resp->setExpiredTime(0); //cached forever
CORS;
CACHE_FOREVER;
callback(resp);
}
@ -151,7 +149,8 @@ void api::AdminAddUser(req_args) const
}
void api::DelUser(req_args) const
{
RESPONSE_PARSE(bank.DelUser(NAME_PARAM))
GEN_BODY
RESPONSE_PARSE(bank.DelUser(body["name"].asCString()))
}
void api::AdminDelUser(req_args) const
{