core-legacy/endpoints/api_v1_schedules_STR.c

55 lines
1.4 KiB
C
Raw Normal View History

2020-05-05 09:42:02 +00:00
#include <cJSON.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_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_printf(c, "{}");
return;
}
char debug_str[40];
uuid_unparse(target_uid, debug_str);
LOG_DEBUG("uid: %s\n", debug_str);
schedule_t** all_schedules = schedule_get_all();
cJSON *json = cJSON_CreateArray();
for(int i = 0; all_schedules[i] != NULL; ++i)
{
cJSON *json_schedule = schedule_to_json(all_schedules[i]);
cJSON_AddItemToArray(json, 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_printf(c, "[]");
}
else
{
mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json");
mg_printf(c, "%s", json_str);
free(json_str);
}
cJSON_Delete(json);
schedule_free_list(all_schedules);
}