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

View file

@ -19,10 +19,7 @@ api_v1_schedules_STR_GET(struct mg_connection *nc, struct http_message *hm, endp
uuid_t target_uid;
if(schedule_uid_parse(args[0].value.v_str, target_uid))
{
LOGGER_DEBUG("failed to unparse uid\n");
static const char content[] = "given id was invalid";
endpoint_response_text(response, 400, content, STRLEN(content));
M_RESPONSE_400_NO_VALID_ID(response);
return;
}
@ -30,10 +27,7 @@ api_v1_schedules_STR_GET(struct mg_connection *nc, struct http_message *hm, endp
if(!schedule)
{
LOGGER_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));
M_RESPONSE_404_NO_SCHEDULE_FOUND_FOR_ID(response);
return;
}
@ -53,10 +47,7 @@ api_v1_schedules_STR_PUT(struct mg_connection *nc, struct http_message *hm, endp
uuid_t target_uid;
if(schedule_uid_parse(args[0].value.v_str, target_uid))
{
LOGGER_DEBUG("failed to unparse uid\n");
static const char content[] = "given id was invalid";
endpoint_response_text(response, 400, content, STRLEN(content));
M_RESPONSE_400_NO_VALID_ID(response);
return;
}
@ -64,10 +55,7 @@ api_v1_schedules_STR_PUT(struct mg_connection *nc, struct http_message *hm, endp
if(!schedule)
{
LOGGER_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));
M_RESPONSE_404_NO_SCHEDULE_FOUND_FOR_ID(response);
return;
}
@ -75,8 +63,7 @@ api_v1_schedules_STR_PUT(struct mg_connection *nc, struct http_message *hm, endp
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;
}
@ -137,12 +124,10 @@ api_v1_schedules_STR_PUT(struct mg_connection *nc, struct http_message *hm, endp
if(schedule_save(schedule))
{
LOGGER_ERR("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));
M_RESPONSE_500_FAILED_TO_SAVE_TO_DATABASE(response);
return;
}
@ -190,10 +175,7 @@ api_v1_schedules_STR_DELETE(struct mg_connection *nc, struct http_message *hm, e
uuid_t target_uid;
if(schedule_uid_parse(target_uid_str, target_uid))
{
LOGGER_DEBUG("failed to unparse uid\n");
static const char content[] = "given id was invalid";
endpoint_response_text(response, 400, content, STRLEN(content));
M_RESPONSE_400_NO_VALID_ID(response);
return;
}
@ -201,33 +183,25 @@ api_v1_schedules_STR_DELETE(struct mg_connection *nc, struct http_message *hm, e
if(!schedule)
{
LOGGER_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));
M_RESPONSE_404_NO_SCHEDULE_FOUND_FOR_ID(response);
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);
M_RESPONSE_403_PROTECTED_SCHEDULE(response);
return;
}
if(schedule_remove(schedule))
{
LOGGER_ERR("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));
M_RESPONSE_500_FAILED_TO_DELETE_FROM_DATABASE(response);
}
else
{
static const char content[] = "deleted schedule";
endpoint_response_text(response, 200, content, STRLEN(content));
M_RESPONSE_TEXT_STATIC(LOGGER_DEBUG, response, 200, "the target schedule got deleted");
}
schedule_free(schedule);
return;