From e4ca47cc71bb64d724db3479406d381edc9661bc Mon Sep 17 00:00:00 2001 From: Michael McVady Date: Thu, 5 Jan 2023 13:24:00 -0600 Subject: Add toy endpoint to insert data into sql --- src/clog.c | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/clog.c b/src/clog.c index e15b1ee..edd9f2a 100644 --- a/src/clog.c +++ b/src/clog.c @@ -56,6 +56,8 @@ int posts(struct http_request *req); int render_posts(struct http_request *req, const char *resource); int render_posts_query(struct post_request *post_req); +int toy(struct http_request *req); + static void process_md_output(const MD_CHAR *, MD_SIZE size, void *); static int render_md(const char *, struct kore_buf *); @@ -212,7 +214,7 @@ render_posts_query(struct post_request *post_req) { err = validate_uuid(post_req->resource); if (err == KORE_RESULT_ERROR) { post_req->resp_status = HTTP_STATUS_NOT_FOUND; - kore_log(LOG_ERR, "Invalid post id %s.", post_req->resource); + kore_log(LOG_ERR, "Invalid post UUID %s.", post_req->resource); goto out; } } @@ -267,6 +269,8 @@ render_posts_query(struct post_request *post_req) { ); } else { // post_req->type == HTML rows = kore_pgsql_ntuples(&sql); + // FIXME: add test for this: + // if (post_req->resource && rows == 0) { if (rows == 0) { post_req->resp_status = HTTP_STATUS_NOT_FOUND; goto out; @@ -311,6 +315,45 @@ out: ; return KORE_RESULT_OK; } +int +toy(struct http_request *req) { + int err = 0; + + int status = HTTP_STATUS_OK; + + struct kore_pgsql sql; + + kore_pgsql_init(&sql); + + err = kore_pgsql_setup(&sql, database, KORE_PGSQL_SYNC); + if (err == KORE_RESULT_ERROR) { + status = HTTP_STATUS_INTERNAL_ERROR; + kore_pgsql_logerror(&sql); + goto out; + } + + // Query a post. + err = kore_pgsql_query( + &sql, + "INSERT INTO posts (id, title, body) VALUES ('00000000-0000-0000-0000-000000000000', 'title', 'body');" + // 'af5bbaec-8d2b-11ed-9bf3-db5637a87ac5' + ); + + if (err == KORE_RESULT_ERROR) { + status = HTTP_STATUS_INTERNAL_ERROR; + kore_pgsql_logerror(&sql); + goto out; + } + +out: ; + + kore_pgsql_cleanup(&sql); + + http_response(req, status, NULL, 0); + + return KORE_RESULT_OK; +} + static int render_md(const char *in, struct kore_buf *out) { int err = 0; -- cgit v1.2.3