add: tagging
This commit is contained in:
parent
5bc849ab40
commit
42c168e627
16 changed files with 749 additions and 161 deletions
|
@ -4,6 +4,8 @@
|
|||
#include <mpack.h>
|
||||
#include <models/controller_dbo.h>
|
||||
#include <models/schedule_dbo.h>
|
||||
#include <models/junction_tag_dbo.h>
|
||||
#include <models/tag_dbo.h>
|
||||
#include <config.h>
|
||||
#include <enums.h>
|
||||
#include "api_v1_controllers.h"
|
||||
|
@ -93,81 +95,105 @@ controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req,
|
|||
}
|
||||
|
||||
Json::Value body = *req->getJsonObject();
|
||||
uuid_t active_schedule_id;
|
||||
if(schedule_dbo::parse_uid(body["active_schedule"]["id"].asCString(), active_schedule_id))
|
||||
{
|
||||
LOG_DEBUG << "bad active_schedule uuid";
|
||||
auto resp = HttpResponse::newHttpResponse();
|
||||
resp->setStatusCode(k400BadRequest);
|
||||
callback(resp);
|
||||
return;
|
||||
}
|
||||
|
||||
uuid_t schedules_ids[7];
|
||||
for(int i = 0; i < 7; ++i)
|
||||
{
|
||||
if(schedule_dbo::parse_uid(body["schedules"][i]["id"].asCString(), schedules_ids[i]))
|
||||
{
|
||||
LOG_DEBUG << "parse_uid failed for schedule " << i;
|
||||
auto res = drogon::HttpResponse::newHttpResponse();
|
||||
res->setStatusCode(k400BadRequest);
|
||||
callback(res);
|
||||
return;
|
||||
}
|
||||
}
|
||||
bool set_name = body["name"].type() == Json::ValueType::stringValue;
|
||||
bool set_tags = body["tags"].type() == Json::ValueType::arrayValue;
|
||||
bool set_schedules = body["schedules"].type() == Json::ValueType::arrayValue;
|
||||
bool set_active_schedule = body["active_schedule"].type() == Json::ValueType::objectValue;
|
||||
|
||||
relay_dbo *relay = relay_dbo::get_relay_for_controller(controller_id, relay_num);
|
||||
|
||||
schedule_dbo **schedule_list = schedule_dbo::get_by_simple("uid", active_schedule_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
|
||||
schedule_dbo *active_schedule = schedule_list[0];
|
||||
|
||||
free(schedule_list);
|
||||
|
||||
schedule_dbo **schedule_list;
|
||||
schedule_dbo *active_schedule;
|
||||
schedule_dbo *schedules[7];
|
||||
for(int i = 0; i < 7; ++i)
|
||||
|
||||
if(set_schedules)
|
||||
{
|
||||
schedule_list = schedule_dbo::get_by_simple("uid", schedules_ids[i], (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
|
||||
schedules[i] = schedule_list[0];
|
||||
uuid_t schedules_ids[7];
|
||||
for(int i = 0; i < 7; ++i)
|
||||
{
|
||||
if(schedule_dbo::parse_uid(body["schedules"][i]["id"].asCString(), schedules_ids[i]))
|
||||
{
|
||||
LOG_DEBUG << "parse_uid failed for schedule " << i;
|
||||
auto res = drogon::HttpResponse::newHttpResponse();
|
||||
res->setStatusCode(k400BadRequest);
|
||||
callback(res);
|
||||
return;
|
||||
}
|
||||
}
|
||||
for(int i = 0; i < 7; ++i)
|
||||
{
|
||||
schedule_list = schedule_dbo::get_by_simple("uid", schedules_ids[i], (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
|
||||
schedules[i] = schedule_list[0];
|
||||
free(schedule_list);
|
||||
}
|
||||
}
|
||||
|
||||
if(set_active_schedule)
|
||||
{
|
||||
uuid_t active_schedule_id;
|
||||
if(schedule_dbo::parse_uid(body["active_schedule"]["id"].asCString(), active_schedule_id))
|
||||
{
|
||||
LOG_DEBUG << "bad active_schedule uuid";
|
||||
auto resp = HttpResponse::newHttpResponse();
|
||||
resp->setStatusCode(k400BadRequest);
|
||||
callback(resp);
|
||||
return;
|
||||
}
|
||||
schedule_list = schedule_dbo::get_by_simple("uid", active_schedule_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
|
||||
active_schedule = schedule_list[0];
|
||||
free(schedule_list);
|
||||
}
|
||||
|
||||
bool db_action_result;
|
||||
|
||||
if(relay)
|
||||
{
|
||||
strncpy(relay->name, body["name"].asCString(), 127);
|
||||
uuid_copy(relay->controller_id, controller_id);
|
||||
|
||||
for(int i = 0; i < 7; ++i)
|
||||
{
|
||||
relay->schedules[i] = schedules[i];
|
||||
}
|
||||
|
||||
relay->schedules[helpers::get_day_of_week()] = active_schedule;
|
||||
relay->active_schedule = active_schedule;
|
||||
|
||||
db_action_result = relay->update();
|
||||
}
|
||||
else
|
||||
if(!relay)
|
||||
{
|
||||
relay = new relay_dbo();
|
||||
relay->number = relay_num;
|
||||
strncpy(relay->name, body["name"].asCString(), 127);
|
||||
uuid_copy(relay->controller_id, controller_id);
|
||||
|
||||
for(int i = 0; i < 7; ++i)
|
||||
{
|
||||
relay->schedules[i] = schedule_dbo::get_by_id_or_off(0);
|
||||
}
|
||||
}
|
||||
|
||||
if(set_name)
|
||||
{
|
||||
strncpy(relay->name, body["name"].asCString(), 127);
|
||||
}
|
||||
|
||||
if(set_tags)
|
||||
{
|
||||
junction_tag_dbo::remove_for_relay(relay->id);
|
||||
|
||||
for(int i = 0; i < body["tags"].size(); ++i)
|
||||
{
|
||||
const char *tag = body["tags"][i].asCString();
|
||||
int tag_id = tag_dbo::get_id(tag);
|
||||
if(tag_id == 0)
|
||||
{
|
||||
tag_dbo::save(tag_id, tag);
|
||||
tag_id = tag_dbo::get_id(tag);
|
||||
}
|
||||
junction_tag_dbo::insert(tag_id, relay->id, 0);
|
||||
}
|
||||
}
|
||||
|
||||
if(set_schedules)
|
||||
{
|
||||
for(int i = 0; i < 7; ++i)
|
||||
{
|
||||
relay->schedules[i] = schedules[i];
|
||||
}
|
||||
|
||||
relay->schedules[helpers::get_day_of_week()] = active_schedule;
|
||||
relay->active_schedule = active_schedule;
|
||||
|
||||
uuid_copy(relay->controller_id, controller_id);
|
||||
|
||||
db_action_result = relay->insert();
|
||||
relay->active_schedule = schedules[helpers::get_day_of_week()];
|
||||
}
|
||||
|
||||
if(!db_action_result)
|
||||
if(set_active_schedule)
|
||||
{
|
||||
relay->schedules[helpers::get_day_of_week()] = active_schedule;
|
||||
relay->active_schedule = active_schedule;
|
||||
}
|
||||
|
||||
if(!relay->save())
|
||||
{
|
||||
auto resp = HttpResponse::newHttpResponse();
|
||||
resp->setStatusCode(k500InternalServerError);
|
||||
|
@ -206,7 +232,7 @@ controllers::put_relays_one_by_id_and_num(const HttpRequestPtr &req,
|
|||
mpack_write_u16(&writer, periods_count);
|
||||
|
||||
mpack_write_uint(&writer, COMMAND_MAPPING_SCHEDULE_ID);
|
||||
mpack_write_bin(&writer, (char*)schedules[0]->uid, sizeof(uuid_t));
|
||||
mpack_write_bin(&writer, (char*)relay->schedules[0]->uid, sizeof(uuid_t));
|
||||
|
||||
mpack_write_uint(&writer, COMMAND_MAPPING_PERIODS_BLOB);
|
||||
// periods + 1 to skip length in periods[0]
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
#include <netdb.h>
|
||||
#include <models/relay_dbo.h>
|
||||
#include <models/tag_dbo.h>
|
||||
#include <models/junction_tag_dbo.h>
|
||||
#include <helpers.h>
|
||||
#include "api_v1_relays.h"
|
||||
using namespace api::v1;
|
||||
|
@ -23,23 +25,30 @@ relays::get_all(const HttpRequestPtr &req, std::function<void(const HttpResponse
|
|||
}
|
||||
|
||||
void
|
||||
relays::get_one_by_tag(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
|
||||
const std::string &relay_tag)
|
||||
relays::get_by_tag(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
|
||||
const std::string &tag)
|
||||
{
|
||||
relay_dbo **relays = relay_dbo::get_by_simple("tag", relay_tag.c_str(), (intptr_t)&sqlite3_bind_text, -1);
|
||||
|
||||
if(relays[0])
|
||||
{
|
||||
auto resp = HttpResponse::newHttpJsonResponse(relays[0]->to_json());
|
||||
|
||||
callback(resp);
|
||||
}
|
||||
else
|
||||
int tag_id = tag_dbo::get_id(tag.c_str());
|
||||
int *relays_ids = junction_tag_dbo::get_relays_for_tag_id(tag_id);
|
||||
if(relays_ids == nullptr)
|
||||
{
|
||||
auto resp = HttpResponse::newHttpResponse();
|
||||
resp->setStatusCode(k404NotFound);
|
||||
|
||||
resp->setStatusCode(k500InternalServerError);
|
||||
callback(resp);
|
||||
}
|
||||
relay_dbo::free_list(relays);
|
||||
|
||||
Json::Value relays_json(Json::arrayValue);
|
||||
|
||||
for(int i = 0; relays_ids[i] != 0; ++i)
|
||||
{
|
||||
relay_dbo *relay = relay_dbo::get_by_id(relays_ids[i]);
|
||||
if(relay)
|
||||
{
|
||||
relays_json.append(relay->to_json());
|
||||
}
|
||||
}
|
||||
|
||||
auto resp = HttpResponse::newHttpJsonResponse(relays_json);
|
||||
callback(resp);
|
||||
free(relays_ids);
|
||||
}
|
||||
|
|
|
@ -8,10 +8,10 @@ namespace api::v1
|
|||
public:
|
||||
METHOD_LIST_BEGIN
|
||||
METHOD_ADD(relays::get_all, "/", Get, Options);
|
||||
METHOD_ADD(relays::get_one_by_tag, "/tag/{1}", Get, Options);
|
||||
METHOD_ADD(relays::get_by_tag, "/tag/{1}", Get, Options);
|
||||
METHOD_LIST_END
|
||||
|
||||
static void get_all(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback);
|
||||
static void get_one_by_tag(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& relay_tag);
|
||||
static void get_by_tag(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& tag);
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
#include <netdb.h>
|
||||
#include <models/schedule_dbo.h>
|
||||
#include <models/tag_dbo.h>
|
||||
#include <models/junction_tag_dbo.h>
|
||||
#include <helpers.h>
|
||||
#include "api_v1_schedules.h"
|
||||
using namespace api::v1;
|
||||
|
@ -101,6 +103,17 @@ schedules::post_new(const HttpRequestPtr &req, std::function<void(const HttpResp
|
|||
{
|
||||
Json::Value body = *req->jsonObject();
|
||||
|
||||
bool set_name = body["name"].type() == Json::ValueType::stringValue;
|
||||
bool set_tags = body["tags"].type() == Json::ValueType::arrayValue;
|
||||
//bool set_periods = body["periods"].type() == Json::ValueType::arrayValue;
|
||||
|
||||
if(!set_name || !set_name)
|
||||
{
|
||||
auto resp = HttpResponse::newHttpResponse();
|
||||
resp->setStatusCode(k400BadRequest);
|
||||
callback(resp);
|
||||
}
|
||||
|
||||
schedule_dbo new_schedule{};
|
||||
|
||||
strncpy(new_schedule.name, body["name"].asCString(), 127);
|
||||
|
@ -136,38 +149,93 @@ schedules::put_one_by_id(const HttpRequestPtr &req, std::function<void(const Htt
|
|||
}
|
||||
schedule_dbo **schedules = schedule_dbo::get_by_simple("uid", schedule_id, (intptr_t) &sqlite3_bind_blob, sizeof(uuid_t));
|
||||
|
||||
Json::Value body = *req->jsonObject();
|
||||
|
||||
if(schedules[0])
|
||||
{
|
||||
strncpy(schedules[0]->name, body["name"].asCString(), 127);
|
||||
schedules[0]->name[127] = '\0';
|
||||
|
||||
// if neither "off" nor "on" allow overwrite of periods
|
||||
if(strcmp(schedule_id_str.c_str(), "off") && strcmp(schedule_id_str.c_str(), "on"))
|
||||
{
|
||||
delete schedules[0]->periods;
|
||||
schedules[0]->periods = helpers::parse_periods(body["periods"]);
|
||||
}
|
||||
|
||||
if(!schedules[0]->update())
|
||||
{
|
||||
auto resp = HttpResponse::newHttpResponse();
|
||||
resp->setStatusCode(k500InternalServerError);
|
||||
callback(resp);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto resp = HttpResponse::newHttpJsonResponse(schedules[0]->to_json());
|
||||
callback(resp);
|
||||
}
|
||||
}
|
||||
else
|
||||
if(schedules[0] == nullptr)
|
||||
{
|
||||
auto resp = HttpResponse::newHttpResponse();
|
||||
resp->setStatusCode(k404NotFound);
|
||||
|
||||
callback(resp);
|
||||
}
|
||||
|
||||
Json::Value body = *req->jsonObject();
|
||||
|
||||
bool set_name = body["name"].type() == Json::ValueType::stringValue;
|
||||
bool set_tags = body["tags"].type() == Json::ValueType::arrayValue;
|
||||
bool set_periods = body["periods"].type() == Json::ValueType::arrayValue;
|
||||
|
||||
if(set_name)
|
||||
{
|
||||
strncpy(schedules[0]->name, body["name"].asCString(), 127);
|
||||
schedules[0]->name[127] = '\0';
|
||||
}
|
||||
|
||||
if(set_periods)
|
||||
{
|
||||
// if neither "off" nor "on" allow overwrite of periods
|
||||
if(strcmp(schedule_id_str.c_str(), "off") && strcmp(schedule_id_str.c_str(), "on"))
|
||||
{
|
||||
delete schedules[0]->periods;
|
||||
schedules[0]->periods = helpers::parse_periods(body["periods"]);
|
||||
}
|
||||
}
|
||||
|
||||
if(set_tags)
|
||||
{
|
||||
junction_tag_dbo::remove_for_schedule(schedules[0]->id);
|
||||
|
||||
for(int i = 0; i < body["tags"].size(); ++i)
|
||||
{
|
||||
const char *tag = body["tags"][i].asCString();
|
||||
int tag_id = tag_dbo::get_id(tag);
|
||||
if(tag_id == 0)
|
||||
{
|
||||
tag_dbo::save(tag_id, tag);
|
||||
tag_id = tag_dbo::get_id(tag);
|
||||
}
|
||||
junction_tag_dbo::insert(tag_id, 0, schedules[0]->id);
|
||||
}
|
||||
}
|
||||
|
||||
if(!schedules[0]->update())
|
||||
{
|
||||
auto resp = HttpResponse::newHttpResponse();
|
||||
resp->setStatusCode(k500InternalServerError);
|
||||
callback(resp);
|
||||
}
|
||||
else
|
||||
{
|
||||
auto resp = HttpResponse::newHttpJsonResponse(schedules[0]->to_json());
|
||||
callback(resp);
|
||||
}
|
||||
|
||||
schedule_dbo::free_list(schedules);
|
||||
}
|
||||
|
||||
void
|
||||
schedules::get_by_tag(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
|
||||
const std::string &tag)
|
||||
{
|
||||
int tag_id = tag_dbo::get_id(tag.c_str());
|
||||
int *schedules_ids = junction_tag_dbo::get_schedules_for_tag_id(tag_id);
|
||||
if(schedules_ids == nullptr)
|
||||
{
|
||||
auto resp = HttpResponse::newHttpResponse();
|
||||
resp->setStatusCode(k500InternalServerError);
|
||||
callback(resp);
|
||||
}
|
||||
|
||||
Json::Value schedules_json(Json::arrayValue);
|
||||
|
||||
for(int i = 0; schedules_ids[i] != 0; ++i)
|
||||
{
|
||||
schedule_dbo *schedule = schedule_dbo::get_by_id(schedules_ids[i]);
|
||||
if(schedule)
|
||||
{
|
||||
schedules_json.append(schedule->to_json());
|
||||
}
|
||||
}
|
||||
|
||||
auto resp = HttpResponse::newHttpJsonResponse(schedules_json);
|
||||
callback(resp);
|
||||
free(schedules_ids);
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ namespace api::v1
|
|||
METHOD_ADD(schedules::get_one_by_id, "/{1}", Get, Options);
|
||||
METHOD_ADD(schedules::delete_one_by_id, "/{1}", Delete, Options);
|
||||
METHOD_ADD(schedules::put_one_by_id, "/{1}", Put, Options, "filters::json_required", "filters::schedules::valid_json");
|
||||
METHOD_ADD(schedules::get_by_tag, "/tag/{1}", Get, Options);
|
||||
//METHOD_ADD(controllers::get_relays_all,"/{1}/relays",Get);
|
||||
//METHOD_ADD(controllers::get_relays_one,"/{1}/relays/{2}",Get);
|
||||
METHOD_LIST_END
|
||||
|
@ -21,6 +22,7 @@ namespace api::v1
|
|||
static void get_one_by_id(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& schedule_id);
|
||||
static void delete_one_by_id(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& schedule_id);
|
||||
static void put_one_by_id(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& schedule_id);
|
||||
static void get_by_tag(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback, const std::string& tag);
|
||||
//void get_relays_all(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback,std::string schedule_id);
|
||||
//void get_relays_one(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback,std::string schedule_id,std::string relay_id);
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue