diff --git a/include/user.h b/include/user.h index 88e28fc..6d9dbfa 100644 --- a/include/user.h +++ b/include/user.h @@ -15,7 +15,9 @@ struct User User(const std::string &init_pass); User(uint32_t init_bal, const std::string &init_pass); User(uint32_t init_bal, uint64_t init_pass); +#if MAX_LOG_SIZE > 0 User(uint32_t init_bal, uint64_t init_pass, const Json::Value &log_j); +#endif Json::Value Serialize() const; //to be removed later }; diff --git a/src/bank.cpp b/src/bank.cpp index 7d2b05c..024ccb9 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -15,12 +15,14 @@ BankResponse Bank::GetBal(const std::string &name) const noexcept BankResponse Bank::GetLogs(const std::string &name) noexcept { BankResponse res; +#if MAX_LOG_SIZE > 0 if (!users.modify_if(name, [&res](User &u) { res = {k200OK, u.log.GetLog()}; })) { return BankResponse(k404NotFound, "User not found"); } +#endif return res; } BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount) noexcept @@ -44,42 +46,13 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam } BankResponse state; - if constexpr (MAX_LOG_SIZE > 0) - { - Transaction temp(a_name, b_name, amount); - std::shared_lock lock{send_funds_l}; - if (!users.modify_if(a_name, [&temp, &state, amount](User &a) { - //if A can afford it - if (a.balance < amount) - { - state = {k402PaymentRequired, "Sender has insufficient funds"}; - } - else - { - a.balance -= amount; - a.log.AddTrans(Transaction(temp)); - state = {k200OK, "Transfer successful!"}; - } - })) - { - return {k404NotFound, "Sender does not exist"}; - } - if (state.first == k200OK) - { - users.modify_if(b_name, [&temp, amount](User &b) { - b.balance += amount; - b.log.AddTrans(std::move(temp)); - }); -#if CONSERVATIVE_DISK_SAVE - save_flag.SetChangesOn(); + std::shared_lock lock{send_funds_l}; +#if MAX_LOG_SIZE > 0 + Transaction temp(a_name, b_name, amount); + if (!users.modify_if(a_name, [&temp, &state, amount](User &a) { +#else + if (!users.modify_if(a_name, [&state, amount](User &a) { #endif - } - return state; - } - else - { - std::shared_lock lock{send_funds_l}; - users.modify_if(a_name, [this, &state, amount](User &a) { //if A can afford it if (a.balance < amount) { @@ -88,20 +61,30 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam else { a.balance -= amount; +#if MAX_LOG_SIZE > 0 + a.log.AddTrans(Transaction(temp)); +#endif state = {k200OK, "Transfer successful!"}; } - }); - if (state.first == k200OK) - { - users.modify_if(b_name, [&a_name, &b_name, amount](User &b) { - b.balance += amount; - }); -#if CONSERVATIVE_DISK_SAVE - save_flag.SetChangesOn(); -#endif - } - return state; + })) + { + return {k404NotFound, "Sender does not exist"}; } + if (state.first == k200OK) + { +#if MAX_LOG_SIZE > 0 + users.modify_if(b_name, [&temp, amount](User &b) { + b.balance += amount; + b.log.AddTrans(std::move(temp)); }); +#else + users.modify_if(b_name, [amount](User &b) { b.balance += amount; }); +#endif + +#if CONSERVATIVE_DISK_SAVE + save_flag.SetChangesOn(); +#endif + } + return state; } bool Bank::VerifyPassword(const std::string &name, const std::string &attempt) const noexcept { diff --git a/src/user.cpp b/src/user.cpp index 3e13465..dae4fb9 100644 --- a/src/user.cpp +++ b/src/user.cpp @@ -22,6 +22,7 @@ User::User(uint32_t init_bal, const std::string &init_pass) : balance(init_bal), * @param init_pass */ User::User(uint32_t init_bal, uint64_t init_pass) : balance(init_bal), password(init_pass) {} +#if MAX_LOG_SIZE > 0 User::User(uint32_t init_bal, uint64_t init_pass, const Json::Value &log_j) : balance(init_bal), password(init_pass) { if (log_j.size()) @@ -41,15 +42,15 @@ User::User(uint32_t init_bal, uint64_t init_pass, const Json::Value &log_j) : ba } } } +#endif Json::Value User::Serialize() const { Json::Value res; res["balance"] = (Json::UInt)balance; res["password"] = (Json::UInt64)password; - if constexpr (MAX_LOG_SIZE > 0) - { - res["log"] = log.Serialize(); - } +#if MAX_LOG_SIZE > 0 + res["log"] = log.Serialize(); +#endif return res; } \ No newline at end of file