mirror of
https://github.com/Expand-sys/CCash
synced 2025-12-17 08:32:13 +11:00
🎨 simplified by specializing toResponse()
This commit is contained in:
parent
f6a6c23fc3
commit
d25ff09bdf
3 changed files with 27 additions and 33 deletions
|
|
@ -14,6 +14,9 @@ class api : public HttpController<api, false>
|
||||||
|
|
||||||
public:
|
public:
|
||||||
api(Bank &b) noexcept;
|
api(Bank &b) noexcept;
|
||||||
|
void JsonCpp(req_args) const;
|
||||||
|
void Json(req_args) const;
|
||||||
|
|
||||||
#if API_VERSION >= 1
|
#if API_VERSION >= 1
|
||||||
void GetBal(req_args, const std::string &name) const;
|
void GetBal(req_args, const std::string &name) const;
|
||||||
void GetLog(req_args);
|
void GetLog(req_args);
|
||||||
|
|
|
||||||
|
|
@ -190,7 +190,7 @@ BankResponse Bank::SubBal(const std::string &name, uint32_t amount) noexcept
|
||||||
{
|
{
|
||||||
return {k400BadRequest, "Amount cannot be 0"};
|
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 CONSERVATIVE_DISK_SAVE
|
||||||
#if MULTI_THREADED
|
#if MULTI_THREADED
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,6 @@
|
||||||
#include "bank_api.h"
|
#include "bank_api.h"
|
||||||
|
|
||||||
// const auto data(R);
|
//all my homies hate jsoncpp
|
||||||
// auto res = std::make_shared<HttpResponseImpl>(data.first, CT_APPLICATION_JSON);
|
|
||||||
// res->setJsonObject(JsonCast(std::move(data.second)));
|
|
||||||
// doResponseCreateAdvices(res);
|
|
||||||
// callback(res);
|
|
||||||
|
|
||||||
#define CACHE_FOREVER resp->setExpiredTime(0);
|
#define CACHE_FOREVER resp->setExpiredTime(0);
|
||||||
|
|
||||||
|
|
@ -12,42 +8,37 @@
|
||||||
const auto temp_req = req->getJsonObject(); \
|
const auto temp_req = req->getJsonObject(); \
|
||||||
const auto body = temp_req ? *temp_req : Json::Value();
|
const auto body = temp_req ? *temp_req : Json::Value();
|
||||||
|
|
||||||
#define RESPONSE_PARSE(R) \
|
#define RESPONSE_PARSE(R) callback(HttpResponse::newCustomHttpResponse(R));
|
||||||
const auto r(R); \
|
|
||||||
auto resp = HttpResponse::newHttpJsonResponse(JsonCast(std::move(r.second))); \
|
|
||||||
resp->setStatusCode(r.first); \
|
|
||||||
callback(resp);
|
|
||||||
|
|
||||||
#define RESPOND_TRUE \
|
#define RESPOND_TRUE \
|
||||||
auto resp = HttpResponse::newHttpJsonResponse(JsonCast(true)); \
|
auto resp = HttpResponse::newHttpJsonResponse(true); \
|
||||||
CACHE_FOREVER \
|
CACHE_FOREVER \
|
||||||
callback(resp);
|
callback(resp);
|
||||||
|
|
||||||
#define NAME_PARAM req->getBody().data()
|
#define NAME_PARAM req->getBody().data()
|
||||||
|
|
||||||
template <typename T>
|
#include <drogon/HttpResponse.h>
|
||||||
constexpr Json::Value JsonCast(T &&val)
|
#include <../src/HttpResponseImpl.h>
|
||||||
|
#include <../src/HttpAppFrameworkImpl.h>
|
||||||
|
template <>
|
||||||
|
HttpResponsePtr drogon::toResponse(BankResponse &&data)
|
||||||
{
|
{
|
||||||
|
auto res = std::make_shared<HttpResponseImpl>(data.first, CT_APPLICATION_JSON);
|
||||||
if constexpr (std::is_same_v<T, int_fast8_t>)
|
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)
|
||||||
}
|
{
|
||||||
else if constexpr (std::is_same_v<T, uint64_t>)
|
advice(res);
|
||||||
{
|
}
|
||||||
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
|
#if API_VERSION >= 1
|
||||||
|
|
||||||
|
|
@ -121,7 +112,7 @@ void api::Close(req_args) const
|
||||||
}
|
}
|
||||||
void api::Contains(req_args, const std::string &name) 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);
|
callback(resp);
|
||||||
}
|
}
|
||||||
void api::AdminVerifyAccount(req_args) const
|
void api::AdminVerifyAccount(req_args) const
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue