From 8d4ad2f6317a9e4773be0e3d28775a8139dacda2 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Mon, 22 Jul 2019 22:26:42 +0200 Subject: [PATCH] add: relays get all api endpoint --- controllers/api_v1_relays.cc | 25 +++++++++++++++++++++++++ controllers/api_v1_relays.h | 18 ++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 controllers/api_v1_relays.cc create mode 100644 controllers/api_v1_relays.h diff --git a/controllers/api_v1_relays.cc b/controllers/api_v1_relays.cc new file mode 100644 index 0000000..d7f5ef7 --- /dev/null +++ b/controllers/api_v1_relays.cc @@ -0,0 +1,25 @@ +#include +#include +#include +#include "api_v1_relays.h" +using namespace api::v1; + +void +relays::get_all(const HttpRequestPtr &req, std::function &&callback) +{ + relay_dbo **all_relays = relay_dbo::get_all(); + Json::Value all_relays_json(Json::arrayValue); + + for(int i = 0; all_relays[i] != nullptr; i++) + { + all_relays_json.append(all_relays[i]->to_json()); + } + + Json::StreamWriterBuilder jw; + + auto resp = HttpResponse::newHttpJsonResponse(all_relays_json); + + callback(resp); + + relay_dbo::free_list(all_relays); +} diff --git a/controllers/api_v1_relays.h b/controllers/api_v1_relays.h new file mode 100644 index 0000000..896ff8c --- /dev/null +++ b/controllers/api_v1_relays.h @@ -0,0 +1,18 @@ +#pragma once +#include +using namespace drogon; +namespace api +{ + namespace v1 + { + class relays:public drogon::HttpController + { + public: + METHOD_LIST_BEGIN + METHOD_ADD(relays::get_all, "/", Get); + METHOD_LIST_END + + static void get_all(const HttpRequestPtr& req, std::function &&callback); + }; + } +}