mirror of
https://github.com/Expand-sys/CCash
synced 2025-12-18 17:12:14 +11:00
🐎 performance changes
This commit is contained in:
parent
1aa450e112
commit
731132e719
1 changed files with 35 additions and 29 deletions
64
src/bank.cpp
64
src/bank.cpp
|
|
@ -19,26 +19,29 @@ bool Bank::GetChangeState() noexcept
|
||||||
|
|
||||||
BankResponse Bank::GetBal(const std::string &name) const noexcept
|
BankResponse Bank::GetBal(const std::string &name) const noexcept
|
||||||
{
|
{
|
||||||
BankResponse res = {k404NotFound, "User not found"};
|
int_fast64_t res = -1;
|
||||||
users.if_contains(name, [&res](const User &u) {
|
users.if_contains(name, [&res](const User &u) {
|
||||||
res = {k200OK, u.balance};
|
res = u.balance;
|
||||||
});
|
});
|
||||||
return res;
|
return res < 0 ? BankResponse(k404NotFound, "User not found") : BankResponse(k200OK, res);
|
||||||
}
|
}
|
||||||
BankResponse Bank::GetLogs(const std::string &name) noexcept
|
BankResponse Bank::GetLogs(const std::string &name) noexcept
|
||||||
{
|
{
|
||||||
BankResponse res{k404NotFound, "User not found"};
|
BankResponse res;
|
||||||
users.if_contains(name, [&res](const User &u) {
|
if (!users.if_contains(name, [&res](const User &u) {
|
||||||
Json::Value temp;
|
Json::Value temp;
|
||||||
for (uint32_t i = u.log.data.size(); i > 0; --i)
|
for (uint32_t i = u.log.data.size(); i > 0; --i)
|
||||||
{
|
{
|
||||||
temp[i - 1]["to"] = u.log.data[u.log.data.size() - i].to;
|
temp[i - 1]["to"] = u.log.data[u.log.data.size() - i].to;
|
||||||
temp[i - 1]["from"] = u.log.data[u.log.data.size() - i].from;
|
temp[i - 1]["from"] = u.log.data[u.log.data.size() - i].from;
|
||||||
temp[i - 1]["amount"] = (Json::UInt)u.log.data[u.log.data.size() - i].amount;
|
temp[i - 1]["amount"] = (Json::UInt)u.log.data[u.log.data.size() - i].amount;
|
||||||
temp[i - 1]["time"] = (Json::UInt64)u.log.data[u.log.data.size() - i].time;
|
temp[i - 1]["time"] = (Json::UInt64)u.log.data[u.log.data.size() - i].time;
|
||||||
}
|
}
|
||||||
res = {k200OK, std::move(temp)};
|
res = {k200OK, std::move(temp)};
|
||||||
});
|
}))
|
||||||
|
{
|
||||||
|
return BankResponse(k404NotFound, "User not found");
|
||||||
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount) noexcept
|
BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount) noexcept
|
||||||
|
|
@ -61,24 +64,27 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
|
||||||
return {k404NotFound, "Reciever does not exist"};
|
return {k404NotFound, "Reciever does not exist"};
|
||||||
}
|
}
|
||||||
|
|
||||||
BankResponse state = {k404NotFound, "Sender does not exist"};
|
BankResponse state;
|
||||||
if constexpr (max_log_size > 0)
|
if constexpr (max_log_size > 0)
|
||||||
{
|
{
|
||||||
Transaction temp(a_name, b_name, amount);
|
Transaction temp(a_name, b_name, amount);
|
||||||
std::shared_lock<std::shared_mutex> lock{send_funds_l};
|
std::shared_lock<std::shared_mutex> lock{send_funds_l};
|
||||||
users.modify_if(a_name, [&temp, &state, amount](User &a) {
|
if (!users.modify_if(a_name, [&temp, &state, amount](User &a) {
|
||||||
//if A can afford it
|
//if A can afford it
|
||||||
if (a.balance < amount)
|
if (a.balance < amount)
|
||||||
{
|
{
|
||||||
state = {k402PaymentRequired, "Sender has insufficient funds"};
|
state = {k402PaymentRequired, "Sender has insufficient funds"};
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
a.balance -= amount;
|
a.balance -= amount;
|
||||||
a.log.AddTrans(Transaction(temp));
|
a.log.AddTrans(Transaction(temp));
|
||||||
state = {k200OK, "Transfer successful!"};
|
state = {k200OK, "Transfer successful!"};
|
||||||
}
|
}
|
||||||
});
|
}))
|
||||||
|
{
|
||||||
|
return {k404NotFound, "Sender does not exist"};
|
||||||
|
}
|
||||||
if (state.first == k200OK)
|
if (state.first == k200OK)
|
||||||
{
|
{
|
||||||
users.modify_if(b_name, [&temp, amount](User &b) {
|
users.modify_if(b_name, [&temp, amount](User &b) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue