add: tag post endpoint

This commit is contained in:
Tobias Reisinger 2020-08-11 23:33:04 +02:00
parent 7371c9a84e
commit 505faa6df2
4 changed files with 60 additions and 2 deletions
src/endpoints

View file

@ -33,3 +33,57 @@ api_v1_tags_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args
cJSON_Delete(json);
free(all_tags);
}
void
api_v1_tags_POST(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
{
(void)args;
(void)nc;
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_tag = cJSON_GetObjectItemCaseSensitive(json, "tag");
if(!cJSON_IsString(json_tag) || (json_tag->valuestring == NULL))
{
LOGGER_DEBUG("no tag provided\n");
cJSON_Delete(json);
static const char content[] = "no tag provided";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
if(tag_get_id(json_tag->valuestring))
{
LOGGER_DEBUG("tag existed already\n");
cJSON_Delete(json);
static const char content[] = "tag existed already";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
if(tag_save(0, json_tag->valuestring))
{
LOGGER_DEBUG("tag could not be saved\n");
static const char content[] = "tag could not be saved";
endpoint_response_text(response, 500, content, STRLEN(content));
}
else
{
LOGGER_DEBUG("new tag saved\n");
endpoint_response_text(response, 201, json_tag->valuestring, 0);
}
cJSON_Delete(json);
return;
}