a38a6e63b3
add: schedule api calls
137 lines
No EOL
3.5 KiB
C++
137 lines
No EOL
3.5 KiB
C++
#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
|
|
schedules::get_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& schedule_id)
|
|
{
|
|
schedule_dbo *schedule = schedule_dbo::get_one_by("id", schedule_id.c_str());
|
|
|
|
if(schedule)
|
|
{
|
|
auto resp = HttpResponse::newHttpJsonResponse(schedule->to_json());
|
|
|
|
callback(resp);
|
|
|
|
delete schedule;
|
|
}
|
|
else
|
|
{
|
|
auto resp = HttpResponse::newHttpResponse();
|
|
resp->setStatusCode(k404NotFound);
|
|
|
|
callback(resp);
|
|
}
|
|
}
|
|
|
|
void
|
|
schedules::delete_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string& schedule_id)
|
|
{
|
|
schedule_dbo *schedule = schedule_dbo::get_one_by("id", schedule_id.c_str());
|
|
|
|
if(schedule)
|
|
{
|
|
auto resp = HttpResponse::newHttpResponse();
|
|
if(!schedule->remove())
|
|
{
|
|
resp->setStatusCode(k500InternalServerError);
|
|
}
|
|
|
|
callback(resp);
|
|
|
|
delete schedule;
|
|
}
|
|
else
|
|
{
|
|
auto resp = HttpResponse::newHttpResponse();
|
|
resp->setStatusCode(k404NotFound);
|
|
|
|
callback(resp);
|
|
}
|
|
}
|
|
|
|
void
|
|
schedules::post_new(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback)
|
|
{
|
|
Json::Value body = *req->jsonObject();
|
|
|
|
schedule_dbo new_schedule{};
|
|
|
|
strncpy(new_schedule.name, body["name"].asCString(), 127);
|
|
new_schedule.name[127] = '\0';
|
|
strncpy(new_schedule.id, drogon::utils::getUuid().c_str(), 32);
|
|
new_schedule.id[32] = '\0';
|
|
new_schedule.periods = helpers::parse_periods(body["periods"]);
|
|
|
|
if(!new_schedule.insert())
|
|
{
|
|
auto resp = HttpResponse::newHttpResponse();
|
|
resp->setStatusCode(k500InternalServerError);
|
|
callback(resp);
|
|
}
|
|
else
|
|
{
|
|
auto resp = HttpResponse::newHttpJsonResponse(new_schedule.to_json());
|
|
callback(resp);
|
|
}
|
|
}
|
|
|
|
void
|
|
schedules::put_one_by_id(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&callback, const std::string &schedule_id)
|
|
{
|
|
Json::Value body = *req->jsonObject();
|
|
|
|
schedule_dbo *schedule = schedule_dbo::get_one_by("id", schedule_id.c_str());
|
|
|
|
if(schedule)
|
|
{
|
|
strncpy(schedule->name, body["name"].asCString(), 127);
|
|
schedule->name[127] = '\0';
|
|
delete schedule->periods;
|
|
schedule->periods = helpers::parse_periods(body["periods"]);
|
|
|
|
if(!schedule->update())
|
|
{
|
|
auto resp = HttpResponse::newHttpResponse();
|
|
resp->setStatusCode(k500InternalServerError);
|
|
callback(resp);
|
|
}
|
|
else
|
|
{
|
|
auto resp = HttpResponse::newHttpJsonResponse(schedule->to_json());
|
|
callback(resp);
|
|
}
|
|
|
|
delete schedule;
|
|
}
|
|
else
|
|
{
|
|
auto resp = HttpResponse::newHttpResponse();
|
|
resp->setStatusCode(k404NotFound);
|
|
|
|
callback(resp);
|
|
}
|
|
|
|
} |