From 505faa6df2f989f31eb4680aa6d33f3f0ab55c23 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Tue, 11 Aug 2020 23:33:04 +0200 Subject: [PATCH] add: tag post endpoint --- include/endpoints/api_v1_tags.h | 3 ++ src/endpoints/api_v1_tags.c | 54 +++++++++++++++++++++++++++++++++ src/models/tag.c | 4 +-- src/router.c | 1 + 4 files changed, 60 insertions(+), 2 deletions(-) diff --git a/include/endpoints/api_v1_tags.h b/include/endpoints/api_v1_tags.h index 0f68f20..50d8cbc 100644 --- a/include/endpoints/api_v1_tags.h +++ b/include/endpoints/api_v1_tags.h @@ -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); diff --git a/src/endpoints/api_v1_tags.c b/src/endpoints/api_v1_tags.c index 66fc6e8..96b3d29 100644 --- a/src/endpoints/api_v1_tags.c +++ b/src/endpoints/api_v1_tags.c @@ -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; +} diff --git a/src/models/tag.c b/src/models/tag.c index 66f840a..e86541c 100644 --- a/src/models/tag.c +++ b/src/models/tag.c @@ -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* diff --git a/src/router.c b/src/router.c index 14e53de..6c74428 100644 --- a/src/router.c +++ b/src/router.c @@ -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);