REFACTOR
This commit is contained in:
parent
0460b2e9f7
commit
176483d72f
59 changed files with 11 additions and 45 deletions
src/endpoints
238
src/endpoints/api_v1_schedules_STR.c
Normal file
238
src/endpoints/api_v1_schedules_STR.c
Normal file
|
@ -0,0 +1,238 @@
|
|||
#include <cJSON.h>
|
||||
#include <macros.h>
|
||||
#include <constants.h>
|
||||
#include <endpoints/api_v1_schedules.h>
|
||||
#include <logger.h>
|
||||
#include <command.h>
|
||||
#include <models/junction_tag.h>
|
||||
#include <models/junction_relay_schedule.h>
|
||||
#include <models/schedule.h>
|
||||
#include <models/relay.h>
|
||||
#include <models/tag.h>
|
||||
|
||||
void
|
||||
api_v1_schedules_STR_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
||||
{
|
||||
(void)hm;
|
||||
(void)nc;
|
||||
|
||||
uuid_t target_uid;
|
||||
if(schedule_uid_parse(args[0].value.v_str, target_uid))
|
||||
{
|
||||
LOG_DEBUG("failed to unparse uid\n");
|
||||
|
||||
static const char content[] = "given id was invalid";
|
||||
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||
return;
|
||||
}
|
||||
|
||||
schedule_t* schedule = schedule_get_by_uid(target_uid);
|
||||
|
||||
if(!schedule)
|
||||
{
|
||||
LOG_DEBUG("could not find a schedule for uid '%s'\n", args[0].value.v_str);
|
||||
|
||||
static const char content[] = "no schedule for id found";
|
||||
endpoint_response_text(response, 404, content, STRLEN(content));
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON *json = schedule_to_json(schedule);
|
||||
|
||||
endpoint_response_json(response, 200, json);
|
||||
cJSON_Delete(json);
|
||||
schedule_free(schedule);
|
||||
}
|
||||
|
||||
void
|
||||
api_v1_schedules_STR_PUT(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
||||
{
|
||||
(void)hm;
|
||||
(void)nc;
|
||||
|
||||
uuid_t target_uid;
|
||||
if(schedule_uid_parse(args[0].value.v_str, target_uid))
|
||||
{
|
||||
LOG_DEBUG("failed to unparse uid\n");
|
||||
|
||||
static const char content[] = "given id was invalid";
|
||||
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||
return;
|
||||
}
|
||||
|
||||
schedule_t* schedule = schedule_get_by_uid(target_uid);
|
||||
|
||||
if(!schedule)
|
||||
{
|
||||
LOG_DEBUG("could not find a schedule for uid '%s'\n", args[0].value.v_str);
|
||||
|
||||
static const char content[] = "no schedule for id found";
|
||||
endpoint_response_text(response, 404, content, STRLEN(content));
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON *json = cJSON_ParseWithLength(hm->body.p, hm->body.len);
|
||||
|
||||
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)
|
||||
{
|
||||
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");
|
||||
free(schedule);
|
||||
cJSON_Delete(json);
|
||||
|
||||
static const char content[] = "failed to save schedule to database";
|
||||
endpoint_response_text(response, 500, content, STRLEN(content));
|
||||
return;
|
||||
}
|
||||
|
||||
relay_t **relays = relay_get_with_schedule(schedule->id);
|
||||
for(int i = 0; relays[i] != NULL; ++i)
|
||||
{
|
||||
command_set_relay_schedule(relays[i]);
|
||||
}
|
||||
|
||||
cJSON *json_tag;
|
||||
cJSON *json_tags = cJSON_GetObjectItemCaseSensitive(json, "tags");
|
||||
if(cJSON_IsArray(json_tags))
|
||||
{
|
||||
junction_tag_remove_for_schedule(schedule->id);
|
||||
}
|
||||
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);
|
||||
|
||||
endpoint_response_json(response, 200, json);
|
||||
cJSON_Delete(json);
|
||||
relay_free_list(relays);
|
||||
schedule_free(schedule);
|
||||
}
|
||||
|
||||
void
|
||||
api_v1_schedules_STR_DELETE(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
||||
{
|
||||
(void)hm;
|
||||
(void)nc;
|
||||
|
||||
const char *target_uid_str = args[0].value.v_str;
|
||||
|
||||
uuid_t target_uid;
|
||||
if(schedule_uid_parse(target_uid_str, target_uid))
|
||||
{
|
||||
LOG_DEBUG("failed to unparse uid\n");
|
||||
|
||||
static const char content[] = "given id was invalid";
|
||||
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||
return;
|
||||
}
|
||||
|
||||
schedule_t* schedule = schedule_get_by_uid(target_uid);
|
||||
|
||||
if(!schedule)
|
||||
{
|
||||
LOG_DEBUG("could not find a schedule for uid '%s'\n", args[0].value.v_str);
|
||||
|
||||
static const char content[] = "no schedule for id found";
|
||||
endpoint_response_text(response, 404, content, STRLEN(content));
|
||||
return;
|
||||
}
|
||||
|
||||
if(schedule_is_protected(schedule))
|
||||
{
|
||||
static const char content[] = "target schedule is protected";
|
||||
endpoint_response_text(response, 403, content, STRLEN(content));
|
||||
|
||||
schedule_free(schedule);
|
||||
return;
|
||||
}
|
||||
|
||||
if(schedule_remove(schedule))
|
||||
{
|
||||
LOG_ERROR("failed to remove schedule from database\n");
|
||||
|
||||
static const char content[] = "failed to remove schedule from database";
|
||||
endpoint_response_text(response, 500, content, STRLEN(content));
|
||||
}
|
||||
else
|
||||
{
|
||||
endpoint_response_text(response, 200, "", 0);
|
||||
}
|
||||
schedule_free(schedule);
|
||||
return;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue