🐎 improved SetBal()

This commit is contained in:
EntireTwix 2021-06-27 18:10:38 -07:00
parent 1a434c6bcb
commit 1aa450e112
2 changed files with 11 additions and 14 deletions

View file

@ -52,9 +52,10 @@ public:
BankResponse GetBal(const std::string &name) const noexcept;
BankResponse GetLogs(const std::string &name) noexcept;
BankResponse SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount) noexcept;
bool VerifyPassword(const std::string &name, const std::string &attempt) const noexcept; //internally used
bool VerifyPassword(const std::string &name, const std::string &attempt) const noexcept;
void ChangePassword(const std::string &name, std::string &&new_pass) noexcept;
BankResponse SetBal(const std::string &name, uint32_t amount) noexcept;
int_fast8_t AddUser(const std::string &name, const std::string &init_pass) noexcept;
int_fast8_t AdminAddUser(const std::string &attempt, std::string &&name, uint32_t init_bal, std::string &&init_pass) noexcept;
@ -63,9 +64,7 @@ public:
int_fast8_t AdminDelUser(const std::string &name, const std::string &attempt) noexcept;
bool Contains(const std::string &name) const noexcept;
bool AdminVerifyPass(const std::string &attempt) noexcept; //interall used
BankResponse SetBal(const std::string &name, uint32_t amount) noexcept;
bool AdminVerifyPass(const std::string &attempt) noexcept;
void Save();
void Load();

View file

@ -129,6 +129,14 @@ void Bank::ChangePassword(const std::string &name, std::string &&new_pass) noexc
u.password = XXH3_64bits(new_pass.data(), new_pass.size());
});
}
BankResponse Bank::SetBal(const std::string &name, uint32_t amount) noexcept
{
return users.modify_if(name, [amount](User &u) {
u.balance = amount;
})
? BankResponse(k200OK, "Balance set!")
: BankResponse(k404NotFound, "User not found");
}
int_fast8_t Bank::AddUser(const std::string &name, const std::string &init_pass) noexcept
{
@ -247,16 +255,6 @@ bool Bank::AdminVerifyPass(const std::string &attempt) noexcept
return (admin_pass == attempt);
}
BankResponse Bank::SetBal(const std::string &name, uint32_t amount) noexcept
{
BankResponse res = {k404NotFound, "User not found"};
users.modify_if(name, [&res, amount](User &u) {
u.balance = amount;
res = {k200OK, "Balance set!"};
});
return res;
}
void Bank::Save()
{
#if CONSERVATIVE_DISK_SAVE