add: tests
add: schedule endpoints
This commit is contained in:
parent
6d828fcffc
commit
b5a8523ae0
14 changed files with 468 additions and 42 deletions
|
@ -1,4 +1,5 @@
|
|||
#include <cJSON.h>
|
||||
#include <constants.h>
|
||||
#include <endpoints/api_v1_schedules.h>
|
||||
#include <logger.h>
|
||||
#include <models/junction_tag.h>
|
||||
|
@ -24,9 +25,9 @@ api_v1_schedules_POST(struct mg_connection *c, endpoint_args_t *args, struct htt
|
|||
cJSON *json_name = cJSON_GetObjectItemCaseSensitive(json, "name");
|
||||
if(!cJSON_IsString(json_name) || (json_name->valuestring == NULL))
|
||||
{
|
||||
char *error_msg = "ERROR: no name for schedule provided";
|
||||
mg_send_head(c, 400, strlen(error_msg), "Content-Type: text/plain");
|
||||
mg_printf(c, "%s", error_msg);
|
||||
LOG_DEBUG("no name for schedule provided\n");
|
||||
mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
|
||||
cJSON_Delete(json);
|
||||
return;
|
||||
|
@ -112,12 +113,12 @@ api_v1_schedules_POST(struct mg_connection *c, endpoint_args_t *args, struct htt
|
|||
if (json_str == NULL)
|
||||
{
|
||||
LOG_ERROR("failed to print schedule json\n");
|
||||
mg_send_head(c, 201, 2, "Content-Type: application/json");
|
||||
mg_send_head(c, 201, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
}
|
||||
else
|
||||
{
|
||||
mg_send_head(c, 201, strlen(json_str), "Content-Type: application/json");
|
||||
mg_send_head(c, 201, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "%s", json_str);
|
||||
free(json_str);
|
||||
}
|
||||
|
@ -145,12 +146,12 @@ api_v1_schedules_GET(struct mg_connection *c, endpoint_args_t *args, struct http
|
|||
if (json_str == NULL)
|
||||
{
|
||||
LOG_ERROR("failed to print schedules json\n");
|
||||
mg_send_head(c, 500, 2, "Content-Type: application/json");
|
||||
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "[]");
|
||||
}
|
||||
else
|
||||
{
|
||||
mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json");
|
||||
mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "%s", json_str);
|
||||
free(json_str);
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include <cJSON.h>
|
||||
#include <constants.h>
|
||||
#include <endpoints/api_v1_schedules.h>
|
||||
#include <logger.h>
|
||||
#include <models/junction_tag.h>
|
||||
|
@ -8,47 +9,227 @@
|
|||
void
|
||||
api_v1_schedules_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
|
||||
{
|
||||
(void)args;
|
||||
(void)hm;
|
||||
|
||||
uuid_t target_uid;
|
||||
if(schedule_uid_parse(args[0].value.v_str, target_uid))
|
||||
{
|
||||
LOG_ERROR("failed to unparse uid\n");
|
||||
mg_send_head(c, 400, 2, "Content-Type: application/json");
|
||||
mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
return;
|
||||
}
|
||||
|
||||
char debug_str[40];
|
||||
uuid_unparse(target_uid, debug_str);
|
||||
LOG_DEBUG("uid: %s\n", debug_str);
|
||||
schedule_t* schedule = schedule_get_by_uid(target_uid);
|
||||
|
||||
schedule_t** all_schedules = schedule_get_all();
|
||||
|
||||
cJSON *json = cJSON_CreateArray();
|
||||
|
||||
for(int i = 0; all_schedules[i] != NULL; ++i)
|
||||
if(!schedule)
|
||||
{
|
||||
cJSON *json_schedule = schedule_to_json(all_schedules[i]);
|
||||
|
||||
cJSON_AddItemToArray(json, json_schedule);
|
||||
LOG_ERROR("could not find a schedule for uid '%s'\n", args[0].value.v_str);
|
||||
mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON *json = schedule_to_json(schedule);
|
||||
|
||||
char *json_str = cJSON_Print(json);
|
||||
if (json_str == NULL)
|
||||
{
|
||||
LOG_ERROR("failed to print schedules json\n");
|
||||
mg_send_head(c, 500, 2, "Content-Type: application/json");
|
||||
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "[]");
|
||||
}
|
||||
else
|
||||
{
|
||||
mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json");
|
||||
mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "%s", json_str);
|
||||
free(json_str);
|
||||
}
|
||||
cJSON_Delete(json);
|
||||
schedule_free_list(all_schedules);
|
||||
schedule_free(schedule);
|
||||
}
|
||||
|
||||
void
|
||||
api_v1_schedules_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
|
||||
{
|
||||
(void)hm;
|
||||
|
||||
uuid_t target_uid;
|
||||
if(schedule_uid_parse(args[0].value.v_str, target_uid))
|
||||
{
|
||||
LOG_ERROR("failed to unparse uid\n");
|
||||
mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
return;
|
||||
}
|
||||
|
||||
schedule_t* schedule = schedule_get_by_uid(target_uid);
|
||||
|
||||
if(!schedule)
|
||||
{
|
||||
LOG_ERROR("could not find a schedule for uid '%s'\n", args[0].value.v_str);
|
||||
mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON *json = cJSON_ParseWithLength(hm->body.p, hm->body.len);
|
||||
|
||||
if(json == NULL)
|
||||
{
|
||||
const char *error_ptr = cJSON_GetErrorPtr();
|
||||
if (error_ptr != NULL)
|
||||
{
|
||||
LOG_ERROR("error before: %s\n", error_ptr);
|
||||
}
|
||||
cJSON_Delete(json);
|
||||
}
|
||||
|
||||
cJSON *json_name = cJSON_GetObjectItemCaseSensitive(json, "name");
|
||||
if(cJSON_IsString(json_name) && json_name->valuestring)
|
||||
{
|
||||
strncpy(schedule->name, json_name->valuestring, MAX_NAME_LENGTH);
|
||||
schedule->name[MAX_NAME_LENGTH] = '\0';
|
||||
}
|
||||
|
||||
if(!schedule_is_protected(schedule))
|
||||
{
|
||||
cJSON *json_period;
|
||||
cJSON *json_periods = cJSON_GetObjectItemCaseSensitive(json, "periods");
|
||||
|
||||
int periods_count = cJSON_GetArraySize(json_periods);
|
||||
free(schedule->periods);
|
||||
schedule->periods = malloc(sizeof(period_t) * periods_count);
|
||||
|
||||
int periods_valid = 0;
|
||||
|
||||
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");
|
||||
continue;
|
||||
}
|
||||
if(!cJSON_IsString(json_period_end) || (json_period_end->valuestring == NULL))
|
||||
{
|
||||
LOG_DEBUG("period is missing end\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
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);
|
||||
continue;
|
||||
}
|
||||
if(period_helper_parse_hhmm(json_period_end->valuestring, &end))
|
||||
{
|
||||
LOG_DEBUG("couldn't parse end '%s'\n", json_period_end->valuestring);
|
||||
continue;
|
||||
}
|
||||
|
||||
schedule->periods[periods_valid].start = start;
|
||||
schedule->periods[periods_valid].end = end;
|
||||
++periods_valid;
|
||||
}
|
||||
|
||||
schedule->periods_count = periods_valid;
|
||||
}
|
||||
|
||||
if(schedule_save(schedule))
|
||||
{
|
||||
LOG_ERROR("failed to save schedule\n");
|
||||
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
free(schedule);
|
||||
cJSON_Delete(json);
|
||||
return;
|
||||
}
|
||||
|
||||
junction_tag_remove_for_schedule(schedule->id);
|
||||
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");
|
||||
continue;
|
||||
}
|
||||
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, schedule->id);
|
||||
}
|
||||
|
||||
cJSON_Delete(json);
|
||||
json = schedule_to_json(schedule);
|
||||
|
||||
char *json_str = cJSON_Print(json);
|
||||
if (json_str == NULL)
|
||||
{
|
||||
LOG_ERROR("failed to print schedule json\n");
|
||||
mg_send_head(c, 200, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
}
|
||||
else
|
||||
{
|
||||
mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "%s", json_str);
|
||||
free(json_str);
|
||||
}
|
||||
cJSON_Delete(json);
|
||||
schedule_free(schedule);
|
||||
}
|
||||
|
||||
void
|
||||
api_v1_schedules_STR_DELETE(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
|
||||
{
|
||||
(void)hm;
|
||||
|
||||
char *target_uid_str = args[0].value.v_str;
|
||||
|
||||
uuid_t target_uid;
|
||||
if(schedule_uid_parse(target_uid_str, target_uid))
|
||||
{
|
||||
LOG_ERROR("failed to unparse uid\n");
|
||||
mg_send_head(c, 400, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
return;
|
||||
}
|
||||
|
||||
schedule_t* schedule = schedule_get_by_uid(target_uid);
|
||||
|
||||
if(schedule_is_protected(schedule))
|
||||
{
|
||||
mg_send_head(c, 403, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
return;
|
||||
}
|
||||
|
||||
if(!schedule)
|
||||
{
|
||||
LOG_ERROR("could not find a schedule for uid '%s'\n", target_uid_str);
|
||||
mg_send_head(c, 404, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
return;
|
||||
}
|
||||
|
||||
if(schedule_remove(schedule))
|
||||
{
|
||||
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
return;
|
||||
}
|
||||
mg_send_head(c, 200, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "{}");
|
||||
return;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue