From 831291cdac14f6d002d3c30de7b5bdc4c7b0ffbd Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Mon, 29 Jul 2019 23:59:16 +0200 Subject: [PATCH] add: put controller api endpoint --- controllers/api_v1_controllers.cc | 38 +++++++++++++++++++++++++++++++ controllers/api_v1_controllers.h | 2 ++ filters/controllers_valid_json.cc | 28 +++++++++++++++++++++++ filters/controllers_valid_json.h | 20 ++++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 filters/controllers_valid_json.cc create mode 100644 filters/controllers_valid_json.h diff --git a/controllers/api_v1_controllers.cc b/controllers/api_v1_controllers.cc index 5199771..ff3f07f 100644 --- a/controllers/api_v1_controllers.cc +++ b/controllers/api_v1_controllers.cc @@ -66,3 +66,41 @@ controllers::delete_one_by_id(const HttpRequestPtr &req, std::function &&callback, + const std::string &controller_id) +{ + Json::Value body = *req->getJsonObject(); + + controller_dbo **controllers = controller_dbo::get_by_simple("id", controller_id.c_str(), (intptr_t) &sqlite3_bind_text); + + if(controllers[0]) + { + strncpy(controllers[0]->name, body["name"].asCString(), 128); + strncpy(controllers[0]->ip, body["ip"].asCString(), 17); + + controllers[0]->name[127] = '\0'; + controllers[0]->ip[16] = '\0'; + + auto resp = HttpResponse::newHttpResponse(); + if(controllers[0]->update()) + { + resp = HttpResponse::newHttpJsonResponse(controllers[0]->to_json()); + } + else + { + resp->setStatusCode(k500InternalServerError); + } + + callback(resp); + } + else + { + auto resp = HttpResponse::newHttpResponse(); + resp->setStatusCode(k404NotFound); + + callback(resp); + } + controller_dbo::free_list(controllers); +} \ No newline at end of file diff --git a/controllers/api_v1_controllers.h b/controllers/api_v1_controllers.h index db37532..0e15efc 100644 --- a/controllers/api_v1_controllers.h +++ b/controllers/api_v1_controllers.h @@ -13,6 +13,7 @@ namespace api METHOD_ADD(controllers::get_all, "/", Get); METHOD_ADD(controllers::get_one_by_id, "/{1}", Get); METHOD_ADD(controllers::delete_one_by_id, "/{1}", Delete); + METHOD_ADD(controllers::put_one_by_id, "/{1}", Put, "filters::json_required", "filters::controllers::valid_json"); METHOD_ADD(controllers::get_relays_all, "/{1}/relays/", Get); METHOD_ADD(controllers::get_relays_one_by_id_and_num, "/{1}/relays/{2}", Get); @@ -23,6 +24,7 @@ namespace api static void get_all(const HttpRequestPtr& req, std::function &&callback); static void get_one_by_id(const HttpRequestPtr& req, std::function &&callback, const std::string& controller_id); static void delete_one_by_id(const HttpRequestPtr& req, std::function &&callback, const std::string& controller_id); + static void put_one_by_id(const HttpRequestPtr& req, std::function &&callback, const std::string& controller_id); static void get_relays_all(const HttpRequestPtr& req, std::function &&callback, const std::string& controller_id); static void get_relays_one_by_id_and_num(const HttpRequestPtr& req, std::function &&callback, const std::string& controller_id, int relay_num); diff --git a/filters/controllers_valid_json.cc b/filters/controllers_valid_json.cc new file mode 100644 index 0000000..fa44442 --- /dev/null +++ b/filters/controllers_valid_json.cc @@ -0,0 +1,28 @@ +#include +#include "controllers_valid_json.h" + +using namespace drogon; +using namespace filters::controllers; + +void valid_json::doFilter(const HttpRequestPtr &req, + FilterCallback &&fcb, + FilterChainCallback &&fccb) +{ + Json::Value body = *req->jsonObject(); + + bool is_valid = true; + + is_valid &= body["name"].type() == Json::ValueType::stringValue; + is_valid &= body["ip"].type() == Json::ValueType::stringValue; + + if(is_valid) + { + //Passed + fccb(); + return; + } + //Check failed + auto res = drogon::HttpResponse::newHttpResponse(); + res->setStatusCode(k400BadRequest); + fcb(res); +} diff --git a/filters/controllers_valid_json.h b/filters/controllers_valid_json.h new file mode 100644 index 0000000..2eb8d19 --- /dev/null +++ b/filters/controllers_valid_json.h @@ -0,0 +1,20 @@ +#pragma once + +#include +using namespace drogon; +namespace filters +{ + namespace controllers + { + + class valid_json : public HttpFilter + { + public: + valid_json() = default; + void doFilter(const HttpRequestPtr &req, + FilterCallback &&fcb, + FilterChainCallback &&fccb) override; + }; + + } +}