add: put controller api endpoint
This commit is contained in:
parent
0247031a3d
commit
831291cdac
4 changed files with 88 additions and 0 deletions
|
@ -66,3 +66,41 @@ controllers::delete_one_by_id(const HttpRequestPtr &req, std::function<void(cons
|
||||||
}
|
}
|
||||||
controller_dbo::free_list(controllers);
|
controller_dbo::free_list(controllers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
controllers::put_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&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);
|
||||||
|
}
|
|
@ -13,6 +13,7 @@ namespace api
|
||||||
METHOD_ADD(controllers::get_all, "/", Get);
|
METHOD_ADD(controllers::get_all, "/", Get);
|
||||||
METHOD_ADD(controllers::get_one_by_id, "/{1}", Get);
|
METHOD_ADD(controllers::get_one_by_id, "/{1}", Get);
|
||||||
METHOD_ADD(controllers::delete_one_by_id, "/{1}", Delete);
|
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_all, "/{1}/relays/", Get);
|
||||||
METHOD_ADD(controllers::get_relays_one_by_id_and_num, "/{1}/relays/{2}", 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<void (const HttpResponsePtr &)> &&callback);
|
static void get_all(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback);
|
||||||
static void get_one_by_id(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id);
|
static void get_one_by_id(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id);
|
||||||
static void delete_one_by_id(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id);
|
static void delete_one_by_id(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id);
|
||||||
|
static void put_one_by_id(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id);
|
||||||
|
|
||||||
static void get_relays_all(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id);
|
static void get_relays_all(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id);
|
||||||
static void get_relays_one_by_id_and_num(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id, int relay_num);
|
static void get_relays_one_by_id_and_num(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& controller_id, int relay_num);
|
||||||
|
|
28
filters/controllers_valid_json.cc
Normal file
28
filters/controllers_valid_json.cc
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
#include <models/relay_dbo.h>
|
||||||
|
#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);
|
||||||
|
}
|
20
filters/controllers_valid_json.h
Normal file
20
filters/controllers_valid_json.h
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <drogon/HttpFilter.h>
|
||||||
|
using namespace drogon;
|
||||||
|
namespace filters
|
||||||
|
{
|
||||||
|
namespace controllers
|
||||||
|
{
|
||||||
|
|
||||||
|
class valid_json : public HttpFilter<valid_json>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
valid_json() = default;
|
||||||
|
void doFilter(const HttpRequestPtr &req,
|
||||||
|
FilterCallback &&fcb,
|
||||||
|
FilterChainCallback &&fccb) override;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue