From 53b3c2c3ac043c8aff918b7e10815fcfeb36d963 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Sun, 4 Apr 2021 21:57:01 -0700 Subject: [PATCH] JsonReturn changes --- include/bank_f.hpp | 75 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 include/bank_f.hpp diff --git a/include/bank_f.hpp b/include/bank_f.hpp new file mode 100644 index 0000000..c7e3cf7 --- /dev/null +++ b/include/bank_f.hpp @@ -0,0 +1,75 @@ +#pragma once +#include +#include "bank.hpp" + +using namespace drogon; + +#define req_args const HttpRequestPtr &req, std::function &&callback +#define JSON(V) callback(HttpResponse::newHttpJsonResponse(JsonReturn(V))); +#define INLINE __attribute__((always_inline)) inline + +template +INLINE Json::Value JsonReturn(T &&val) +{ + Json::Value res; + std::string reason; + if constexpr (std::is_same_v) + { + res["value"] = (int)val; //becuase of json lib interpreting 67 as 'A' for example + } + else + { + res["value"] = val; + } + return res; +} + +class BankF : public HttpController +{ +public: + void Help(req_args) const + { + auto resp = HttpResponse::newHttpResponse(); + auto handlerInfo = app().getHandlersInfo(); + resp->setBody(""); + resp->setExpiredTime(0); + callback(resp); + } + void Close(req_args, const std::string &attempt) const + { + auto resp = HttpResponse::newHttpResponse(); + if (attempt == bank.admin_pass) + { + resp->setBody("

[Webserver Closed]

"); + bank.Save(); + callback(resp); + app().quit(); + } + else + { + resp->setBody("

[Invalid Password]

"); + callback(resp); + } + } + void AddUser(req_args, const std::string &name, std::string &&init_pass) const { JSON(bank.AddUser(name, std::move(init_pass))); } + void AdminAddUser(req_args, const std::string &attempt, std::string &&name, uint_fast32_t init_bal, std::string &&init_pass) const { JSON(bank.AdminAddUser(attempt, std::move(name), init_bal, std::move(init_pass))); } + + void DelUser(req_args, const std::string &name, const std::string &attempt) const { JSON(bank.DelUser(name, attempt)); } + void AdminDelUser(req_args, const std::string &name, const std::string &attempt) const { JSON(bank.AdminDelUser(name, attempt)); } + + void SendFunds(req_args, const std::string &a_name, const std::string &b_name, uint_fast32_t amount, const std::string &attempt) const { JSON(bank.SendFunds(a_name, b_name, amount, attempt)); } + void ChangePassword(req_args, const std::string &name, const std::string &attempt, std::string &&new_pass) const { JSON(bank.ChangePassword(name, attempt, std::move(new_pass))); } + + void Contains(req_args, const std::string &name) const { JSON(bank.Contains(name)); } + void GetBal(req_args, const std::string &name) const { JSON(bank.GetBal(name)); } + void VerifyPassword(req_args, const std::string &name, const std::string &attempt) const { JSON(bank.VerifyPassword(name, attempt)); } + + METHOD_LIST_BEGIN + METHOD_ADD(BankF::Help, "/help", Get); + //gotta add bindings here + METHOD_LIST_END +}; \ No newline at end of file