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
|
|
|
|
devices::get_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, std::string device_id)
|
|
|
|
{
|
|
|
|
device_dbo *device = device_dbo::get_one_by("id", device_id.c_str());
|
|
|
|
|
|
|
|
if(device)
|
|
|
|
{
|
|
|
|
auto resp = HttpResponse::newHttpJsonResponse(device->to_json());
|
|
|
|
|
|
|
|
callback(resp);
|
|
|
|
|
|
|
|
free(device);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto resp = HttpResponse::newHttpResponse();
|
|
|
|
resp->setStatusCode(k404NotFound);
|
|
|
|
|
|
|
|
callback(resp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
devices::delete_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, std::string device_id)
|
|
|
|
{
|
|
|
|
device_dbo *device = device_dbo::get_one_by("id", device_id.c_str());
|
|
|
|
|
|
|
|
if(device)
|
|
|
|
{
|
|
|
|
auto resp = HttpResponse::newHttpResponse();
|
|
|
|
if(!device->remove())
|
|
|
|
{
|
|
|
|
resp->setStatusCode(k500InternalServerError);
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(resp);
|
|
|
|
|
|
|
|
free(device);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto resp = HttpResponse::newHttpResponse();
|
|
|
|
resp->setStatusCode(k404NotFound);
|
|
|
|
|
|
|
|
callback(resp);
|
|
|
|
}
|
|
|
|
}
|