add: tagging

This commit is contained in:
Tobias Reisinger 2020-04-28 21:50:19 +02:00
parent 5bc849ab40
commit 42c168e627
16 changed files with 749 additions and 161 deletions

View file

@ -4,6 +4,8 @@
#include <mpack.h> #include <mpack.h>
#include <models/controller_dbo.h> #include <models/controller_dbo.h>
#include <models/schedule_dbo.h> #include <models/schedule_dbo.h>
#include <models/junction_tag_dbo.h>
#include <models/tag_dbo.h>
#include <config.h> #include <config.h>
#include <enums.h> #include <enums.h>
#include "api_v1_controllers.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(); Json::Value body = *req->getJsonObject();
uuid_t active_schedule_id; bool set_name = body["name"].type() == Json::ValueType::stringValue;
if(schedule_dbo::parse_uid(body["active_schedule"]["id"].asCString(), active_schedule_id)) bool set_tags = body["tags"].type() == Json::ValueType::arrayValue;
{ bool set_schedules = body["schedules"].type() == Json::ValueType::arrayValue;
LOG_DEBUG << "bad active_schedule uuid"; bool set_active_schedule = body["active_schedule"].type() == Json::ValueType::objectValue;
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;
}
}
relay_dbo *relay = relay_dbo::get_relay_for_controller(controller_id, relay_num); 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 **schedule_list;
schedule_dbo *active_schedule = schedule_list[0]; schedule_dbo *active_schedule;
free(schedule_list);
schedule_dbo *schedules[7]; 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)); uuid_t schedules_ids[7];
schedules[i] = schedule_list[0]; 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); free(schedule_list);
} }
bool db_action_result; if(!relay)
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
{ {
relay = new relay_dbo(); relay = new relay_dbo();
relay->number = relay_num; 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) for(int i = 0; i < 7; ++i)
{ {
relay->schedules[i] = schedules[i]; relay->schedules[i] = schedules[i];
} }
relay->active_schedule = schedules[helpers::get_day_of_week()];
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();
} }
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(); auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k500InternalServerError); 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_u16(&writer, periods_count);
mpack_write_uint(&writer, COMMAND_MAPPING_SCHEDULE_ID); 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); mpack_write_uint(&writer, COMMAND_MAPPING_PERIODS_BLOB);
// periods + 1 to skip length in periods[0] // periods + 1 to skip length in periods[0]

View file

@ -1,5 +1,7 @@
#include <netdb.h> #include <netdb.h>
#include <models/relay_dbo.h> #include <models/relay_dbo.h>
#include <models/tag_dbo.h>
#include <models/junction_tag_dbo.h>
#include <helpers.h> #include <helpers.h>
#include "api_v1_relays.h" #include "api_v1_relays.h"
using namespace api::v1; using namespace api::v1;
@ -23,23 +25,30 @@ relays::get_all(const HttpRequestPtr &req, std::function<void(const HttpResponse
} }
void void
relays::get_one_by_tag(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, relays::get_by_tag(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
const std::string &relay_tag) const std::string &tag)
{ {
relay_dbo **relays = relay_dbo::get_by_simple("tag", relay_tag.c_str(), (intptr_t)&sqlite3_bind_text, -1); 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[0]) if(relays_ids == nullptr)
{
auto resp = HttpResponse::newHttpJsonResponse(relays[0]->to_json());
callback(resp);
}
else
{ {
auto resp = HttpResponse::newHttpResponse(); auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k404NotFound); resp->setStatusCode(k500InternalServerError);
callback(resp); 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);
} }

View file

@ -8,10 +8,10 @@ namespace api::v1
public: public:
METHOD_LIST_BEGIN METHOD_LIST_BEGIN
METHOD_ADD(relays::get_all, "/", Get, Options); 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 METHOD_LIST_END
static void get_all(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback); 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);
}; };
} }

View file

