From 174907730bd2175bddc896cb6484918e2e5064f5 Mon Sep 17 00:00:00 2001 From: Michael McVady Date: Wed, 22 Feb 2023 22:23:27 -0600 Subject: Make post UUID optional --- src/clog.c | 48 +++++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 21 deletions(-) (limited to 'src/clog.c') diff --git a/src/clog.c b/src/clog.c index 6364c49..2e2be5a 100644 --- a/src/clog.c +++ b/src/clog.c @@ -301,18 +301,13 @@ int post_posts(struct http_request *req) { if (item != NULL) { id = item->data.string; kore_log(LOG_INFO, "id = '%s'\n", id); - } else { - status = HTTP_STATUS_BAD_REQUEST; - kore_log(LOG_ERR, "error parsing id: %s\n", kore_json_strerror()); - goto out; - } - - // Check for valid resource ID/UUID - err = validate_uuid(id); - if (err == KORE_RESULT_ERROR) { - status = HTTP_STATUS_BAD_REQUEST; - kore_log(LOG_ERR, "Invalid post UUID %s.", id); - goto out; + // Check for valid resource ID/UUID + err = validate_uuid(id); + if (err == KORE_RESULT_ERROR) { + status = HTTP_STATUS_BAD_REQUEST; + kore_log(LOG_ERR, "Invalid post UUID %s.", id); + goto out; + } } item = kore_json_find_string(json.root, "title"); @@ -518,15 +513,26 @@ int sql_insert_posts(const char *id, const char *title, const char *body) { goto out; } - err = kore_pgsql_query_params( - &sql, - q_insert_posts, - 0, - 3, - KORE_PGSQL_PARAM_TEXT(id), - KORE_PGSQL_PARAM_TEXT(title), - KORE_PGSQL_PARAM_TEXT(body) - ); + if (id == NULL) { + err = kore_pgsql_query_params( + &sql, + q_insert_posts, + 0, + 2, + KORE_PGSQL_PARAM_TEXT(title), + KORE_PGSQL_PARAM_TEXT(body) + ); + } else { + err = kore_pgsql_query_params( + &sql, + q_insert_posts_with_id, + 0, + 3, + KORE_PGSQL_PARAM_TEXT(id), + KORE_PGSQL_PARAM_TEXT(title), + KORE_PGSQL_PARAM_TEXT(body) + ); + } if (err == KORE_RESULT_ERROR) { kore_pgsql_logerror(&sql); goto out; -- cgit v1.2.3