add: tag post endpoint
This commit is contained in:
parent
7371c9a84e
commit
505faa6df2
4 changed files with 60 additions and 2 deletions
|
@ -6,6 +6,9 @@
|
|||
void
|
||||
api_v1_tags_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
|
||||
|
||||
void
|
||||
api_v1_tags_POST(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
|
||||
|
||||
void
|
||||
api_v1_tags_STR_GET(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response);
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -28,12 +28,12 @@ tag_save(int id, const char *tag)
|
|||
if (rc != SQLITE_DONE)
|
||||
{
|
||||
LOGGER_ERR("error saving tag: %s\n", sqlite3_errmsg(global_database));
|
||||
return false;
|
||||
return 1;
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
char*
|
||||
|
|
|
@ -86,6 +86,7 @@ router_init()
|
|||
router_register_endpoint("/api/v1/relays/tag/{str}", HTTP_METHOD_GET, api_v1_relays_tag_STR_GET);
|
||||
|
||||
router_register_endpoint("/api/v1/tags/", HTTP_METHOD_GET, api_v1_tags_GET);
|
||||
router_register_endpoint("/api/v1/tags/", HTTP_METHOD_POST, api_v1_tags_POST);
|
||||
router_register_endpoint("/api/v1/tags/{str}", HTTP_METHOD_GET, api_v1_tags_STR_GET);
|
||||
router_register_endpoint("/api/v1/tags/{str}", HTTP_METHOD_DELETE, api_v1_tags_STR_DELETE);
|
||||
|
||||
|
|
Loading…
Reference in a new issue