@ -1,5 +1,7 @@
#include <netdb.h> #include <netdb.h>
#include <models/schedule_dbo.h> #include <models/schedule_dbo.h>
#include <models/tag_dbo.h>
#include <models/junction_tag_dbo.h>
#include <helpers.h> #include <helpers.h>
#include "api_v1_schedules.h" #include "api_v1_schedules.h"
using namespace api::v1; using namespace api::v1;
@ -101,6 +103,17 @@ schedules::post_new(const HttpRequestPtr &req, std::function<void(const HttpResp
{ {
Json::Value body = *req->jsonObject(); 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{}; schedule_dbo new_schedule{};
strncpy(new_schedule.name, body["name"].asCString(), 127); 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)); 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] == nullptr)
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
{ {
auto resp = HttpResponse::newHttpResponse(); auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k404NotFound); resp->setStatusCode(k404NotFound);
callback(resp); 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); 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);
}

View file

@ -12,6 +12,7 @@ namespace api::v1
METHOD_ADD(schedules::get_one_by_id, "/{1}", Get, Options); METHOD_ADD(schedules::get_one_by_id, "/{1}", Get, Options);
METHOD_ADD(schedules::delete_one_by_id, "/{1}", Delete, 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::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_all,"/{1}/relays",Get);
//METHOD_ADD(controllers::get_relays_one,"/{1}/relays/{2}",Get); //METHOD_ADD(controllers::get_relays_one,"/{1}/relays/{2}",Get);
METHOD_LIST_END 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 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 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 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_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); //void get_relays_one(const HttpRequestPtr& req,std::function<void (const HttpResponsePtr &)> &&callback,std::string schedule_id,std::string relay_id);
}; };

View file

