core-legacy/controllers/api_v1_relays.cc
2020-04-28 21:50:19 +02:00

54 lines
1.4 KiB
C++

#include <netdb.h>
#include <models/relay_dbo.h>
#include <models/tag_dbo.h>
#include <models/junction_tag_dbo.h>
#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
relays::get_by_tag(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
const std::string &tag)
{
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)
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k500InternalServerError);
callback(resp);
}
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());
}
}
auto resp = HttpResponse::newHttpJsonResponse(relays_json);
callback(resp);
free(relays_ids);
}