diff --git a/controllers/api_v1_schedules.cc b/controllers/api_v1_schedules.cc index af45f8f..34a0184 100644 --- a/controllers/api_v1_schedules.cc +++ b/controllers/api_v1_schedules.cc @@ -119,7 +119,6 @@ schedules::post_new(const HttpRequestPtr &req, std::function &&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); +} + void schedules::put_one_by_id(const HttpRequestPtr &req, std::function &&callback, const std::string &schedule_id_str) diff --git a/controllers/api_v1_schedules.h b/controllers/api_v1_schedules.h index c3c1a5b..ea81bf2 100644 --- a/controllers/api_v1_schedules.h +++ b/controllers/api_v1_schedules.h @@ -8,6 +8,7 @@ namespace api::v1 public: METHOD_LIST_BEGIN METHOD_ADD(schedules::post_new, "/", Post, Options, "filters::json_required", "filters::schedules::valid_json"); + METHOD_ADD(schedules::post_list, "/list", Post, Options, "filters::json_required"); METHOD_ADD(schedules::get_all, "/", Get, Options); METHOD_ADD(schedules::get_one_by_id, "/{1}", Get, Options); METHOD_ADD(schedules::delete_one_by_id, "/{1}", Delete, Options); @@ -18,6 +19,7 @@ namespace api::v1 METHOD_LIST_END static void post_new(const HttpRequestPtr& req, std::function &&callback); + static void post_list(const HttpRequestPtr& req, std::function &&callback); static void get_all(const HttpRequestPtr& req, std::function &&callback); static void get_one_by_id(const HttpRequestPtr& req, std::function &&callback, const std::string& schedule_id); static void delete_one_by_id(const HttpRequestPtr& req, std::function &&callback, const std::string& schedule_id);