@ -19,26 +19,43 @@ void valid_json::doFilter(const HttpRequestPtr &req,
bool is_valid = true; bool is_valid = true;
// level 1 // level 1
is_valid &= body["name"].type() == Json::ValueType::stringValue; bool set_name = body["name"].type() == Json::ValueType::stringValue;
is_valid &= body["active_schedule"].type() == Json::ValueType::objectValue; bool set_tags = body["tags"].type() == Json::ValueType::arrayValue;
is_valid &= body["schedules"].type() == Json::ValueType::arrayValue; bool set_schedules = body["schedules"].type() == Json::ValueType::arrayValue;
bool set_active_schedule = body["active_schedule"].type() == Json::ValueType::objectValue;
// level 2 // level 2
if(is_valid) if(is_valid)
{ {
is_valid &= body["active_schedule"]["id"].type() == Json::ValueType::stringValue; if(set_tags)
for(int i = 0; i < 7; ++i)
{ {
is_valid &= body["schedules"][i].type() == Json::ValueType::objectValue; for(int i = 0; i < body["tags"].size(); ++i)
{
is_valid &= body["tags"][i].type() == Json::ValueType::stringValue;
}
}
if(set_active_schedule)
{
is_valid &= body["active_schedule"]["id"].type() == Json::ValueType::stringValue;
}
if(set_schedules)
{
for(int i = 0; i < 7; ++i)
{
is_valid &= body["schedules"][i].type() == Json::ValueType::objectValue;
}
} }
} }
// level 3 // level 3
if(is_valid) if(is_valid)
{ {
for(int i = 0; i < 7; ++i) if(set_schedules)
{ {
is_valid &= body["schedules"][i]["id"].type() == Json::ValueType::stringValue; for(int i = 0; i < 7; ++i)
{
is_valid &= body["schedules"][i]["id"].type() == Json::ValueType::stringValue;
}
} }
} }
@ -51,46 +68,23 @@ void valid_json::doFilter(const HttpRequestPtr &req,
return; return;
} }
uuid_t active_schedule_id; if(set_active_schedule)
if(schedule_dbo::parse_uid(body["active_schedule"]["id"].asCString(), active_schedule_id))
{ {
LOG_DEBUG << "parse_uid failed for active_schedule"; uuid_t active_schedule_id;
auto res = drogon::HttpResponse::newHttpResponse(); if(schedule_dbo::parse_uid(body["active_schedule"]["id"].asCString(), active_schedule_id))
res->setStatusCode(k400BadRequest);
fcb(res);
return;
}
schedule_dbo **schedules = schedule_dbo::get_by_simple("uid", active_schedule_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
bool schedule_found = schedules[0] != nullptr;
schedule_dbo::free_list(schedules);
if(!schedule_found)
{
LOG_DEBUG << "could not find active_schedule";
auto res = drogon::HttpResponse::newHttpResponse();
res->setStatusCode(k400BadRequest);
fcb(res);
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; LOG_DEBUG << "parse_uid failed for active_schedule";
auto res = drogon::HttpResponse::newHttpResponse(); auto res = drogon::HttpResponse::newHttpResponse();
res->setStatusCode(k400BadRequest); res->setStatusCode(k400BadRequest);
fcb(res); fcb(res);
return; return;
} }
schedule_dbo **schedules = schedule_dbo::get_by_simple("uid", active_schedule_id, (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
schedules = schedule_dbo::get_by_simple("uid", schedules_ids[i], (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
bool schedule_found = schedules[0] != nullptr; bool schedule_found = schedules[0] != nullptr;
schedule_dbo::free_list(schedules); schedule_dbo::free_list(schedules);
if(!schedule_found) if(!schedule_found)
{ {
LOG_DEBUG << "could not find schedule " << i; LOG_DEBUG << "could not find active_schedule";
auto res = drogon::HttpResponse::newHttpResponse(); auto res = drogon::HttpResponse::newHttpResponse();
res->setStatusCode(k400BadRequest); res->setStatusCode(k400BadRequest);
fcb(res); fcb(res);
@ -98,6 +92,34 @@ void valid_json::doFilter(const HttpRequestPtr &req,
} }
} }
if(set_schedules)
{
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);
fcb(res);
return;
}
schedule_dbo **schedules = schedule_dbo::get_by_simple("uid", schedules_ids[i], (intptr_t)&sqlite3_bind_blob, sizeof(uuid_t));
bool schedule_found = schedules[0] != nullptr;
schedule_dbo::free_list(schedules);
if(!schedule_found)
{
LOG_DEBUG << "could not find schedule " << i;
auto res = drogon::HttpResponse::newHttpResponse();
res->setStatusCode(k400BadRequest);
fcb(res);
return;
}
}
}
//Passed //Passed
fccb(); fccb();
return; return;

View file

@ -17,8 +17,41 @@ void valid_json::doFilter(const HttpRequestPtr &req,
bool is_valid = true; bool is_valid = true;
is_valid &= body["name"].type() == Json::ValueType::stringValue; bool set_name = body["name"].type() == Json::ValueType::stringValue;
is_valid &= body["periods"].type() == Json::ValueType::arrayValue; bool set_tags = body["tags"].type() == Json::ValueType::arrayValue;
bool set_periods = body["periods"].type() == Json::ValueType::arrayValue;
// level 2
if(is_valid)
{
if(set_tags)
{
for(int i = 0; i < body["tags"].size(); ++i)
{
is_valid &= body["tags"][i].type() == Json::ValueType::stringValue;
}
}
if(set_periods)
{
for(int i = 0; i < body["periods"].size(); ++i)
{
is_valid &= body["periods"][i].type() == Json::ValueType::objectValue;
}
}
}
// level 3
if(is_valid)
{
if(set_periods)
{
for(int i = 0; i < body["periods"].size(); ++i)
{
is_valid &= body["periods"][i]["start"].type() == Json::ValueType::stringValue;
is_valid &= body["periods"][i]["end"].type() == Json::ValueType::stringValue;
}
}
}
if(is_valid) if(is_valid)
{ {

174
models/junction_tag_dbo.cc Normal file
View file

@ -0,0 +1,174 @@
#include <cstdio>
#include <cstring>
#include <trantor/utils/Logger.h>
#include <helpers.h>
#include "junction_tag_dbo.h"
#include "globals.h"
bool
junction_tag_dbo::insert(int tag_id, int relay_id, int schedule_id)
{
int rc;
sqlite3_stmt *stmt;
sqlite3_prepare_v2(globals::db, "INSERT INTO junction_tag(tag_id, schedule_id, relay_id) values (?1, ?2, ?3);", -1, &stmt, nullptr);
sqlite3_bind_int(stmt, 1, tag_id);
sqlite3_bind_int(stmt, 2, schedule_id);
sqlite3_bind_int(stmt, 3, relay_id);
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE)
{
printf("ERROR inserting data: %s\n", sqlite3_errmsg(globals::db));
return false;
}
sqlite3_finalize(stmt);
return true;
}
static int*
get_ids(sqlite3_stmt *stmt)
{
auto *ids = (int*)malloc(sizeof(int));
int new_id;
int row = 0;
while(true)
{
int s;
s = sqlite3_step(stmt);
if (s == SQLITE_ROW)
{
new_id = sqlite3_column_int(stmt, 0);
row++;
ids = (int*)realloc(ids, sizeof(int) * (row + 1));
ids[row - 1] = new_id;
}
else
{
if (s == SQLITE_DONE)
{
break;
}
else
{
LOG_ERROR << "Error Selecting relays from database: " << sqlite3_errstr(s);
sqlite3_finalize(stmt);
return nullptr;
}
}
}
sqlite3_finalize(stmt);
ids[row] = 0;
return ids;
}
int*
junction_tag_dbo::get_relays_for_tag_id(int tag_id)
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(globals::db, "SELECT relay_id FROM junction_tag WHERE tag_id=?1;", -1, &stmt, nullptr);
sqlite3_bind_int(stmt, 1, tag_id);
return get_ids(stmt);
}
int*
junction_tag_dbo::get_schedules_for_tag_id(int tag_id)
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(globals::db, "SELECT schedule_id FROM junction_tag WHERE tag_id=?1;", -1, &stmt, nullptr);
sqlite3_bind_int(stmt, 1, tag_id);
return get_ids(stmt);
}
int*
junction_tag_dbo::get_tags_for_relay_id(int relay_id)
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(globals::db, "SELECT tag_id FROM junction_tag WHERE relay_id=?1;", -1, &stmt, nullptr);
sqlite3_bind_int(stmt, 1, relay_id);
return get_ids(stmt);
}
int*
junction_tag_dbo::get_tags_for_schedule_id(int schedule_id)
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(globals::db, "SELECT tag_id FROM junction_tag WHERE schedule_id=?1;", -1, &stmt, nullptr);
sqlite3_bind_int(stmt, 1, schedule_id);
return get_ids(stmt);
}
bool
junction_tag_dbo::remove(int tag_id, int relay_id, int schedule_id)
{
sqlite3_stmt *stmt;
int rc;
sqlite3_prepare_v2(globals::db, "DELETE FROM junction_tag WHERE tag_id=?1 AND schedule_id=?2 AND relay_id=?3;", -1, &stmt, nullptr);
sqlite3_bind_int(stmt, 1, tag_id);
sqlite3_bind_int(stmt, 2, schedule_id);
sqlite3_bind_int(stmt, 3, relay_id);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return rc == SQLITE_DONE;
}
bool
junction_tag_dbo::remove_for_tag(int tag_id)
{
sqlite3_stmt *stmt;
int rc;
sqlite3_prepare_v2(globals::db, "DELETE FROM junction_tag WHERE tag_id=?1;", -1, &stmt, nullptr);
sqlite3_bind_int(stmt, 1, tag_id);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return rc == SQLITE_DONE;
}
bool
junction_tag_dbo::remove_for_relay(int relay_id)
{
sqlite3_stmt *stmt;
int rc;
sqlite3_prepare_v2(globals::db, "DELETE FROM junction_tag WHERE relay_id=?1;", -1, &stmt, nullptr);
sqlite3_bind_int(stmt, 1, relay_id);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return rc == SQLITE_DONE;
}
bool
junction_tag_dbo::remove_for_schedule(int schedule_id)
{
sqlite3_stmt *stmt;
int rc;
sqlite3_prepare_v2(globals::db, "DELETE FROM junction_tag WHERE schedule_id=?1;", -1, &stmt, nullptr);
sqlite3_bind_int(stmt, 1, schedule_id);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return rc == SQLITE_DONE;
}

