SetBal()

This commit is contained in:
EntireTwix 2021-06-27 18:05:33 -07:00
parent deae9fab7c
commit 1a434c6bcb
5 changed files with 21 additions and 24 deletions

View file

@ -66,7 +66,7 @@ int main(int argc, char **argv)
bank.admin_pass = "root";
Op_a(bank.AddUser("", ""), "add user: ", 1000000, bank.DelUser("", ""));
Op_a(bank.AdminAddUser("root", "", 0, ""), "admin add user: ", 1000000, bank.DelUser("", ""));
Op(bank.SetBal("twix", "root", 1000000), "set bal: ", 1000000);
Op(bank.SetBal("twix", 1000000), "set bal: ", 1000000);
Op(bank.SendFunds("twix", "jolly", 1), "send funds: ", 1000000);
bank.AddUser("", "");

View file

@ -65,7 +65,7 @@ public:
bool Contains(const std::string &name) const noexcept;
bool AdminVerifyPass(const std::string &attempt) noexcept; //interall used
int_fast8_t SetBal(const std::string &name, const std::string &attempt, uint32_t amount) noexcept;
BankResponse SetBal(const std::string &name, uint32_t amount) noexcept;
void Save();
void Load();

View file

@ -20,6 +20,7 @@ public:
void VerifyPassword(req_args) const;
void ChangePassword(req_args) const;
void SetBal(req_args) const;
void Help(req_args) const;
void Ping(req_args) const;
@ -29,7 +30,6 @@ public:
void DelUser(req_args, const std::string &name) const;
void AdminDelUser(req_args, const std::string &name) const;
void Contains(req_args, const std::string &name) const;
void SetBal(req_args, const std::string &name, uint32_t amount) const;
void AdminVerifyPass(req_args);
METHOD_LIST_BEGIN
@ -42,7 +42,7 @@ public:
//Meta Usage
METHOD_ADD(api::ChangePassword, "/v1/user/change_password", Patch, Options, "UserFilter"); //done
METHOD_ADD(api::SetBal, "/admin/{name}/bal?amount={amount}", Patch, Options);
METHOD_ADD(api::SetBal, "/v1/admin/bal", Patch, Options, "AdminFilter"); //done
//System Usage
METHOD_ADD(api::Help, "/help", Get, Options);

View file

@ -247,18 +247,14 @@ bool Bank::AdminVerifyPass(const std::string &attempt) noexcept
return (admin_pass == attempt);
}
int_fast8_t Bank::SetBal(const std::string &name, const std::string &attempt, uint32_t amount) noexcept
BankResponse Bank::SetBal(const std::string &name, uint32_t amount) noexcept
{
if (admin_pass != attempt)
{
return ErrorResponse::WrongPassword;
}
return (users.modify_if(name, [amount](User &u) {
BankResponse res = {k404NotFound, "User not found"};
users.modify_if(name, [&res, amount](User &u) {
u.balance = amount;
}))
? true
: ErrorResponse::UserNotFound;
res = {k200OK, "Balance set!"};
});
return res;
}
void Bank::Save()

View file

@ -19,7 +19,7 @@
resp->setExpiredTime(0); \
callback(resp);
#define NAME_PARAM req->getBody()
#define NAME_PARAM req->getBody().data()
template <typename T>
constexpr Json::Value JsonCast(T &&val)
@ -56,7 +56,7 @@ void api::GetLog(req_args)
{
if constexpr (max_log_size > 0)
{
RESPONSE_PARSE(bank.GetLogs(NAME_PARAM.data()));
RESPONSE_PARSE(bank.GetLogs(NAME_PARAM));
}
else
{
@ -69,18 +69,23 @@ void api::GetLog(req_args)
void api::SendFunds(req_args) const
{
GEN_BODY
RESPONSE_PARSE(bank.SendFunds(NAME_PARAM.data(), body["to"].asCString(), body["amount"].asUInt()));
RESPONSE_PARSE(bank.SendFunds(NAME_PARAM, body["to"].asCString(), body["amount"].asUInt()));
}
void api::VerifyPassword(req_args) const
{
RESPOND_TRUE
RESPOND_TRUE //as we know the user exists and is verified
}
void api::ChangePassword(req_args) const
{
GEN_BODY
bank.ChangePassword(NAME_PARAM.data(), std::move(body["new_pass"].asCString())); //may make asString()
RESPOND_TRUE
bank.ChangePassword(NAME_PARAM, std::move(body["new_pass"].asCString())); //may make asString()
RESPOND_TRUE //as we know the user exists and is verified
}
void api::SetBal(req_args) const
{
GEN_BODY
RESPONSE_PARSE(bank.SetBal(NAME_PARAM, body["amount"].asUInt()));
}
void api::Help(req_args) const
@ -133,10 +138,6 @@ void api::Contains(req_args, const std::string &name) const
{
JSON(bank.Contains(name));
}
void api::SetBal(req_args, const std::string &name, uint32_t amount) const
{
JSON(bank.SetBal(name, PASS_HEADER, amount));
}
void api::AdminVerifyPass(req_args)
{
JSON(bank.AdminVerifyPass(PASS_HEADER));