🐎🔨 modified endpoints to better adhere to JSON API conventions

This commit is contained in:
EntireTwix 2021-07-09 19:51:26 -07:00
parent 2d7d29372c
commit 1f0a3dc28d
5 changed files with 26 additions and 20 deletions

View file

@ -15,11 +15,11 @@
### Usage endpoints
| name | purpose | json input | path | HTTP Method | return type | return value | Jresp | Jreq | A | U |
| :------------- | ------------------------------------------------------------------------------ | -------------------------------- | ------------------------------- | :---------: | :------------: | :--------------------------------------------: | :----------------: | :----------------------: | :----------------------: | :----------------------: |
| :------------- | ------------------------------------------------------------------------------ | -------------------------------- | ------------------------------- | :---------: | :--------------: | :--------------------------------------------: | :----------------: | :----------------------: | :----------------------: | :----------------------: |
| GetBal | retrieving the balance of a given user, `{name}` | `N/A` | api/v1/user/balance?name={name} | `GET` | uint32 | the user's balance | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_multiplication_x: | :heavy_multiplication_x: |
| GetLog | retrieves the logs of a given user, length varies by server configuration | `N/A` | api/v1/user/log | `GET` | array of jsons | [{"to":string, "amount":uint32, "time":int64}] | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_multiplication_x: | :heavy_check_mark: |
| SendFunds | sends funds from the authenticated user to the user `{name}` given in the json | {"name":string, "amount":uint32} | api/v1/user/transfer | `POST` | bool | true | :heavy_check_mark: | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_check_mark: |
| VerifyPassword | verifies the credentials, used for connected services for ease of use | `N/A` | api/v1/user/verify_password | `POST` | bool | true | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_multiplication_x: | :heavy_check_mark: |
| GetLog | retrieves the logs of a given user, length varies by server configuration | `N/A` | api/v1/user/log | `GET` | array of objects | [{"to":string, "amount":uint32, "time":int64}] | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_multiplication_x: | :heavy_check_mark: |
| SendFunds | sends funds from the authenticated user to the user `{name}` given in the json | {"name":string, "amount":uint32} | api/v1/user/transfer | `POST` | uint32 | the user's balance after the transaction | :heavy_check_mark: | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_check_mark: |
| VerifyPassword | verifies the credentials, used for connected services for ease of use | `N/A` | api/v1/user/verify_password | `POST` | `N/A` | null | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_multiplication_x: | :heavy_check_mark: |
### Usage enpoints errors
| name | 400 | 401 | 404 | 405 | 406 |

View file

@ -60,7 +60,7 @@ public:
bool Contains(const std::string &name) const noexcept;
bool AdminVerifyAccount(const std::string &name) noexcept;
BankResponse AddUser(std::string &&name, uint32_t init_bal, std::string &&init_pass) noexcept;
BankResponse AddUser(const std::string &name, uint32_t init_bal, std::string &&init_pass) noexcept;
BankResponse DelUser(const std::string &name) noexcept;
void Save();

View file

@ -11,9 +11,9 @@ struct BankResponse
drogon::HttpStatusCode first = drogon::k200OK;
std::string second;
BankResponse() noexcept ;
BankResponse(drogon::HttpStatusCode code, std::string &&str) noexcept ;
BankResponse(drogon::HttpStatusCode code, const std::string &str) noexcept ;
BankResponse() noexcept;
BankResponse(drogon::HttpStatusCode code, std::string &&str) noexcept;
BankResponse(drogon::HttpStatusCode code, const std::string &str) noexcept;
};
template <>

View file

@ -104,10 +104,8 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
std::shared_lock<std::shared_mutex> lock{save_lock}; //about 10% of this function's cost
#if MAX_LOG_SIZE > 0
static thread_local Transaction temp(a_name, b_name, amount);
if (!users.modify_if(a_name, [&state, amount](User &a) {
#else
if (!users.modify_if(a_name, [&state, amount](User &a) {
#endif
if (!users.modify_if(a_name, [&state, amount](User &a) {
//if A can afford it
if (a.balance < amount)
{
@ -119,7 +117,7 @@ BankResponse Bank::SendFunds(const std::string &a_name, const std::string &b_nam
#if MAX_LOG_SIZE > 0
a.log.AddTrans(Transaction(temp)); //about 40% of this function's cost
#endif
state = BankResponse(k200OK, "true");
state = BankResponse(k200OK, std::to_string(a.balance));
}
}))
{
@ -211,8 +209,7 @@ bool Bank::AdminVerifyAccount(const std::string &name) noexcept
{
return (name == admin_account);
}
BankResponse Bank::AddUser(std::string &&name, uint32_t init_bal, std::string &&init_pass) noexcept
BankResponse Bank::AddUser(const std::string &name, uint32_t init_bal, std::string &&init_pass) noexcept
{
if (!ValidUsrname(name))
{
@ -229,7 +226,7 @@ BankResponse Bank::AddUser(std::string &&name, uint32_t init_bal, std::string &&
save_flag = true;
#endif
#endif
return {k200OK, "true"};
return {k204NoContent, nullptr};
}
else
{
@ -259,7 +256,7 @@ BankResponse Bank::DelUser(const std::string &name) noexcept
save_flag = true;
#endif
#endif
return BankResponse(k200OK, "true");
return BankResponse(k204NoContent, nullptr);
}
else
{

View file

@ -7,8 +7,17 @@ BankResponse::BankResponse(drogon::HttpStatusCode code, const std::string &str)
template <>
drogon::HttpResponsePtr drogon::toResponse(BankResponse &&data)
{
const auto &res = std::make_shared<HttpResponseImpl>(data.first, CT_APPLICATION_JSON);
std::shared_ptr<HttpResponseImpl> res;
if (data.first == k204NoContent)
{
res = std::make_shared<HttpResponseImpl>(data.first, CT_APPLICATION_JSON);
res->setBody(std::move(data.second));
}
else
{
res = std::make_shared<HttpResponseImpl>();
res->setStatusCode(data.first);
}
const auto &advices = HttpAppFrameworkImpl::instance().getResponseCreationAdvices();
if (!advices.empty())
{