core-legacy/controllers/api_v1_devices.cc

71 lines
1.7 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 *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
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 *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);
}
}