diff --git a/include/bank_api.h b/include/bank_api.h index ff14f5d..50d00d7 100644 --- a/include/bank_api.h +++ b/include/bank_api.h @@ -14,6 +14,9 @@ class api : public HttpController public: api(Bank &b) noexcept; + void JsonCpp(req_args) const; + void Json(req_args) const; + #if API_VERSION >= 1 void GetBal(req_args, const std::string &name) const; void GetLog(req_args); diff --git a/src/bank.cpp b/src/bank.cpp index 7e8441e..6441299 100644 --- a/src/bank.cpp +++ b/src/bank.cpp @@ -190,7 +190,7 @@ BankResponse Bank::SubBal(const std::string &name, uint32_t amount) noexcept { return {k400BadRequest, "Amount cannot be 0"}; } - if (users.modify_if(name, [amount](User &u) { amount > u.balance ? u.balance = 0 : u.balance -= amount; })) + if (users.modify_if(name, [amount](User &u) { amount >= u.balance ? u.balance = 0 : u.balance -= amount; })) { #if CONSERVATIVE_DISK_SAVE #if MULTI_THREADED diff --git a/src/bank_api.cpp b/src/bank_api.cpp index b05dec4..15261be 100644 --- a/src/bank_api.cpp +++ b/src/bank_api.cpp @@ -1,10 +1,6 @@ #include "bank_api.h" -// const auto data(R); -// auto res = std::make_shared(data.first, CT_APPLICATION_JSON); -// res->setJsonObject(JsonCast(std::move(data.second))); -// doResponseCreateAdvices(res); -// callback(res); +//all my homies hate jsoncpp #define CACHE_FOREVER resp->setExpiredTime(0); @@ -12,42 +8,37 @@ const auto temp_req = req->getJsonObject(); \ const auto body = temp_req ? *temp_req : Json::Value(); -#define RESPONSE_PARSE(R) \ - const auto r(R); \ - auto resp = HttpResponse::newHttpJsonResponse(JsonCast(std::move(r.second))); \ - resp->setStatusCode(r.first); \ - callback(resp); +#define RESPONSE_PARSE(R) callback(HttpResponse::newCustomHttpResponse(R)); -#define RESPOND_TRUE \ - auto resp = HttpResponse::newHttpJsonResponse(JsonCast(true)); \ - CACHE_FOREVER \ +#define RESPOND_TRUE \ + auto resp = HttpResponse::newHttpJsonResponse(true); \ + CACHE_FOREVER \ callback(resp); #define NAME_PARAM req->getBody().data() -template -constexpr Json::Value JsonCast(T &&val) +#include +#include <../src/HttpResponseImpl.h> +#include <../src/HttpAppFrameworkImpl.h> +template <> +HttpResponsePtr drogon::toResponse(BankResponse &&data) { - - if constexpr (std::is_same_v) + auto res = std::make_shared(data.first, CT_APPLICATION_JSON); + res->setJsonObject(std::move(data.second)); + auto &advices = HttpAppFrameworkImpl::instance().getResponseCreationAdvices(); + if (!advices.empty()) { - return (int)val; //becuase of json lib interpreting 67 as 'A' for example - } - else if constexpr (std::is_same_v) - { - return (Json::UInt64)val; - } - else if constexpr (std::is_same_v) - { - return (Json::UInt)val; - } - else - { - return val; + for (auto &advice : advices) + { + advice(res); + } } + return res; } -api::api(Bank &b) noexcept : bank(b) {} +api::api(Bank &b) noexcept : bank(b) +{ +} #if API_VERSION >= 1 @@ -121,7 +112,7 @@ void api::Close(req_args) const } void api::Contains(req_args, const std::string &name) const { - auto resp = HttpResponse::newHttpJsonResponse(JsonCast(bank.Contains(name))); + auto resp = HttpResponse::newHttpJsonResponse(bank.Contains(name)); callback(resp); } void api::AdminVerifyAccount(req_args) const