ac61c26d56
add: improved sql calls
68 lines
1.8 KiB
C++
68 lines
1.8 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 **devices = device_dbo::get_by_simple("id", device_id.c_str(), (intptr_t) &sqlite3_bind_text);
|
|
|
|
if(devices[0])
|
|
{
|
|
auto resp = HttpResponse::newHttpJsonResponse(devices[0]->to_json());
|
|
|
|
callback(resp);
|
|
}
|
|
else
|
|
{
|
|
auto resp = HttpResponse::newHttpResponse();
|
|
resp->setStatusCode(k404NotFound);
|
|
|
|
callback(resp);
|
|
}
|
|
device_dbo::free_list(devices);
|
|
}
|
|
|
|
void
|
|
devices::delete_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& device_id)
|
|
{
|
|
device_dbo **devices = device_dbo::get_by_simple("id", device_id.c_str(), (intptr_t) &sqlite3_bind_text);
|
|
|
|
if(devices[0])
|
|
{
|
|
auto resp = HttpResponse::newHttpResponse();
|
|
if(!devices[0]->remove())
|
|
{
|
|
resp->setStatusCode(k500InternalServerError);
|
|
}
|
|
|
|
callback(resp);
|
|
}
|
|
else
|
|
{
|
|
auto resp = HttpResponse::newHttpResponse();
|
|
resp->setStatusCode(k404NotFound);
|
|
|
|
callback(resp);
|
|
}
|
|
device_dbo::free_list(devices);
|
|
}
|