core-legacy/controllers/api_v1_devices.cc

69 lines
1.8 KiB
C++
Raw Normal View History

2019-07-14 22:39:37 +00:00
#include <netdb.h>
2019-07-19 09:41:39 +00:00
#include <models/device_dbo.h>
2019-07-14 22:39:37 +00:00
#include "api_v1_devices.h"
using namespace api::v1;
2019-07-19 09:41:39 +00:00
void
devices::get_all(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
{
device_dbo **all_devices = device_dbo::get_all();
Json::Value all_devices_json(Json::arrayValue);
for(int i = 0; all_devices[i] != nullptr; i++)
{
all_devices_json.append(all_devices[i]->to_json());
}
auto resp = HttpResponse::newHttpJsonResponse(all_devices_json);
callback(resp);
device_dbo::free_list(all_devices);
}
void
2019-07-20 12:51:45 +00:00
devices::get_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& device_id)
2019-07-19 09:41:39 +00:00
{
device_dbo **devices = device_dbo::get_by_simple("id", device_id.c_str(), (intptr_t) &sqlite3_bind_text);
2019-07-19 09:41:39 +00:00
if(devices[0])
2019-07-19 09:41:39 +00:00
{
auto resp = HttpResponse::newHttpJsonResponse(devices[0]->to_json());
2019-07-19 09:41:39 +00:00
callback(resp);
}
else
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k404NotFound);
callback(resp);
}
device_dbo::free_list(devices);
2019-07-19 09:41:39 +00:00
}
void
2019-07-20 12:51:45 +00:00
devices::delete_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& device_id)
2019-07-19 09:41:39 +00:00
{
device_dbo **devices = device_dbo::get_by_simple("id", device_id.c_str(), (intptr_t) &sqlite3_bind_text);
2019-07-19 09:41:39 +00:00
if(devices[0])
2019-07-19 09:41:39 +00:00
{
auto resp = HttpResponse::newHttpResponse();
if(!devices[0]->remove())
2019-07-19 09:41:39 +00:00
{
resp->setStatusCode(k500InternalServerError);
}
callback(resp);
}
else
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k404NotFound);
callback(resp);
}
device_dbo::free_list(devices);
2019-07-19 09:41:39 +00:00
}