mirror of
https://github.com/Expand-sys/CCash
synced 2025-12-17 08:32:13 +11:00
⚡🔥 removed GEN_BODY and improved error responses of JSON parsing functions
This commit is contained in:
parent
b2f306aee5
commit
6f8475ad1d
1 changed files with 109 additions and 57 deletions
166
src/bank_api.cpp
166
src/bank_api.cpp
|
|
@ -6,14 +6,10 @@
|
||||||
|
|
||||||
#define CORS resp->addHeader("Access-Control-Allow-Origin", "*")
|
#define CORS resp->addHeader("Access-Control-Allow-Origin", "*")
|
||||||
|
|
||||||
#define GEN_BODY \
|
|
||||||
static thread_local const auto temp_req = req->getJsonObject(); \
|
|
||||||
static thread_local const auto body = temp_req ? *temp_req : Json::Value()
|
|
||||||
|
|
||||||
static thread_local ondemand::parser parser;
|
static thread_local ondemand::parser parser;
|
||||||
#define SIMD_JSON_GEN \
|
#define SIMD_JSON_GEN \
|
||||||
static thread_local simdjson::padded_string input(req->getBody()); \
|
static thread_local simdjson::padded_string input(req->getBody()); \
|
||||||
static thread_local ondemand::document doc = parser.iterate(input)
|
static thread_local auto doc = parser.iterate(input)
|
||||||
|
|
||||||
#define RESPONSE_PARSE(R) \
|
#define RESPONSE_PARSE(R) \
|
||||||
static thread_local auto resp = HttpResponse::newCustomHttpResponse(R); \
|
static thread_local auto resp = HttpResponse::newCustomHttpResponse(R); \
|
||||||
|
|
@ -21,7 +17,7 @@ static thread_local ondemand::parser parser;
|
||||||
callback(resp)
|
callback(resp)
|
||||||
|
|
||||||
#define RESPOND_TRUE \
|
#define RESPOND_TRUE \
|
||||||
static thread_local auto resp = HttpResponse::newCustomHttpResponse(BankResponse(k204NoContent, std::nullopt)); \
|
static thread_local auto resp = HttpResponse::newCustomHttpResponse(BankResponse{k204NoContent, std::nullopt}); \
|
||||||
CORS; \
|
CORS; \
|
||||||
CACHE_FOREVER; \
|
CACHE_FOREVER; \
|
||||||
callback(resp)
|
callback(resp)
|
||||||
|
|
@ -47,7 +43,7 @@ void api::GetLogs(req_args)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
static thread_local auto resp = HttpResponse::newCustomHttpResponse(BankResponse(k404NotFound, "\"Logs are Disabled\""));
|
static thread_local auto resp = HttpResponse::newCustomHttpResponse(BankResponse{k404NotFound, "\"Logs are Disabled\""});
|
||||||
CORS;
|
CORS;
|
||||||
CACHE_FOREVER;
|
CACHE_FOREVER;
|
||||||
callback(resp);
|
callback(resp);
|
||||||
|
|
@ -56,17 +52,24 @@ void api::GetLogs(req_args)
|
||||||
void api::SendFunds(req_args) const
|
void api::SendFunds(req_args) const
|
||||||
{
|
{
|
||||||
SIMD_JSON_GEN;
|
SIMD_JSON_GEN;
|
||||||
auto name = doc.find_field("name").get_string();
|
|
||||||
auto amount = doc.find_field("amount").get_uint64();
|
|
||||||
BankResponse res;
|
BankResponse res;
|
||||||
if (name.error() || amount.error())
|
if (doc.error())
|
||||||
{
|
{
|
||||||
res = BankResponse(k400BadRequest, "Invalid JSON");
|
res = BankResponse{k400BadRequest, "Invalid JSON"};
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
StrFromSV_Wrapper name_val(name.value());
|
auto name = doc.find_field("name").get_string();
|
||||||
res = bank.SendFunds(NAME_PARAM, name_val.str, amount.value());
|
auto amount = doc.find_field("amount").get_uint64();
|
||||||
|
if (name.error() || amount.error())
|
||||||
|
{
|
||||||
|
res = BankResponse{k400BadRequest, "Missing JSON arg(s)"};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
StrFromSV_Wrapper name_val(name.value());
|
||||||
|
res = bank.SendFunds(NAME_PARAM, name_val.str, amount.value());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
RESPONSE_PARSE(std::move(res));
|
RESPONSE_PARSE(std::move(res));
|
||||||
}
|
}
|
||||||
|
|
@ -76,68 +79,96 @@ void api::VerifyPassword(req_args) const { RESPOND_TRUE; }
|
||||||
void api::ChangePassword(req_args) const
|
void api::ChangePassword(req_args) const
|
||||||
{
|
{
|
||||||
SIMD_JSON_GEN;
|
SIMD_JSON_GEN;
|
||||||
auto pass = doc.find_field("pass").get_string();
|
|
||||||
BankResponse res;
|
BankResponse res;
|
||||||
if (pass.error())
|
if (doc.error())
|
||||||
{
|
{
|
||||||
res = BankResponse(k400BadRequest, "Invalid JSON");
|
res = BankResponse{k400BadRequest, "Invalid JSON"};
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
StrFromSV_Wrapper pass_val(pass.value());
|
auto pass = doc.find_field("pass").get_string();
|
||||||
bank.ChangePassword(NAME_PARAM, std::move(pass_val.str));
|
if (pass.error())
|
||||||
|
{
|
||||||
|
res = BankResponse{k400BadRequest, "Missing JSON arg(s)"};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
StrFromSV_Wrapper pass_val(pass.value());
|
||||||
|
bank.ChangePassword(NAME_PARAM, std::move(pass_val.str));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
RESPOND_TRUE;
|
RESPOND_TRUE;
|
||||||
}
|
}
|
||||||
void api::AdminChangePassword(req_args) const
|
void api::AdminChangePassword(req_args) const
|
||||||
{
|
{
|
||||||
SIMD_JSON_GEN;
|
SIMD_JSON_GEN;
|
||||||
auto name = doc.find_field("name").get_string();
|
|
||||||
auto pass = doc.find_field("pass").get_string();
|
|
||||||
BankResponse res;
|
BankResponse res;
|
||||||
if (name.error() || pass.error())
|
if (doc.error())
|
||||||
{
|
{
|
||||||
res = BankResponse(k400BadRequest, "Invalid JSON");
|
res = BankResponse{k400BadRequest, "Invalid JSON"};
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
StrFromSV_Wrapper name_val(name.value());
|
auto name = doc.find_field("name").get_string();
|
||||||
StrFromSV_Wrapper pass_val(pass.value());
|
auto pass = doc.find_field("pass").get_string();
|
||||||
bank.ChangePassword(name_val.str, std::move(pass_val.str));
|
if (name.error() || pass.error())
|
||||||
|
{
|
||||||
|
res = BankResponse{k400BadRequest, "Missing JSON arg(s)"};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
StrFromSV_Wrapper name_val(name.value());
|
||||||
|
StrFromSV_Wrapper pass_val(pass.value());
|
||||||
|
bank.ChangePassword(name_val.str, std::move(pass_val.str));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
RESPOND_TRUE;
|
RESPOND_TRUE;
|
||||||
}
|
}
|
||||||
void api::SetBal(req_args) const
|
void api::SetBal(req_args) const
|
||||||
{
|
{
|
||||||
SIMD_JSON_GEN;
|
SIMD_JSON_GEN;
|
||||||
auto name = doc.find_field("name").get_string();
|
|
||||||
auto amount = doc.find_field("amount").get_uint64();
|
|
||||||
BankResponse res;
|
BankResponse res;
|
||||||
if (name.error() || amount.error())
|
if (doc.error())
|
||||||
{
|
{
|
||||||
res = BankResponse(k400BadRequest, "Invalid JSON");
|
res = BankResponse{k400BadRequest, "Invalid JSON"};
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
StrFromSV_Wrapper name_val(name.value());
|
auto name = doc.find_field("name").get_string();
|
||||||
res = bank.SetBal(name_val.str, amount.value());
|
auto amount = doc.find_field("amount").get_uint64();
|
||||||
|
if (name.error() || amount.error())
|
||||||
|
{
|
||||||
|
res = BankResponse(k400BadRequest, "Invalid JSON");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
StrFromSV_Wrapper name_val(name.value());
|
||||||
|
res = bank.SetBal(name_val.str, amount.value());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
RESPONSE_PARSE(std::move(res));
|
RESPONSE_PARSE(std::move(res));
|
||||||
}
|
}
|
||||||
void api::ImpactBal(req_args) const
|
void api::ImpactBal(req_args) const
|
||||||
{
|
{
|
||||||
SIMD_JSON_GEN;
|
SIMD_JSON_GEN;
|
||||||
auto name = doc.find_field("name").get_string();
|
|
||||||
auto amount = doc.find_field("amount").get_int64();
|
|
||||||
BankResponse res;
|
BankResponse res;
|
||||||
if (name.error() || amount.error())
|
if (doc.error())
|
||||||
{
|
{
|
||||||
res = BankResponse(k400BadRequest, "Invalid JSON");
|
res = BankResponse{k400BadRequest, "Invalid JSON"};
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
StrFromSV_Wrapper name_val(name.value());
|
auto name = doc.find_field("name").get_string();
|
||||||
res = bank.ImpactBal(name_val.str, amount.value());
|
auto amount = doc.find_field("amount").get_int64();
|
||||||
|
if (name.error() || amount.error())
|
||||||
|
{
|
||||||
|
res = BankResponse(k400BadRequest, "Invalid JSON");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
StrFromSV_Wrapper name_val(name.value());
|
||||||
|
res = bank.ImpactBal(name_val.str, amount.value());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
RESPONSE_PARSE(std::move(res));
|
RESPONSE_PARSE(std::move(res));
|
||||||
}
|
}
|
||||||
|
|
@ -182,37 +213,51 @@ void api::ApiProperties(req_args) const
|
||||||
void api::AddUser(req_args) const
|
void api::AddUser(req_args) const
|
||||||
{
|
{
|
||||||
SIMD_JSON_GEN;
|
SIMD_JSON_GEN;
|
||||||
auto name = doc.find_field("name").get_string();
|
|
||||||
auto pass = doc.find_field("pass").get_string();
|
|
||||||
BankResponse res;
|
BankResponse res;
|
||||||
if (name.error() || pass.error())
|
if (doc.error())
|
||||||
{
|
{
|
||||||
res = BankResponse(k400BadRequest, "Invalid JSON");
|
res = BankResponse{k400BadRequest, "Invalid JSON"};
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
StrFromSV_Wrapper name_val(name.value());
|
auto name = doc.find_field("name").get_string();
|
||||||
StrFromSV_Wrapper pass_val(pass.value());
|
auto pass = doc.find_field("pass").get_string();
|
||||||
res = bank.AddUser(std::move(name_val.str), 0, std::move(pass_val.str));
|
if (name.error() || pass.error())
|
||||||
|
{
|
||||||
|
res = BankResponse(k400BadRequest, "Invalid JSON");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
StrFromSV_Wrapper name_val(name.value());
|
||||||
|
StrFromSV_Wrapper pass_val(pass.value());
|
||||||
|
res = bank.AddUser(std::move(name_val.str), 0, std::move(pass_val.str));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
RESPONSE_PARSE(std::move(res));
|
RESPONSE_PARSE(std::move(res));
|
||||||
}
|
}
|
||||||
void api::AdminAddUser(req_args) const
|
void api::AdminAddUser(req_args) const
|
||||||
{
|
{
|
||||||
SIMD_JSON_GEN;
|
SIMD_JSON_GEN;
|
||||||
auto name = doc.find_field("name").get_string();
|
|
||||||
auto amount = doc.find_field("amount").get_uint64();
|
|
||||||
auto pass = doc.find_field("pass").get_string();
|
|
||||||
BankResponse res;
|
BankResponse res;
|
||||||
if (name.error() || amount.error() || pass.error())
|
if (doc.error())
|
||||||
{
|
{
|
||||||
res = BankResponse(k400BadRequest, "Invalid JSON");
|
res = BankResponse{k400BadRequest, "Invalid JSON"};
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
StrFromSV_Wrapper name_val(name.value());
|
auto name = doc.find_field("name").get_string();
|
||||||
StrFromSV_Wrapper pass_val(pass.value());
|
auto amount = doc.find_field("amount").get_uint64();
|
||||||
res = bank.AddUser(std::move(name_val.str), amount.value(), std::move(pass_val.str));
|
auto pass = doc.find_field("pass").get_string();
|
||||||
|
if (name.error() || amount.error() || pass.error())
|
||||||
|
{
|
||||||
|
res = BankResponse(k400BadRequest, "Invalid JSON");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
StrFromSV_Wrapper name_val(name.value());
|
||||||
|
StrFromSV_Wrapper pass_val(pass.value());
|
||||||
|
res = bank.AddUser(std::move(name_val.str), amount.value(), std::move(pass_val.str));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
RESPONSE_PARSE(std::move(res));
|
RESPONSE_PARSE(std::move(res));
|
||||||
}
|
}
|
||||||
|
|
@ -223,16 +268,23 @@ void api::DelUser(req_args) const
|
||||||
void api::AdminDelUser(req_args) const
|
void api::AdminDelUser(req_args) const
|
||||||
{
|
{
|
||||||
SIMD_JSON_GEN;
|
SIMD_JSON_GEN;
|
||||||
auto name = doc.find_field("name").get_string();
|
|
||||||
BankResponse res;
|
BankResponse res;
|
||||||
if (name.error())
|
if (doc.error())
|
||||||
{
|
{
|
||||||
res = BankResponse(k400BadRequest, "Invalid JSON");
|
res = BankResponse{k400BadRequest, "Invalid JSON"};
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
StrFromSV_Wrapper name_val(name.value());
|
auto name = doc.find_field("name").get_string();
|
||||||
res = bank.DelUser(name_val.str);
|
if (name.error())
|
||||||
|
{
|
||||||
|
res = BankResponse(k400BadRequest, "Invalid JSON");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
StrFromSV_Wrapper name_val(name.value());
|
||||||
|
res = bank.DelUser(name_val.str);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
RESPONSE_PARSE(std::move(res));
|
RESPONSE_PARSE(std::move(res));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue