add: POST schedules/list
fix: CORS
This commit is contained in:
parent
a78815cb32
commit
309f2fbb52
5 changed files with 190 additions and 7 deletions
endpoints
155
endpoints/api_v1_schedules_list.c
Normal file
155
endpoints/api_v1_schedules_list.c
Normal file
|
@ -0,0 +1,155 @@
|
|||
#include <cJSON.h>
|
||||
#include <macros.h>
|
||||
#include <constants.h>
|
||||
#include <endpoints/api_v1_schedules.h>
|
||||
#include <logger.h>
|
||||
#include <models/junction_tag.h>
|
||||
#include <models/schedule.h>
|
||||
#include <models/tag.h>
|
||||
|
||||
void
|
||||
api_v1_schedules_list_POST(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
||||
{
|
||||
(void)args;
|
||||
(void)nc;
|
||||
cJSON *json_list = cJSON_ParseWithLength(hm->body.p, hm->body.len);
|
||||
|
||||
|
||||
cJSON *json;
|
||||
cJSON_ArrayForEach(json, json_list)
|
||||
{
|
||||
if(json == NULL)
|
||||
{
|
||||
static const char content[] = "no valid json was supplied";
|
||||
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON *json_name = cJSON_GetObjectItemCaseSensitive(json, "name");
|
||||
if(!cJSON_IsString(json_name) || (json_name->valuestring == NULL))
|
||||
{
|
||||
LOG_DEBUG("no name for schedule provided\n");
|
||||
cJSON_Delete(json_list);
|
||||
|
||||
static const char content[] = "no name for schedule provided";
|
||||
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||
return;
|
||||
}
|
||||
cJSON *json_periods = cJSON_GetObjectItemCaseSensitive(json, "periods");
|
||||
if(!cJSON_IsArray(json_periods))
|
||||
{
|
||||
LOG_DEBUG("no periods for schedule provided\n");
|
||||
cJSON_Delete(json_list);
|
||||
|
||||
static const char content[] = "no periods for schedule provided";
|
||||
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON *json_tag;
|
||||
cJSON *json_tags = cJSON_GetObjectItemCaseSensitive(json, "tags");
|
||||
cJSON_ArrayForEach(json_tag, json_tags)
|
||||
{
|
||||
if(!cJSON_IsString(json_tag) || (json_tag->valuestring == NULL))
|
||||
{
|
||||
LOG_DEBUG("invalid tag in tags\n");
|
||||
cJSON_Delete(json_list);
|
||||
|
||||
static const char content[] = "invalid tag in tags";
|
||||
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
schedule_t *new_schedule = malloc(sizeof(schedule_t));
|
||||
|
||||
new_schedule->id = 0;
|
||||
uuid_generate(new_schedule->uid);
|
||||
|
||||
strncpy(new_schedule->name, json_name->valuestring, MAX_NAME_LENGTH);
|
||||
new_schedule->name[MAX_NAME_LENGTH] = '\0';
|
||||
|
||||
int periods_count = cJSON_GetArraySize(json_periods);
|
||||
new_schedule->periods = malloc(sizeof(period_t) * periods_count);
|
||||
|
||||
int periods_valid = 0;
|
||||
|
||||
cJSON *json_period;
|
||||
cJSON_ArrayForEach(json_period, json_periods)
|
||||
{
|
||||
cJSON *json_period_start = cJSON_GetObjectItemCaseSensitive(json_period, "start");
|
||||
cJSON *json_period_end = cJSON_GetObjectItemCaseSensitive(json_period, "end");
|
||||
|
||||
if(!cJSON_IsString(json_period_start) || (json_period_start->valuestring == NULL))
|
||||
{
|
||||
LOG_DEBUG("period is missing start\n");
|
||||
cJSON_Delete(json_list);
|
||||
schedule_free(new_schedule);
|
||||
|
||||
static const char content[] = "one period is missing a start";
|
||||
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||
return;
|
||||
}
|
||||
if(!cJSON_IsString(json_period_end) || (json_period_end->valuestring == NULL))
|
||||
{
|
||||
LOG_DEBUG("period is missing end\n");
|
||||
cJSON_Delete(json_list);
|
||||
schedule_free(new_schedule);
|
||||
|
||||
static const char content[] = "one period is missing an end";
|
||||
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||
return;
|
||||
}
|
||||
|
||||
uint16_t start;
|
||||
uint16_t end;
|
||||
if(period_helper_parse_hhmm(json_period_start->valuestring, &start))
|
||||
{
|
||||
LOG_DEBUG("couldn't parse start '%s'\n", json_period_start->valuestring);
|
||||
cJSON_Delete(json_list);
|
||||
schedule_free(new_schedule);
|
||||
|
||||
static const char content[] = "the start for one period is invalid";
|
||||
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||
return;
|
||||
}
|
||||
if(period_helper_parse_hhmm(json_period_end->valuestring, &end))
|
||||
{
|
||||
LOG_DEBUG("couldn't parse end '%s'\n", json_period_end->valuestring);
|
||||
cJSON_Delete(json_list);
|
||||
schedule_free(new_schedule);
|
||||
|
||||
static const char content[] = "the end for one period is invalid";
|
||||
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||
return;
|
||||
}
|
||||
|
||||
new_schedule->periods[periods_valid].start = start;
|
||||
new_schedule->periods[periods_valid].end = end;
|
||||
++periods_valid;
|
||||
}
|
||||
|
||||
new_schedule->periods_count = periods_valid;
|
||||
|
||||
schedule_save(new_schedule);
|
||||
|
||||
junction_tag_remove_for_schedule(new_schedule->id);
|
||||
|
||||
json_tags = cJSON_GetObjectItemCaseSensitive(json, "tags");
|
||||
cJSON_ArrayForEach(json_tag, json_tags)
|
||||
{
|
||||
const char *tag = json_tag->valuestring;
|
||||
int tag_id = tag_get_id(tag);
|
||||
if(tag_id == 0)
|
||||
{
|
||||
tag_save(tag_id, tag);
|
||||
tag_id = tag_get_id(tag);
|
||||
}
|
||||
junction_tag_insert(tag_id, 0, new_schedule->id);
|
||||
}
|
||||
schedule_free(new_schedule);
|
||||
}
|
||||
|
||||
cJSON_Delete(json_list);
|
||||
api_v1_schedules_GET(nc, hm, args, response);;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue