🐛 bug fixes and implementing Bank API

This commit is contained in:
EntireTwix 2021-04-02 12:32:00 -07:00
parent e4c262bd79
commit 1f6378ad92
4 changed files with 54 additions and 68 deletions

View file

@ -14,5 +14,5 @@ add_executable(${PROJECT_NAME} main.cpp)
target_include_directories(${PROJECT_NAME} PUBLIC include) target_include_directories(${PROJECT_NAME} PUBLIC include)
target_include_directories(${PROJECT_NAME} PUBLIC third_party) target_include_directories(${PROJECT_NAME} PUBLIC third_party)
add_subdirectory(third_party/drogon) # add_subdirectory(third_party/drogon)
target_link_libraries(${PROJECT_NAME} PRIVATE drogon) # target_link_libraries(${PROJECT_NAME} PRIVATE drogon)

View file

@ -20,25 +20,61 @@ public:
bool AddUser(const std::string &name, std::string &&init_pass) bool AddUser(const std::string &name, std::string &&init_pass)
{ {
return users.try_emplace_l( return users.try_emplace_l(
name, []() {}, init_pass); name, [](User &) {}, std::forward<std::string &&>(init_pass));
} }
bool AdminAddUser(const std::string &attempt, std::string &&name, uint_fast64_t init_bal, std::string &&init_pass) bool AdminAddUser(const std::string &attempt, std::string &&name, uint_fast32_t init_bal, std::string &&init_pass)
{ {
const bool state = (admin_pass == attempt); const bool state = (admin_pass == attempt);
if (state) if (state)
{ {
users.try_emplace_l( users.try_emplace_l(
name, []() {}, init_bal, init_pass); name, [](User &) {}, init_bal, std::forward<std::string &&>(init_pass));
} }
return state; return state;
} }
bool SendFunds(const std::string &a_name, const std::string &b_name, uint_fast64_t amount, const std::string &attempt)
bool DelUser(const std::string &name, const std::string &attempt)
{
bool state = false;
//if password for user is correct, and user exists
users.if_contains(name, [&attempt, &state](const User &u) {
if (attempt == u.password)
{
state = true;
}
});
if (state) //correct password
{
state = users.erase_if(name, [](const User &) { return true; });
}
return state;
}
bool AdminDelUser(const std::string &name, const std::string &attempt)
{
bool state = (attempt == admin_pass);
if (state)
{
state = users.erase_if(name, [&state](const User &) { state = true; return state; });
}
return state;
}
int_fast64_t GetBal(const std::string &name)
{
int_fast64_t res = -1;
users.if_contains(name, [&res](const User &u) {
res = u.balance;
});
return res;
}
bool SendFunds(const std::string &a_name, const std::string &b_name, uint_fast32_t amount, const std::string &attempt)
{ {
//if A exists, A can afford it, and A's password matches //if A exists, A can afford it, and A's password matches
bool state = false; bool state = false;
users.modify_if(a_name, [&state, amount, &attempt](User &a) { users.modify_if(a_name, [&state, amount, &attempt](User &a) {
if (state = (a.balance >= amount) && (a.password == attempt)) if (state = (a.balance >= amount) && (a.password == attempt), state)
{ {
a.balance -= amount; a.balance -= amount;
} }
@ -55,7 +91,7 @@ public:
users.modify_if(a_name, [amount](User &a) { users.modify_if(a_name, [amount](User &a) {
a.balance += amount; a.balance += amount;
}); });
state = false; //because had to refund trasnaction state = false; //because had to refund transaction
} }
} }

View file

@ -4,7 +4,7 @@
struct User struct User
{ {
uint_fast64_t balance = 0; uint_fast32_t balance = 0;
std::string password; std::string password;
/** /**
@ -20,56 +20,5 @@ struct User
* @param init_bal initial balance * @param init_bal initial balance
* @param init_pass initial password * @param init_pass initial password
*/ */
User(uint_fast64_t init_bal, std::string &&init_pass) : balance(init_bal), password(init_pass) {} User(uint_fast32_t init_bal, std::string &&init_pass) : balance(init_bal), password(init_pass) {}
// bool ChangePassword(const std::string &attempt, std::string &&new_pass)
// {
// const bool state = (password == attempt);
// if (state)
// {
// password = new_pass;
// }
// return state;
// }
// // /**
// // * @brief SendFunds allows sending of money between users if verification is provided in the form of a password and if the user has sufficient funds
// // *
// // * @param a first user
// // * @param b second user
// // * @param amount amount being sent
// // * @param attempt password of first user
// // * @return wether transaction was succesful
// // */
// // static bool SendFunds(User &a, User &b, uint_fast64_t amount, const std::string &attempt)
// // {
// // const bool state = (a.password == attempt) && (a.balance >= amount);
// // if (state)
// // {
// // a.balance -= amount;
// // b.balance += amount;
// // }
// // return state;
// // }
// /**
// * @brief Get the balance of the User object
// *
// * @return the balance
// */
// uint_fast64_t GetBal() const
// {
// return balance;
// }
// /**
// * @brief Used for Verification of password by external services, this can be used for logging in or signature
// *
// * @param attempt the password
// * @return wether the passwords match
// */
// bool VerifyPassword(const std::string &attempt) const
// {
// return (password == attempt);
// }
}; };

View file

@ -1,14 +1,15 @@
#include <iostream> #include <iostream>
#include "user.hpp" #include "bank.hpp"
int main() int main()
{ {
User a(1000, "pass123"); Bank.admin_pass = "root";
User b(0, "pass123"); std::cout << Bank.AddUser("Twix", "pass123") << '\n';
a.ChangePassword("pass123", "newpass123"); std::cout << Bank.GetBal("Twixy") << '\n';
User::SendFunds(a, b, 250, "newpass123"); std::cout << Bank.AdminAddUser("root", "Jollymonsam", 2500, "pass123") << '\n';
a.GetBal(); std::cout << Bank.GetBal("Jollymonsam") << '\n';
a.VerifyPassword("newpass124"); std::cout << Bank.SendFunds("Jollymonsam", "Twix", 333, "pass123") << '\n';
std::cout << Bank.GetBal("Twix") << " | " << Bank.GetBal("Jollymonsam") << '\n';
return 0; return 0;
} }