add: response macros

add: preparation for more macro endpoints
This commit is contained in:
Tobias Reisinger 2020-09-05 13:29:46 +02:00
parent 9d2c48d645
commit 01ffb1d58d
26 changed files with 482 additions and 353 deletions
src/endpoints

View file

@ -42,30 +42,23 @@ api_v1_macros_POST(struct mg_connection *nc, struct http_message *hm, endpoint_a
if(json == NULL)
{
static const char content[] = "no valid json was supplied";
endpoint_response_text(response, 400, content, STRLEN(content));
M_RESPONSE_400_NO_VALID_JSON(response);
return;
}
cJSON *json_name = cJSON_GetObjectItemCaseSensitive(json, "name");
if(!cJSON_IsString(json_name) || (json_name->valuestring == NULL))
{
LOGGER_DEBUG("no name for macro provided\n");
cJSON_Delete(json);
static const char content[] = "no name for macro provided";
endpoint_response_text(response, 400, content, STRLEN(content));
M_RESPONSE_400_NO_NAME(response);
return;
}
cJSON *json_actions = cJSON_GetObjectItemCaseSensitive(json, "actions");
if(!cJSON_IsArray(json_actions))
{
LOGGER_DEBUG("no actions for macro provided\n");
cJSON_Delete(json);
static const char content[] = "no actions for macro provided";
endpoint_response_text(response, 400, content, STRLEN(content));
M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request does not contains actions for the macro");
return;
}
@ -82,15 +75,11 @@ api_v1_macros_POST(struct mg_connection *nc, struct http_message *hm, endpoint_a
if(macro_save(new_macro))
{
LOGGER_DEBUG("macro could not be saved\n");
database_transaction_rollback(&lock);
cJSON_Delete(json);
macro_free(new_macro);
static const char content[] = "macro could not be saved";
endpoint_response_text(response, 500, content, STRLEN(content));
M_RESPONSE_500_FAILED_TO_SAVE_TO_DATABASE(response);
return;
}
@ -100,56 +89,53 @@ api_v1_macros_POST(struct mg_connection *nc, struct http_message *hm, endpoint_a
cJSON *json_action_weekday = cJSON_GetObjectItemCaseSensitive(json_action, "weekday");
if(!cJSON_IsNumber(json_action_weekday) || (json_action_weekday->valueint < 0) || (json_action_weekday->valueint > 6))
{
LOGGER_DEBUG("one action is missing a weekday\n");
database_transaction_rollback(&lock);
cJSON_Delete(json);
macro_free(new_macro);
static const char content[] = "one action is missing a weekday";
endpoint_response_text(response, 400, content, STRLEN(content));
M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains an action without weekday");
return;
}
cJSON *json_action_schedule = cJSON_GetObjectItemCaseSensitive(json_action, "schedule");
if(!cJSON_IsObject(json_action_schedule))
{
LOGGER_DEBUG("action is missing schedule\n");
database_transaction_rollback(&lock);
cJSON_Delete(json);
macro_free(new_macro);
static const char content[] = "one action is missing schedule";
endpoint_response_text(response, 400, content, STRLEN(content));
M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains an action without schedule");
return;
}
cJSON *json_action_schedule_uid = cJSON_GetObjectItemCaseSensitive(json_action_schedule, "id");
if(!cJSON_IsString(json_action_schedule_uid) || (json_action_schedule_uid->valuestring == NULL))
{
LOGGER_DEBUG("action is missing schedule id\n");
database_transaction_rollback(&lock);
cJSON_Delete(json);
macro_free(new_macro);
static const char content[] = "one action is missing schedule id";
endpoint_response_text(response, 400, content, STRLEN(content));
M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains an action without schedule id");
return;
}
uuid_t action_schedule_uid;
if(schedule_uid_parse(json_action_schedule_uid->valuestring, action_schedule_uid))
{
LOGGER_DEBUG("action schedule has bad uid\n");
database_transaction_rollback(&lock);
cJSON_Delete(json);
macro_free(new_macro);
static const char content[] = "action schedule has bad id";
endpoint_response_text(response, 400, content, STRLEN(content));
M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains an action with an invalid schedule id");
return;
}
schedule_t *action_schedule = schedule_get_by_uid(action_schedule_uid);
if(action_schedule == NULL)
{
LOGGER_DEBUG("action schedule was not found\n");
database_transaction_rollback(&lock);
cJSON_Delete(json);
macro_free(new_macro);
static const char content[] = "action schedule was not found";
endpoint_response_text(response, 400, content, STRLEN(content));
M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 404, "the schedule for at least one action was not found");
return;
}
@ -160,55 +146,52 @@ api_v1_macros_POST(struct mg_connection *nc, struct http_message *hm, endpoint_a
cJSON *json_action_relay = cJSON_GetObjectItemCaseSensitive(json_action, "relay");
if(!cJSON_IsObject(json_action_relay))
{
LOGGER_DEBUG("action is missing relay\n");
database_transaction_rollback(&lock);
cJSON_Delete(json);
macro_free(new_macro);
static const char content[] = "one action is missing relay";
endpoint_response_text(response, 400, content, STRLEN(content));
M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains an action without relay");
return;
}
cJSON *json_action_relay_number = cJSON_GetObjectItemCaseSensitive(json_action_relay, "number");
if(!cJSON_IsNumber(json_action_relay_number))
{
LOGGER_DEBUG("action is missing relay number\n");
database_transaction_rollback(&lock);
cJSON_Delete(json);
macro_free(new_macro);
static const char content[] = "one action is missing relay number";
endpoint_response_text(response, 400, content, STRLEN(content));
M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains an action without relay number");
return;
}
cJSON *json_action_relay_controller_uid = cJSON_GetObjectItemCaseSensitive(json_action_relay, "controller_id");
if(!cJSON_IsString(json_action_relay_controller_uid) || (json_action_relay_controller_uid->valuestring == NULL))
{
LOGGER_DEBUG("action is missing relay controller id\n");
database_transaction_rollback(&lock);
cJSON_Delete(json);
macro_free(new_macro);
static const char content[] = "one action is missing relay controller id";
endpoint_response_text(response, 400, content, STRLEN(content));
M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains an action without relay controller id");
return;
}
uuid_t action_controller_uid;
if(uuid_parse(json_action_relay_controller_uid->valuestring, action_controller_uid))
{
LOGGER_DEBUG("action controller has bad uid\n");
database_transaction_rollback(&lock);
cJSON_Delete(json);
macro_free(new_macro);
static const char content[] = "action controller has bad id";
endpoint_response_text(response, 400, content, STRLEN(content));
M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains an action with an invalid relay controller id");
return;
}
controller_t *action_controller = controller_get_by_uid(action_controller_uid);
if(action_controller == NULL)
{
LOGGER_DEBUG("action controller was not found\n");
database_transaction_rollback(&lock);
cJSON_Delete(json);
macro_free(new_macro);
static const char content[] = "action controller was not found";
endpoint_response_text(response, 400, content, STRLEN(content));
M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 404, "the controller for at least one action relay was not found");
return;
}
@ -220,11 +203,11 @@ api_v1_macros_POST(struct mg_connection *nc, struct http_message *hm, endpoint_a
relay_t *action_relay = relay_get_for_controller(controller_id, relay_num);
if(action_relay == NULL)
{
LOGGER_DEBUG("action relay was not found\n");
database_transaction_rollback(&lock);
cJSON_Delete(json);
macro_free(new_macro);
static const char content[] = "action relay was not found";
endpoint_response_text(response, 400, content, STRLEN(content));
M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 404, "the relay for at least one action was not found");
return;
}