🎨 simplified by specializing toResponse()

This commit is contained in:
EntireTwix 2021-07-05 14:52:31 -07:00
parent f6a6c23fc3
commit d25ff09bdf
3 changed files with 27 additions and 33 deletions

View file

@ -14,6 +14,9 @@ class api : public HttpController<api, false>
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);

View file

@ -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

View file

@ -1,10 +1,6 @@
#include "bank_api.h"
// const auto data(R);
// auto res = std::make_shared<HttpResponseImpl>(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)); \
auto resp = HttpResponse::newHttpJsonResponse(true); \
CACHE_FOREVER \
callback(resp);
#define NAME_PARAM req->getBody().data()
template <typename T>
constexpr Json::Value JsonCast(T &&val)
#include <drogon/HttpResponse.h>
#include <../src/HttpResponseImpl.h>
#include <../src/HttpAppFrameworkImpl.h>
template <>
HttpResponsePtr drogon::toResponse(BankResponse &&data)
{
if constexpr (std::is_same_v<T, int_fast8_t>)
auto res = std::make_shared<HttpResponseImpl>(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
for (auto &advice : advices)
{
advice(res);
}
else if constexpr (std::is_same_v<T, uint64_t>)
{
return (Json::UInt64)val;
}
else if constexpr (std::is_same_v<T, uint32_t>)
{
return (Json::UInt)val;
}
else
{
return val;
}
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