core-legacy/controllers/api_v1_schedules.cc

278 lines
8 KiB
C++
Raw Normal View History

2019-07-19 09:41:39 +00:00
#include <netdb.h>
#include <models/schedule_dbo.h>
2020-04-28 19:50:19 +00:00
#include <models/tag_dbo.h>
#include <models/junction_tag_dbo.h>
2019-07-19 09:41:39 +00:00
#include <helpers.h>
#include "api_v1_schedules.h"
using namespace api::v1;
void
schedules::get_all(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
{
schedule_dbo **all_schedules = schedule_dbo::get_all();
Json::Value all_schedules_json(Json::arrayValue);
for(int i = 0; all_schedules[i] != nullptr; i++)
{
all_schedules_json.append(all_schedules[i]->to_json());
}
Json::StreamWriterBuilder jw;
auto resp = HttpResponse::newHttpJsonResponse(all_schedules_json);
callback(resp);
schedule_dbo::free_list(all_schedules);
}
void
schedules::get_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& schedule_id_str)
2019-07-19 09:41:39 +00:00
{
uuid_t schedule_id;
2020-04-19 00:44:35 +00:00
if(schedule_dbo::parse_uid(schedule_id_str.c_str(), schedule_id))
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k400BadRequest);
callback(resp);
return;
}
2020-04-19 00:44:35 +00:00
schedule_dbo **schedules = schedule_dbo::get_by_simple("uid", schedule_id, (intptr_t) &sqlite3_bind_blob, sizeof(uuid_t));
2019-07-19 09:41:39 +00:00
if(schedules[0])
2019-07-19 09:41:39 +00:00
{
auto resp = HttpResponse::newHttpJsonResponse(schedules[0]->to_json());
2019-07-19 09:41:39 +00:00
callback(resp);
}
else
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k404NotFound);
callback(resp);
}
schedule_dbo::free_list(schedules);
2019-07-19 09:41:39 +00:00
}
void
schedules::delete_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& schedule_id_str)
2019-07-19 09:41:39 +00:00
{
if(strcmp(schedule_id_str.c_str(), "off") == 0 || strcmp(schedule_id_str.c_str(), "on") == 0)
2019-07-29 20:02:38 +00:00
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k403Forbidden);
callback(resp);
return;
}
uuid_t schedule_id;
2020-04-19 00:44:35 +00:00
if(schedule_dbo::parse_uid(schedule_id_str.c_str(), schedule_id))
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k400BadRequest);
callback(resp);
return;
}
2020-04-19 00:44:35 +00:00
schedule_dbo **schedules = schedule_dbo::get_by_simple("uid", schedule_id, (intptr_t) &sqlite3_bind_blob, sizeof(uuid_t));
2019-07-19 09:41:39 +00:00
if(schedules[0])
2019-07-19 09:41:39 +00:00
{
auto resp = HttpResponse::newHttpResponse();
if(!schedules[0]->remove())
2019-07-19 09:41:39 +00:00
{
resp->setStatusCode(k500InternalServerError);
}
callback(resp);
}
else
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k404NotFound);
callback(resp);
}
schedule_dbo::free_list(schedules);
2019-07-19 09:41:39 +00:00
}
void
schedules::post_new(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
{
Json::Value body = *req->jsonObject();
2020-04-28 19:50:19 +00:00
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);
}
2019-07-20 12:51:45 +00:00
schedule_dbo new_schedule{};
2019-07-19 09:41:39 +00:00
2019-07-20 12:51:45 +00:00
strncpy(new_schedule.name, body["name"].asCString(), 127);
2019-07-19 09:41:39 +00:00
new_schedule.name[127] = '\0';
2020-04-19 00:44:35 +00:00
uuid_generate(new_schedule.uid);
2019-07-20 12:51:45 +00:00
new_schedule.periods = helpers::parse_periods(body["periods"]);
2019-07-19 09:41:39 +00:00
2019-07-19 12:42:36 +00:00
if(!new_schedule.insert())
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k500InternalServerError);
callback(resp);
}
else
{
auto resp = HttpResponse::newHttpJsonResponse(new_schedule.to_json());
callback(resp);
}
2019-07-19 09:41:39 +00:00
}
void
schedules::post_list(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
{
Json::Value body = *req->jsonObject();
Json::Value schedules_json(Json::arrayValue);
for(int i = 0; i < body.size(); ++i)
{
bool set_name = body[i]["name"].type() == Json::ValueType::stringValue;
bool set_tags = body[i]["tags"].type() == Json::ValueType::arrayValue;
//bool set_periods = body[i]["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[i]["name"].asCString(), 127);
new_schedule.name[127] = '\0';
uuid_generate(new_schedule.uid);
new_schedule.periods = helpers::parse_periods(body[i]["periods"]);
if(new_schedule.insert())
{
schedules_json.append(new_schedule.to_json());
}
}
auto resp = HttpResponse::newHttpJsonResponse(schedules_json);
callback(resp);
}
2019-07-20 12:51:45 +00:00
void
schedules::put_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
const std::string &schedule_id_str)
2019-07-20 12:51:45 +00:00
{
uuid_t schedule_id;
2020-04-19 00:44:35 +00:00
if(schedule_dbo::parse_uid(schedule_id_str.c_str(), schedule_id))
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k400BadRequest);
callback(resp);
return;
}
2020-04-19 00:44:35 +00:00
schedule_dbo **schedules = schedule_dbo::get_by_simple("uid", schedule_id, (intptr_t) &sqlite3_bind_blob, sizeof(uuid_t));
2019-07-20 12:51:45 +00:00
2020-04-28 19:50:19 +00:00
if(schedules[0] == nullptr)
{
auto resp = HttpResponse::newHttpResponse();
resp->setStatusCode(k404NotFound);
callback(resp);
}
Json::Value body = *req->jsonObject();
2019-07-20 12:51:45 +00:00
2020-04-28 19:50:19 +00:00
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)
2019-07-20 12:51:45 +00:00
{
strncpy(schedules[0]->name, body["name"].asCString(), 127);
schedules[0]->name[127] = '\0';
2020-04-28 19:50:19 +00:00
}
2020-02-23 21:54:33 +00:00
2020-04-28 19:50:19 +00:00
if(set_periods)
{
2020-02-23 21:54:33 +00:00
// 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"]);
}
2020-04-28 19:50:19 +00:00
}
2019-07-20 12:51:45 +00:00
2020-04-28 19:50:19 +00:00
if(set_tags)
{
junction_tag_dbo::remove_for_schedule(schedules[0]->id);
for(int i = 0; i < body["tags"].size(); ++i)
2019-07-20 12:51:45 +00:00
{
2020-04-28 19:50:19 +00:00
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);
2019-07-20 12:51:45 +00:00
}
}
2020-04-28 19:50:19 +00:00
if(!schedules[0]->update())
2019-07-20 12:51:45 +00:00
{
auto resp = HttpResponse::newHttpResponse();
2020-04-28 19:50:19 +00:00
resp->setStatusCode(k500InternalServerError);
2019-07-20 12:51:45 +00:00
callback(resp);
}
2020-04-28 19:50:19 +00:00
else
{
auto resp = HttpResponse::newHttpJsonResponse(schedules[0]->to_json());
callback(resp);
}
schedule_dbo::free_list(schedules);
}
2020-04-28 19:50:19 +00:00
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);
}