2019-07-19 09:41:39 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <models/schedule_dbo.h>
|
|
|
|
#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
|
2019-07-20 12:51:45 +00:00
|
|
|
schedules::get_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& schedule_id)
|
2019-07-19 09:41:39 +00:00
|
|
|
{
|
2019-07-20 21:33:17 +00:00
|
|
|
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", schedule_id.c_str(), (intptr_t) &sqlite3_bind_text);
|
2019-07-19 09:41:39 +00:00
|
|
|
|
2019-07-20 21:33:17 +00:00
|
|
|
if(schedules[0])
|
2019-07-19 09:41:39 +00:00
|
|
|
{
|
2019-07-20 21:33:17 +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);
|
|
|
|
}
|
2019-07-20 21:33:17 +00:00
|
|
|
schedule_dbo::free_list(schedules);
|
2019-07-19 09:41:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-07-20 12:51:45 +00:00
|
|
|
schedules::delete_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& schedule_id)
|
2019-07-19 09:41:39 +00:00
|
|
|
{
|
2019-07-20 21:33:17 +00:00
|
|
|
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", schedule_id.c_str(), (intptr_t) &sqlite3_bind_text);
|
2019-07-19 09:41:39 +00:00
|
|
|
|
2019-07-20 21:33:17 +00:00
|
|
|
if(schedules[0])
|
2019-07-19 09:41:39 +00:00
|
|
|
{
|
|
|
|
auto resp = HttpResponse::newHttpResponse();
|
2019-07-20 21:33:17 +00:00
|
|
|
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);
|
|
|
|
}
|
2019-07-20 21:33:17 +00:00
|
|
|
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();
|
|
|
|
|
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';
|
|
|
|
strncpy(new_schedule.id, drogon::utils::getUuid().c_str(), 32);
|
|
|
|
new_schedule.id[32] = '\0';
|
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
|
|
|
}
|
|
|
|
|
2019-07-20 12:51:45 +00:00
|
|
|
void
|
2019-07-20 21:33:17 +00:00
|
|
|
schedules::put_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback,
|
|
|
|
const std::string &schedule_id)
|
2019-07-20 12:51:45 +00:00
|
|
|
{
|
|
|
|
Json::Value body = *req->jsonObject();
|
|
|
|
|
2019-07-20 21:33:17 +00:00
|
|
|
schedule_dbo **schedules = schedule_dbo::get_by_simple("id", schedule_id.c_str(), (intptr_t) &sqlite3_bind_text);
|
2019-07-20 12:51:45 +00:00
|
|
|
|
2019-07-20 21:33:17 +00:00
|
|
|
if(schedules[0])
|
2019-07-20 12:51:45 +00:00
|
|
|
{
|
2019-07-20 21:33:17 +00:00
|
|
|
strncpy(schedules[0]->name, body["name"].asCString(), 127);
|
|
|
|
schedules[0]->name[127] = '\0';
|
|
|
|
delete schedules[0]->periods;
|
|
|
|
schedules[0]->periods = helpers::parse_periods(body["periods"]);
|
2019-07-20 12:51:45 +00:00
|
|
|
|
2019-07-20 21:33:17 +00:00
|
|
|
if(!schedules[0]->update())
|
2019-07-20 12:51:45 +00:00
|
|
|
{
|
|
|
|
auto resp = HttpResponse::newHttpResponse();
|
|
|
|
resp->setStatusCode(k500InternalServerError);
|
|
|
|
callback(resp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-07-20 21:33:17 +00:00
|
|
|
auto resp = HttpResponse::newHttpJsonResponse(schedules[0]->to_json());
|
2019-07-20 12:51:45 +00:00
|
|
|
callback(resp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto resp = HttpResponse::newHttpResponse();
|
|
|
|
resp->setStatusCode(k404NotFound);
|
|
|
|
|
|
|
|
callback(resp);
|
|
|
|
}
|
2019-07-20 21:33:17 +00:00
|
|
|
schedule_dbo::free_list(schedules);
|
2019-07-20 12:51:45 +00:00
|
|
|
}
|