mirror of
https://github.com/Expand-sys/CCash
synced 2025-12-17 00:22:14 +11:00
🎨 stylistic
This commit is contained in:
parent
14b1bff364
commit
c99e542111
1 changed files with 26 additions and 46 deletions
72
src/bank.cpp
72
src/bank.cpp
|
|
@ -50,8 +50,7 @@ size_t Bank::SumBal() const noexcept
|
||||||
BankResponse Bank::GetBal(const std::string &name) const noexcept
|
BankResponse Bank::GetBal(const std::string &name) const noexcept
|
||||||
{
|
{
|
||||||
uint32_t res = 0;
|
uint32_t res = 0;
|
||||||
if (!ValidUsername(name) || !users.if_contains(name, [&res](const User &u)
|
if (!ValidUsername(name) || !users.if_contains(name, [&res](const User &u) { res = u.balance; }))
|
||||||
{ res = u.balance; }))
|
|
||||||
{
|
{
|
||||||
return {k404NotFound, "\"User not found\""};
|
return {k404NotFound, "\"User not found\""};
|
||||||
}
|
}
|
||||||
|
|
@ -64,8 +63,7 @@ BankResponse Bank::GetBal(const std::string &name) const noexcept
|
||||||
BankResponse Bank::GetLogs(const std::string &name) noexcept
|
BankResponse Bank::GetLogs(const std::string &name) noexcept
|
||||||
{
|
{
|
||||||
BankResponse res;
|
BankResponse res;
|
||||||
if (!users.modify_if(name, [&res](User &u)
|
if (!users.modify_if(name, [&res](User &u) { res = {k200OK, u.log.GetLogs()}; }))
|
||||||
{ res = {k200OK, u.log.GetLogs()}; }))
|
|
||||||
{
|
{
|
||||||
return {k404NotFound, "\"User not found\""};
|
return {k404NotFound, "\"User not found\""};
|
||||||
}
|
}
|
||||||
|
|
@ -95,36 +93,33 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
|
||||||
#if MAX_LOG_SIZE > 0
|
#if MAX_LOG_SIZE > 0
|
||||||
time_t current_time = time(NULL);
|
time_t current_time = time(NULL);
|
||||||
#endif
|
#endif
|
||||||
if (!users.modify_if(a_name, [current_time, &a_name, &b_name, &res, amount](User &a)
|
if (!users.modify_if(a_name, [current_time, &a_name, &b_name, &res, amount](User &a) {
|
||||||
{
|
//if A can afford it
|
||||||
//if A can afford it
|
if (a.balance < amount)
|
||||||
if (a.balance < amount)
|
{
|
||||||
{
|
res = {k400BadRequest, "\"Insufficient funds\""};
|
||||||
res = {k400BadRequest, "\"Insufficient funds\""};
|
}
|
||||||
}
|
else
|
||||||
else
|
{
|
||||||
{
|
a.balance -= amount;
|
||||||
a.balance -= amount;
|
|
||||||
#if MAX_LOG_SIZE > 0
|
#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
|
#endif
|
||||||
res = {k200OK, std::to_string(a.balance)};
|
res = {k200OK, std::to_string(a.balance)};
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
{
|
{
|
||||||
return {k404NotFound, "\"Sender does not exist\""};
|
return {k404NotFound, "\"Sender does not exist\""};
|
||||||
}
|
}
|
||||||
if (res.first == k200OK)
|
if (res.first == k200OK)
|
||||||
{
|
{
|
||||||
#if MAX_LOG_SIZE > 0
|
#if MAX_LOG_SIZE > 0
|
||||||
users.modify_if(b_name, [current_time, &a_name, &b_name, amount](User &b)
|
users.modify_if(b_name, [current_time, &a_name, &b_name, amount](User &b) {
|
||||||
{
|
b.balance += amount;
|
||||||
b.balance += amount;
|
b.log.AddTrans(a_name, b_name, amount, current_time);
|
||||||
b.log.AddTrans(a_name, b_name, amount, current_time);
|
});
|
||||||
});
|
|
||||||
#else
|
#else
|
||||||
users.modify_if(b_name, [amount](User &b)
|
users.modify_if(b_name, [amount](User &b) { b.balance += amount; });
|
||||||
{ b.balance += amount; });
|
|
||||||
#endif
|
#endif
|
||||||
#if CONSERVATIVE_DISK_SAVE
|
#if CONSERVATIVE_DISK_SAVE
|
||||||
#if MULTI_THREADED
|
#if MULTI_THREADED
|
||||||
|
|
@ -139,8 +134,7 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
|
||||||
bool Bank::VerifyPassword(const std::string &name, const std::string_view &attempt) const noexcept
|
bool Bank::VerifyPassword(const std::string &name, const std::string_view &attempt) const noexcept
|
||||||
{
|
{
|
||||||
bool res = false;
|
bool res = false;
|
||||||
users.if_contains(name, [&res, &attempt](const User &u)
|
users.if_contains(name, [&res, &attempt](const User &u) { res = (u.password == xxHashStringGen{}(attempt)); });
|
||||||
{ res = (u.password == xxHashStringGen{}(attempt)); });
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -153,13 +147,11 @@ void Bank::ChangePassword(const std::string &name, const std::string &new_pass)
|
||||||
save_flag = true;
|
save_flag = true;
|
||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
users.modify_if(name, [&new_pass](User &u)
|
users.modify_if(name, [&new_pass](User &u) { u.password = xxHashStringGen{}(new_pass); });
|
||||||
{ u.password = xxHashStringGen{}(new_pass); });
|
|
||||||
}
|
}
|
||||||
BankResponse Bank::SetBal(const std::string &name, uint32_t amount) noexcept
|
BankResponse Bank::SetBal(const std::string &name, uint32_t amount) noexcept
|
||||||
{
|
{
|
||||||
if (ValidUsername(name) && users.modify_if(name, [amount](User &u)
|
if (ValidUsername(name) && users.modify_if(name, [amount](User &u) { u.balance = amount; }))
|
||||||
{ u.balance = amount; }))
|
|
||||||
{
|
{
|
||||||
#if CONSERVATIVE_DISK_SAVE
|
#if CONSERVATIVE_DISK_SAVE
|
||||||
#if MULTI_THREADED
|
#if MULTI_THREADED
|
||||||
|
|
@ -182,8 +174,7 @@ BankResponse Bank::ImpactBal(const std::string &name, int64_t amount) noexcept
|
||||||
return {k400BadRequest, "\"Amount cannot be 0\""};
|
return {k400BadRequest, "\"Amount cannot be 0\""};
|
||||||
}
|
}
|
||||||
uint32_t balance;
|
uint32_t balance;
|
||||||
if (ValidUsername(name) && users.modify_if(name, [&balance, amount](User &u)
|
if (ValidUsername(name) && users.modify_if(name, [&balance, amount](User &u) { balance = (u.balance < (amount * -1) ? u.balance = 0 : u.balance += amount); }))
|
||||||
{ balance = (u.balance < (amount * -1) ? u.balance = 0 : u.balance += amount); }))
|
|
||||||
{
|
{
|
||||||
#if CONSERVATIVE_DISK_SAVE
|
#if CONSERVATIVE_DISK_SAVE
|
||||||
#if MULTI_THREADED
|
#if MULTI_THREADED
|
||||||
|
|
@ -251,8 +242,7 @@ BankResponse Bank::DelUser(const std::string &name) noexcept
|
||||||
{
|
{
|
||||||
#if RETURN_ON_DEL
|
#if RETURN_ON_DEL
|
||||||
uint32_t bal;
|
uint32_t bal;
|
||||||
if (users.if_contains(name, [&bal](const User &u)
|
if (users.if_contains(name, [&bal](const User &u) { bal = u.balance; }) &&
|
||||||
{ bal = u.balance; }) &&
|
|
||||||
bal)
|
bal)
|
||||||
{
|
{
|
||||||
users.modify_if(return_account, [bal](User & u))
|
users.modify_if(return_account, [bal](User & u))
|
||||||
|
|
@ -283,8 +273,7 @@ void Bank::DelSelf(const std::string &name) noexcept
|
||||||
{
|
{
|
||||||
#if RETURN_ON_DEL
|
#if RETURN_ON_DEL
|
||||||
uint32_t bal;
|
uint32_t bal;
|
||||||
if (users.if_contains(name, [&bal](const User &u)
|
if (users.if_contains(name, [&bal](const User &u) { bal = u.balance; }) &&
|
||||||
{ bal = u.balance; }) &&
|
|
||||||
bal)
|
bal)
|
||||||
{
|
{
|
||||||
users.modify_if(return_account, [bal](User & u))
|
users.modify_if(return_account, [bal](User & u))
|
||||||
|
|
@ -328,19 +317,10 @@ const char *Bank::Save()
|
||||||
std::unique_lock<std::shared_mutex> lock{iter_lock};
|
std::unique_lock<std::shared_mutex> lock{iter_lock};
|
||||||
for (const auto &u : users)
|
for (const auto &u : users)
|
||||||
{
|
{
|
||||||
<<<<<<< Updated upstream
|
|
||||||
//we know it contains this key but we call this func to grab mutex
|
|
||||||
users.if_contains(u.first, [&users_copy, &u](const User &u_val)
|
|
||||||
{
|
|
||||||
users_copy.users.emplace_back(u_val.Encode());
|
|
||||||
users_copy.keys.emplace_back(u.first);
|
|
||||||
});
|
|
||||||
=======
|
|
||||||
users.if_contains(u.first, [&users_copy, &u](const User &u_val) {
|
users.if_contains(u.first, [&users_copy, &u](const User &u_val) {
|
||||||
users_copy.users.emplace_back(u_val.Encode());
|
users_copy.users.emplace_back(u_val.Encode());
|
||||||
users_copy.keys.emplace_back(u.first);
|
users_copy.keys.emplace_back(u.first);
|
||||||
});
|
});
|
||||||
>>>>>>> Stashed changes
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
FBE::bank_dom::GlobalFinalModel writer;
|
FBE::bank_dom::GlobalFinalModel writer;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue