diff --git a/include/user.h b/include/user.h index db095c7..dcb0040 100644 --- a/include/user.h +++ b/include/user.h @@ -16,9 +16,9 @@ struct User User(uint32_t init_bal, XXH64_hash_t init_pass) noexcept; #if MAX_LOG_SIZE > 0 User(uint32_t init_bal, XXH64_hash_t init_pass, const Json::Value &log_j) noexcept; +#endif User(const bank_dom::User &u) noexcept; bank_dom::User Encode() const noexcept; -#endif Json::Value Serialize() const; //to be removed later }; diff --git a/src/bank.cpp b/src/bank.cpp index 6875834..0469ece 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -92,22 +92,25 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam std::shared_lock lock{iter_lock}; #if MAX_LOG_SIZE > 0 time_t current_time = time(NULL); + if (!users.modify_if(a_name, [current_time, &a_name, &b_name, &res, amount](User &a) +#else + if (!users.modify_if(a_name, [&a_name, &b_name, &res, amount](User &a) #endif - if (!users.modify_if(a_name, [current_time, &a_name, &b_name, &res, amount](User &a) { - //if A can afford it - if (a.balance < amount) - { - res = {k400BadRequest, "\"Insufficient funds\""}; - } - else - { - a.balance -= amount; + { + //if A can afford it + if (a.balance < amount) + { + res = {k400BadRequest, "\"Insufficient funds\""}; + } + else + { + a.balance -= amount; #if MAX_LOG_SIZE > 0 - a.log.AddTrans(a_name, b_name, amount, current_time); + a.log.AddTrans(a_name, b_name, amount, current_time); #endif - res = {k200OK, std::to_string(a.balance)}; - } - })) + res = {k200OK, std::to_string(a.balance)}; + } + })) { return {k404NotFound, "\"Sender does not exist\""}; } @@ -201,7 +204,11 @@ BankResponse Bank::PruneUsers(time_t threshold_time, uint32_t threshold_bal) noe for (const auto &u : users) { users.erase_if(u.first, [threshold_time, threshold_bal, &deleted_count](User &u) -> bool { - if (u.log.data.back().time < threshold_time && u.balance < threshold_bal) +#if MAX_LOG_SIZE > 0 + if (u.data.back().time < threshold_time && u.balance < threshold_bal) +#else + if (u.balance < threshold_bal) +#endif { return ++deleted_count; } diff --git a/src/bank_api.cpp b/src/bank_api.cpp index e06ae74..6e14566 100644 --- a/src/bank_api.cpp +++ b/src/bank_api.cpp @@ -37,17 +37,14 @@ void api::GetBal(req_args, const std::string &name) const } void api::GetLogs(req_args) { - if constexpr (MAX_LOG_SIZE > 0) - { - RESPONSE_PARSE(bank.GetLogs(NAME_PARAM)); - } - else - { - static thread_local auto resp = HttpResponse::newCustomHttpResponse(BankResponse{k404NotFound, "\"Logs are Disabled\""}); - CORS; - CACHE_FOREVER; - callback(resp); - } +#if MAX_LOG_SIZE > 0 + RESPONSE_PARSE(bank.GetLogs(NAME_PARAM)); +#else + static thread_local auto resp = HttpResponse::newCustomHttpResponse(BankResponse{k404NotFound, "\"Logs are Disabled\""}); + CORS; + CACHE_FOREVER; + callback(resp); +#endif } void api::SendFunds(req_args) const { diff --git a/src/user.cpp b/src/user.cpp index e6d56ef..b93be89 100644 --- a/src/user.cpp +++ b/src/user.cpp @@ -16,9 +16,9 @@ User::User(uint32_t init_bal, const std::string &init_pass) noexcept : balance(i */ User::User(uint32_t init_bal, XXH64_hash_t init_pass) noexcept : balance(init_bal), password(init_pass) {} -#if MAX_LOG_SIZE > 0 User::User(const bank_dom::User &u) noexcept : balance(u.balance), password(u.password) { +#if MAX_LOG_SIZE > 0 if (u.logs) { for (uint32_t i = (u.logs.value().data.size() - MAX_LOG_SIZE); i < u.logs.value().data.size(); ++i) @@ -27,8 +27,8 @@ User::User(const bank_dom::User &u) noexcept : balance(u.balance), password(u.pa log.data.emplace_front(temp.from, temp.to, temp.amount, temp.time); } } -} #endif +} bank_dom::User User::Encode() const noexcept { #if MAX_LOG_SIZE > 0