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

@ -16,30 +16,23 @@ api_v1_schedules_POST(struct mg_connection *nc, struct http_message *hm, endpoin
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 schedule provided\n");
cJSON_Delete(json);
static const char content[] = "no name for schedule provided";
endpoint_response_text(response, 400, content, STRLEN(content));
M_RESPONSE_400_NO_NAME(response);
return;
}
cJSON *json_periods = cJSON_GetObjectItemCaseSensitive(json, "periods");
if(!cJSON_IsArray(json_periods))
{
LOGGER_DEBUG("no periods for schedule provided\n");
cJSON_Delete(json);
static const char content[] = "no periods for schedule provided";
endpoint_response_text(response, 400, content, STRLEN(content));
M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request does not contain periods");
return;
}
@ -49,11 +42,9 @@ api_v1_schedules_POST(struct mg_connection *nc, struct http_message *hm, endpoin
{
if(!cJSON_IsString(json_tag) || (json_tag->valuestring == NULL))
{
LOGGER_DEBUG("invalid tag in tags\n");
cJSON_Delete(json);
static const char content[] = "invalid tag in tags";
endpoint_response_text(response, 400, content, STRLEN(content));
M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains at least one invalid tag");
return;
}
}
@ -79,22 +70,18 @@ api_v1_schedules_POST(struct mg_connection *nc, struct http_message *hm, endpoin
if(!cJSON_IsString(json_period_start) || (json_period_start->valuestring == NULL))
{
LOGGER_DEBUG("period is missing start\n");
cJSON_Delete(json);
schedule_free(new_schedule);
static const char content[] = "one period is missing a start";
endpoint_response_text(response, 400, content, STRLEN(content));
M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains at least one period without a start");
return;
}
if(!cJSON_IsString(json_period_end) || (json_period_end->valuestring == NULL))
{
LOGGER_DEBUG("period is missing end\n");
cJSON_Delete(json);
schedule_free(new_schedule);
static const char content[] = "one period is missing an end";
endpoint_response_text(response, 400, content, STRLEN(content));
M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains at least one period without an end");
return;
}
@ -102,22 +89,18 @@ api_v1_schedules_POST(struct mg_connection *nc, struct http_message *hm, endpoin
uint16_t end;
if(period_helper_parse_hhmm(json_period_start->valuestring, &start))
{
LOGGER_DEBUG("couldn't parse start '%s'\n", json_period_start->valuestring);
cJSON_Delete(json);
schedule_free(new_schedule);
static const char content[] = "the start for one period is invalid";
endpoint_response_text(response, 400, content, STRLEN(content));
M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains at least one period with an invalid start");
return;
}
if(period_helper_parse_hhmm(json_period_end->valuestring, &end))
{
LOGGER_DEBUG("couldn't parse end '%s'\n", json_period_end->valuestring);
cJSON_Delete(json);
schedule_free(new_schedule);
static const char content[] = "the end for one period is invalid";
endpoint_response_text(response, 400, content, STRLEN(content));
M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 400, "the request contains at least one period with an invalid end");
return;
}