41
models/junction_tag_dbo.h Normal file
View file

@ -0,0 +1,41 @@
#ifndef EMGAUWA_CORE_JUNCTION_TAG_DBO_H
#define EMGAUWA_CORE_JUNCTION_TAG_DBO_H
#include <string>
#include <sqlite3.h>
#include <json/value.h>
#include <uuid/uuid.h>
#include <helpers.h>
namespace junction_tag_dbo
{
int*
get_relays_for_tag_id(int tag_id);
int*
get_schedules_for_tag_id(int tag_id);
int*
get_tags_for_relay_id(int relay_id);
int*
get_tags_for_schedule_id(int schedule_id);
bool
insert(int tag_id, int relay_id, int schedule_id);
bool
remove(int tag_id, int relay_id, int schedule_id);
bool
remove_for_tag(int tag_id);
bool
remove_for_relay(int relay_id);
bool
remove_for_schedule(int schedule_id);
};
#endif //EMGAUWA_CORE_JUNCTION_TAG_DBO_H

View file

@ -5,6 +5,8 @@
#include "relay_dbo.h" #include "relay_dbo.h"
#include "globals.h" #include "globals.h"
#include "junction_relay_schedule_dbo.h" #include "junction_relay_schedule_dbo.h"
#include "junction_tag_dbo.h"
#include "tag_dbo.h"
#include "controller_dbo.h" #include "controller_dbo.h"
#include "schedule_dbo.h" #include "schedule_dbo.h"
@ -117,21 +119,17 @@ relay_db_select(sqlite3_stmt *stmt)
} }
bool bool
relay_dbo::update() relay_dbo::save()
{ {
sqlite3_stmt *stmt; sqlite3_stmt *stmt;
if(this->id)
sqlite3_prepare_v2(globals::db, "UPDATE relays set number = ?2, name = ?3, controller_id = ?4 WHERE id = ?1;", -1, &stmt, nullptr); {
sqlite3_prepare_v2(globals::db, "UPDATE relays set number = ?2, name = ?3, controller_id = ?4 WHERE id = ?1;", -1, &stmt, nullptr);
return relay_db_update_insert(this, stmt); }
} else
{
bool sqlite3_prepare_v2(globals::db, "INSERT INTO relays(number, name, controller_id) values (?2, ?3, ?4);", -1, &stmt, nullptr);
relay_dbo::insert() }
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(globals::db, "INSERT INTO relays(number, name, controller_id) values (?2, ?3, ?4);", -1, &stmt, nullptr);
return relay_db_update_insert(this, stmt); return relay_db_update_insert(this, stmt);
} }
@ -175,6 +173,24 @@ relay_dbo::to_json()
relay_json["active_schedule"] = this->active_schedule->to_json(); relay_json["active_schedule"] = this->active_schedule->to_json();
relay_json["schedules"] = schedules_json; relay_json["schedules"] = schedules_json;
int *tags_ids = junction_tag_dbo::get_tags_for_relay_id(this->id);
if(tags_ids != nullptr)
{
Json::Value tags_json(Json::arrayValue);
int tags_count;
for(tags_count = 0; tags_ids[tags_count] != 0; ++tags_count);
char **tags = (char**)malloc(sizeof(char*) * tags_count);
for(int i = 0; i < tags_count; ++i)
{
tags[i] = tag_dbo::get_tag(tags_ids[i]);
tags_json[i] = tags[i];
}
relay_json["tags"] = tags_json;
}
return relay_json; return relay_json;
} }
@ -213,6 +229,17 @@ relay_dbo::get_by(helpers::sql_filter_builder **filters)
return relay_db_select(stmt); return relay_db_select(stmt);
} }
relay_dbo*
relay_dbo::get_by_id(int id)
{
relay_dbo **relays = relay_dbo::get_by_simple("id", &id, (intptr_t)&sqlite3_bind_int, 0);
relay_dbo *result = relays[0];
free(relays);
return result;
}
relay_dbo* relay_dbo*
relay_dbo::get_relay_for_controller(uuid_t controller_id, int relay_num) relay_dbo::get_relay_for_controller(uuid_t controller_id, int relay_num)
{ {

View file

@ -24,10 +24,7 @@ public:
reload_active_schedule(); reload_active_schedule();
bool bool
update(); save();
bool
insert();
bool bool
remove(); remove();
@ -42,7 +39,7 @@ public:
get_by_simple(const char *key, const void *value, intptr_t bind_func, int bind_func_param); get_by_simple(const char *key, const void *value, intptr_t bind_func, int bind_func_param);
static relay_dbo* static relay_dbo*
get_by_id_or_off(const int id); get_by_id(int id);
static relay_dbo** static relay_dbo**
get_by(helpers::sql_filter_builder **filters); get_by(helpers::sql_filter_builder **filters);

View file

@ -2,6 +2,8 @@
#include <trantor/utils/Logger.h> #include <trantor/utils/Logger.h>
#include <helpers.h> #include <helpers.h>
#include "schedule_dbo.h" #include "schedule_dbo.h"
#include "junction_tag_dbo.h"
#include "tag_dbo.h"
#include "globals.h" #include "globals.h"
#include "period.h" #include "period.h"
@ -151,6 +153,24 @@ schedule_dbo::to_json()
schedule_json["id"] = id_str; schedule_json["id"] = id_str;
schedule_json["periods"] = this->periods->to_json(); schedule_json["periods"] = this->periods->to_json();
int *tags_ids = junction_tag_dbo::get_tags_for_schedule_id(this->id);
if(tags_ids != nullptr)
{
Json::Value tags_json(Json::arrayValue);
int tags_count;
for(tags_count = 0; tags_ids[tags_count] != 0; ++tags_count);
char **tags = (char**)malloc(sizeof(char*) * tags_count);
for(int i = 0; i < tags_count; ++i)
{
tags[i] = tag_dbo::get_tag(tags_ids[i]);
tags_json[i] = tags[i];
}
schedule_json["tags"] = tags_json;
}
return schedule_json; return schedule_json;
} }
@ -184,7 +204,18 @@ schedule_dbo::get_by_simple(const char *key, const void *value, intptr_t bind_fu
} }
schedule_dbo* schedule_dbo*
schedule_dbo::get_by_id_or_off(const int id) schedule_dbo::get_by_id(int id)
{
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", &id, (intptr_t)&sqlite3_bind_int, 0);
schedule_dbo *result = schedules[0];
free(schedules);
return result;
}
schedule_dbo*
schedule_dbo::get_by_id_or_off(int id)
{ {
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", &id, (intptr_t)&sqlite3_bind_int, 0); schedule_dbo **schedules = schedule_dbo::get_by_simple("id", &id, (intptr_t)&sqlite3_bind_int, 0);

View file

@ -39,7 +39,10 @@ public:
get_by_simple(const char *key, const void *value, intptr_t bind_func, int bind_func_param); get_by_simple(const char *key, const void *value, intptr_t bind_func, int bind_func_param);
static schedule_dbo* static schedule_dbo*
get_by_id_or_off(const int id); get_by_id_or_off(int id);
static schedule_dbo*
get_by_id(int id);
static schedule_dbo** static schedule_dbo**
get_by(helpers::sql_filter_builder **filters); get_by(helpers::sql_filter_builder **filters);

131
models/tag_dbo.cc Normal file
View file

@ -0,0 +1,131 @@
#include <cstdio>
#include <cstring>
#include <trantor/utils/Logger.h>
#include <helpers.h>
#include "tag_dbo.h"
#include "globals.h"
bool
tag_dbo::save(int id, const char *tag)
{
int rc;
sqlite3_stmt *stmt;
if(id)
{
sqlite3_prepare_v2(globals::db, "UPDATE tags SET tag = ?2 WHERE id = ?1;", -1, &stmt, nullptr);
}
else
{
sqlite3_prepare_v2(globals::db, "INSERT INTO tags(tag) values (?2);", -1, &stmt, nullptr);
}
sqlite3_bind_int(stmt, 1, id);
sqlite3_bind_text(stmt, 2, tag, -1, SQLITE_STATIC);
rc = sqlite3_step(stmt);
if (rc != SQLITE_DONE)
{
printf("ERROR saving tag: %s\n", sqlite3_errmsg(globals::db));
return false;
}
sqlite3_finalize(stmt);
return true;
}
char*
tag_dbo::get_tag(int id)
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(globals::db, "SELECT tag FROM tags WHERE id=?1;", -1, &stmt, nullptr);
sqlite3_bind_int(stmt, 1, id);
char *result = nullptr;
while(true)
{
int s;
s = sqlite3_step(stmt);
if (s == SQLITE_ROW)
{
const char *found_tag = (const char *)sqlite3_column_text(stmt, 0);
result = (char*)malloc(sizeof(char) * (strlen(found_tag) + 1));
strcpy(result, found_tag);
}
else
{
if (s == SQLITE_DONE)
{
break;
}
else
{
LOG_ERROR << "Error Selecting tags from database: " << sqlite3_errstr(s);
sqlite3_finalize(stmt);
return nullptr;
}
}
}
sqlite3_finalize(stmt);
return result;
}
int
tag_dbo::get_id(const char *tag)
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(globals::db, "SELECT id FROM tags WHERE tag=?1;", -1, &stmt, nullptr);
sqlite3_bind_text(stmt, 1, tag, -1, SQLITE_STATIC);
int result = 0;
while(true)
{
int s;
s = sqlite3_step(stmt);
if (s == SQLITE_ROW)
{
result = sqlite3_column_int(stmt, 0);
}
else
{
if (s == SQLITE_DONE)
{
break;
}
else
{
LOG_ERROR << "Error Selecting tags from database: " << sqlite3_errstr(s);
sqlite3_finalize(stmt);
return 0;
}
}
}
sqlite3_finalize(stmt);
return result;
}
bool
tag_dbo::remove(int id)
{
sqlite3_stmt *stmt;
int rc;
sqlite3_prepare_v2(globals::db, "DELETE FROM tags WHERE id=?1;", -1, &stmt, nullptr);
sqlite3_bind_int(stmt, 1, id);
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
return rc == SQLITE_DONE;
}

23
models/tag_dbo.h Normal file
View file

@ -0,0 +1,23 @@
#ifndef EMGAUWA_CORE_TAG_DBO_H
#define EMGAUWA_CORE_TAG_DBO_H
#include <string>
#include <sqlite3.h>
namespace tag_dbo
{
bool
save(int id, const char *tag);
bool
remove(int id);
char*
get_tag(int id);
int
get_id(const char* tag);
};
#endif //EMGAUWA_CORE_TAG_DBO_H

View file

@ -44,13 +44,14 @@ create table schedules
); );
create table tags create table tags
(
id INTEGER id INTEGER
PRIMARY KEY PRIMARY KEY
AUTOINCREMENT, AUTOINCREMENT,
tag VARCHAR(128) tag VARCHAR(128)
NOT NULL NOT NULL
UNIQUE UNIQUE
) );
create table junction_tag create table junction_tag
( (