core-legacy/controllers/api_v1_relays.cc

55 lines
1.4 KiB
C++
Raw Normal View History

2019-07-22 20:26:42 +00:00
#include <netdb.h>
#include <models/relay_dbo.h>
2020-04-28 19:50:19 +00:00
#include <models/tag_dbo.h>
#include <models/junction_tag_dbo.h>
2019-07-22 20:26:42 +00:00
#include <helpers.h>
#include "api_v1_relays.h"
using namespace api::v1;
void
relays::get_all(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&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());
}
auto resp = HttpResponse::newHttpJsonResponse(all_relays_json);
callback(resp);
relay_dbo::free_list(all_relays);
}
void
2020-04-28 19:50:19 +00:00
relays::get_by_tag(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
const std::string &tag)
{
2020-04-28 19:50:19 +00:00
int tag_id = tag_dbo::get_id(tag.c_str());
int *relays_ids = junction_tag_dbo::get_relays_for_tag_id(tag_id);
if(relays_ids == nullptr)
{
2020-04-28 19:50:19 +00:00
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k500InternalServerError);
callback(resp);
}
2020-04-28 19:50:19 +00:00
Json::Value relays_json(Json::arrayValue);
for(int i = 0; relays_ids[i] != 0; ++i)
{
relay_dbo *relay = relay_dbo::get_by_id(relays_ids[i]);
if(relay)
{
relays_json.append(relay->to_json());
}
}
2020-04-28 19:50:19 +00:00
auto resp = HttpResponse::newHttpJsonResponse(relays_json);
callback(resp);
free(relays_ids);
}