a38a6e63b3
add: schedule api calls
70 lines
1.7 KiB
C++
70 lines
1.7 KiB
C++
#include <netdb.h>
|
|
#include <models/device_dbo.h>
|
|
#include "api_v1_devices.h"
|
|
using namespace api::v1;
|
|
|
|
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, const 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, const 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);
|
|
}
|
|
}
|