diff --git a/include/bank.hpp b/include/bank.hpp index c67fbca..768151a 100644 --- a/include/bank.hpp +++ b/include/bank.hpp @@ -23,14 +23,15 @@ private: * permitted while saving so they do not have to share this lock * */ - std::shared_mutex save_lock; + std::shared_mutex size_lock; + std::shared_mutex bal_lock; public: std::string admin_pass; bool AddUser(const std::string &name, std::string &&init_pass) { - std::shared_lock lock{save_lock}; + std::shared_lock lock{size_lock}; return users.try_emplace_l( name, [](User &) {}, std::move(init_pass)); } @@ -39,7 +40,7 @@ public: bool state = (admin_pass == attempt); if (state) { - std::shared_lock lock{save_lock}; + std::shared_lock lock{size_lock}; state = users.try_emplace_l( name, [](User &) {}, init_bal, std::move(init_pass)); } @@ -48,12 +49,12 @@ public: bool DelUser(const std::string &name, const std::string &attempt) { - std::shared_lock lock{save_lock}; + std::shared_lock lock{size_lock}; return users.erase_if(name, [&attempt](const User &u) { return (attempt == u.password); }); } bool AdminDelUser(const std::string &name, const std::string &attempt) { - std::shared_lock lock{save_lock}; + std::shared_lock lock{size_lock}; return users.erase_if(name, [this, &attempt](const User &) { return (admin_pass == attempt); }); } @@ -68,7 +69,7 @@ public: //if A exists, A can afford it, and A's password matches bool state = false; - std::shared_lock lock{save_lock}; //because SendFunds requires 3 locking operations + std::shared_lock lock{bal_lock}; //because SendFunds requires 3 locking operations users.modify_if(a_name, [&state, amount, &attempt](User &a) { if (state = (a.balance >= amount) && (a.password == attempt), state) { @@ -138,6 +139,19 @@ public: return res; } + Json::Value AllUsers() + { + Json::Value temp; + Json::UInt i = 0; + std::unique_lock lock{size_lock}; + for (const auto &u : users) + { + //we know it contains this key but we call this func to grab mutex + temp[i++] = u.first; + } + return temp; + } + void Save() { Json::StreamWriterBuilder builder; @@ -148,7 +162,7 @@ public: //loading info into json temp { - std::unique_lock lock{save_lock}; //grabbing it from any busy add/del opperations + std::scoped_lock lock{size_lock, bal_lock}; //grabbing it from any busy add/del/sendfunds opperations for (const auto &u : users) { //we know it contains this key but we call this func to grab mutex diff --git a/include/bank_f.hpp b/include/bank_f.hpp index d516622..121f086 100644 --- a/include/bank_f.hpp +++ b/include/bank_f.hpp @@ -37,7 +37,7 @@ public: { auto resp = HttpResponse::newHttpResponse(); auto handlerInfo = app().getHandlersInfo(); - resp->setBody("

ALL FUNCTIONS (that have args) ARE EXPECTING JSON AS DATA TYPE


/BankF/admin/close (POST)

 attempt  - admin password

Closes and Saves the server.


/BankF/user (POST)

 name  - name of the user being added

 init_pass  - initial password for the user being added

Adds a user to the bank


/BankF/admin/user (POST)

 name  - name of the user being added

 attempt  - admin password required to add user with balance

 init_bal  - initial balance for user being added

 init_pass  - initial password for user being added

Adds a user with initial balance


/BankF/sendfunds (POST)

 a_name  - sender's name

 b_name  - reciever's name

 amount  - amount being sent

 attempt  - password of sender

Sends money from one user to another


/BankF/changepass (PATCH)

 name  - name of user's password being changes

 attempt  - password of user being changed

 new_pass  - new password to replace the current user's password

 Changes password of a user

 

/BankF/{name}/bal (PATCH)

 name  - the name of the user being set

 attempt  - the admin password required

 amount  - the new balance of the user

Sets the balance of a user

 

/BankF/help (GET)

the page you're looking at right now!


/BankF/vpass (POST)

 name  - name of user being verified

 attempt  - password being verified

returns 0 or 1 based on if [attempt] is equal to the password of the user [name]. The intended usage for this function is for connected services


/BankF/contains/{name} (GET)

returns a 0 or 1 based on if the bank contains the user

 

/BankF/{name}/bal (GET)

returns the balance of a given user's name, if -1 that means the user does not exist


/BankF/user (DELETE)

 name  - name of user being deleted

 attempt  - password of user being deleted

Deletes a user with the password of the user as verification


/BankF/admin/user (DELETE)

 name  - name of user being deleted

 attempt  - admin password

Deletes a user with admin password as verification

"); + resp->setBody("

ALL FUNCTIONS (that have args) ARE EXPECTING JSON AS DATA TYPE

/BankF/admin/close (POST)

 attempt  - admin password

Closes and Saves the server.

/BankF/user (POST)

 name  - name of the user being added

 init_pass  - initial password for the user being added

Adds a user to the bank

/BankF/admin/user (POST)

 name  - name of the user being added

 attempt  - admin password required to add user with balance

 init_bal  - initial balance for user being added

 init_pass  - initial password for user being added

Adds a user with initial balance

/BankF/sendfunds (POST)

 a_name  - sender's name

 b_name  - reciever's name

 amount  - amount being sent

 attempt  - password of sender

Sends money from one user to another

/BankF/changepass (PATCH)

 name  - name of user's password being changes

 attempt  - password of user being changed

 new_pass  - new password to replace the current user's password

Changes password of a user

/BankF/{name}/bal (PATCH)

 name  - the name of the user being set

 attempt  - the admin password required

 amount  - the new balance of the user

Sets the balance of a user

/BankF/help (GET)

the page you're looking at right now!

/BankF/vpass (POST)

 name  - name of user being verified

 attempt  - password being verified

returns 0 or 1 based on if [attempt] is equal to the password of the user [name]. The intended usage for this function is for connected services

/BankF/contains/{name} (GET)

returns a 0 or 1 based on if the bank contains the user

/BankF/{name}/bal (GET)

returns the balance of a given user's name, if -1 that means the user does not exist

/BankF/allusers/ (GET)

returns an array of all users in the bank

/BankF/user (DELETE)

 name  - name of user being deleted

 attempt  - password of user being deleted

Deletes a user with the password of the user as verification

/BankF/admin/user (DELETE)

 name  - name of user being deleted

 attempt  - admin password

Deletes a user with admin password as verification

"); resp->setExpiredTime(0); callback(resp); } @@ -106,6 +106,10 @@ public: GEN_BODY JSON(bank.SetBal(name, body["attempt"].asCString(), body["amount"].asUInt())); } + void AllUsers(req_args) + { + JSON(bank.AllUsers()); + } METHOD_LIST_BEGIN METHOD_ADD(BankF::Close, "/admin/close", Post, Options); @@ -120,6 +124,7 @@ public: METHOD_ADD(BankF::VerifyPassword, "/vpass", Post, Options); METHOD_ADD(BankF::Contains, "/contains/{name}", Get, Options); METHOD_ADD(BankF::GetBal, "/{name}/bal", Get, Options); + METHOD_ADD(BankF::AllUsers, "/allusers", Get, Options); METHOD_ADD(BankF::DelUser, "/user", Delete, Options); METHOD_ADD(BankF::AdminDelUser, "/admin/user", Delete, Options);