From 5e996b27059c44fed3369784d4d77d658846981b Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Mon, 9 Jan 2023 16:43:54 -0800 Subject: [PATCH] :sparkles: `ImpactBal` and `SetBal` add a log now --- include/bank.h | 2 +- src/bank.cpp | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/include/bank.h b/include/bank.h index 4cb8340..952497f 100644 --- a/include/bank.h +++ b/include/bank.h @@ -55,7 +55,7 @@ public: static bool VerifyPassword(const std::string &name, const std::string_view &attempt) noexcept; static void ChangePassword(const std::string &name, const std::string &new_pass) noexcept; - static BankResponse SetBal(const std::string &name, uint32_t amount) noexcept; + static BankResponse SetBal(const std::string &name, int64_t amount) noexcept; static BankResponse ImpactBal(const std::string &name, int64_t amount) noexcept; static bool Contains(const std::string &name) noexcept; diff --git a/src/bank.cpp b/src/bank.cpp index ebbd40b..ad6dd46 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -185,9 +185,15 @@ void Bank::ChangePassword(const std::string &name, const std::string &new_pass) SET_CHANGES_ON; Bank::users.modify_if(name, [&new_pass](User &u) { u.password = xxHashStringGen{}(new_pass); }); } -BankResponse Bank::SetBal(const std::string &name, uint32_t amount) noexcept +BankResponse Bank::SetBal(const std::string &name, int64_t amount) noexcept { - if (ValidUsername(name) && Bank::users.modify_if(name, [amount](User &u) { u.balance = amount; })) + if (ValidUsername(name) && Bank::users.modify_if(name, [&amount](User &u) { + amount -= u.balance; + u.balance -= amount; +#if MAX_LOG_SIZE > 0 + u.log.AddTrans(nullptr, (amount > 0), amount, time(NULL)); +#endif + })) { SET_CHANGES_ON; return {k204NoContent, std::nullopt}; //returns new balance @@ -204,7 +210,13 @@ BankResponse Bank::ImpactBal(const std::string &name, int64_t amount) noexcept return {k400BadRequest, "\"Amount cannot be 0\""}; } uint32_t bal; - if (ValidUsername(name) && Bank::users.modify_if(name, [&bal, amount](User &u) { bal = (u.balance < (amount * -1) ? u.balance = 0 : u.balance += amount); })) + if (ValidUsername(name) && Bank::users.modify_if(name, [&bal, &amount](User &u) { + amount += (u.balance < (amount * -1)) * (amount + u.balance); + bal = u.balance -= amount; +#if MAX_LOG_SIZE > 0 + u.log.AddTrans(nullptr, (amount > 0), amount, time(NULL)); +#endif + })) { SET_CHANGES_ON; return {k200OK, std::to_string(bal)}; //may return new balance