aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael McVady <femtonaut@gmail.com>2023-01-02 14:38:19 -0600
committerMichael McVady <femtonaut@gmail.com>2023-01-03 10:54:24 -0600
commit1ed181620d489d860bda150acba05d884fec147e (patch)
tree5978f5f6bafd01a6963f891abdcdc2f81302b626
parent832ab13c9dd74fe30533f345d7e9209dff8c89d1 (diff)
Combine JSON & HTML query functions
-rw-r--r--src/clog.c169
1 files changed, 72 insertions, 97 deletions
diff --git a/src/clog.c b/src/clog.c
index 3ec3ee3..5083d8a 100644
--- a/src/clog.c
+++ b/src/clog.c
@@ -48,8 +48,7 @@ int post(struct http_request *req);
int posts(struct http_request *req);
int render_posts(struct http_request *req, const char *resource);
-int render_posts_html(struct post_request *post_req);
-int render_posts_json(struct post_request *post_req);
+int render_posts_query(struct post_request *post_req);
static void process_md_output(const MD_CHAR *, MD_SIZE size, void *);
static int render_md(const char *, struct kore_buf *);
@@ -130,7 +129,7 @@ render_posts(struct http_request *req, const char *resource)
if (post_req.type == JSON)
{
http_response_header(post_req.req, "content-type", "application/json; charset=utf-8");
- (void) render_posts_json(&post_req);
+ (void) render_posts_query(&post_req);
if (post_req.resp_status != HTTP_STATUS_OK)
{
kore_buf_appendf(
@@ -144,7 +143,7 @@ render_posts(struct http_request *req, const char *resource)
{
http_response_header(post_req.req, "content-type", "text/html; charset=utf-8");
kore_buf_append(post_req.resp_buf, asset_index_begin_html, asset_len_index_begin_html);
- (void) render_posts_html(&post_req);
+ (void) render_posts_query(&post_req);
if (post_req.resp_status != HTTP_STATUS_OK)
{
kore_buf_appendf(
@@ -168,8 +167,9 @@ render_posts(struct http_request *req, const char *resource)
return KORE_RESULT_OK;
}
+
int
-render_posts_html(struct post_request *post_req)
+render_posts_query(struct post_request *post_req)
{
int err = 0;
@@ -180,6 +180,8 @@ render_posts_html(struct post_request *post_req)
const char *created_at = NULL;
const char *body = NULL;
+ const char *json = NULL;
+
struct kore_pgsql sql;
kore_pgsql_init(&sql);
@@ -197,9 +199,9 @@ render_posts_html(struct post_request *post_req)
}
// Query for posts, check for error.
- if (post_req->resource)
+ if (post_req->type == HTML && post_req->resource)
{
- // Query a post.
+ // Query an HTML post.
err = kore_pgsql_query_params(
&sql,
"SELECT id, title, created_at::DATE, body "
@@ -210,9 +212,9 @@ render_posts_html(struct post_request *post_req)
KORE_PGSQL_PARAM_TEXT(post_req->resource)
);
}
- else
+ else if (post_req->type == HTML)
{
- // Query all posts.
+ // Query all HTML posts.
err = kore_pgsql_query(
&sql,
"SELECT id, title, created_at::DATE, body "
@@ -220,83 +222,9 @@ render_posts_html(struct post_request *post_req)
"ORDER BY updated_at DESC;"
);
}
-
- if (err == KORE_RESULT_ERROR)
- {
- post_req->resp_status = HTTP_STATUS_INTERNAL_ERROR;
- kore_pgsql_logerror(&sql);
- goto out;
- }
-
- // Iterate over posts and render them.
- rows = kore_pgsql_ntuples(&sql);
-
- if (rows == 0)
- {
- post_req->resp_status = HTTP_STATUS_NOT_FOUND;
- goto out;
- }
-
- for (row = 0; row < rows; row++)
- {
- id = kore_pgsql_getvalue(&sql, row, 0);
- title = kore_pgsql_getvalue(&sql, row, 1);
- created_at = kore_pgsql_getvalue(&sql, row, 2);
- body = kore_pgsql_getvalue(&sql, row, 3);
- kore_log(LOG_DEBUG, "id: '%s'; title '%s'", id, title);
-
- // Allocate a buffer to render the markdown as HTML into.
- struct kore_buf *html_buf = kore_buf_alloc(0);
-
- // Render MD.
- err = render_md(body, html_buf);
- if (err == KORE_RESULT_ERROR)
- {
- kore_log(LOG_ERR, "Error rendering markdown for entry %s.", id);
- kore_buf_free(html_buf);
- continue;
- }
-
- // Append rendered MD post.
- kore_buf_appendf(
- post_req->resp_buf,
- (const char *) asset_post_html,
- title,
- created_at,
- kore_buf_stringify(html_buf, NULL)
- );
-
- kore_buf_free(html_buf);
- }
-
-out: ;
-
- kore_pgsql_cleanup(&sql);
-
- return KORE_RESULT_OK;
-}
-
-int
-render_posts_json(struct post_request *post_req)
-{
- int err = 0;
-
- const char *json = NULL;
-
- struct kore_pgsql sql;
-
- kore_pgsql_init(&sql);
-
- err = kore_pgsql_setup(&sql, database, KORE_PGSQL_SYNC);
- if (err == KORE_RESULT_ERROR)
- {
- post_req->resp_status = HTTP_STATUS_INTERNAL_ERROR;
- kore_pgsql_logerror(&sql);
- goto out;
- }
-
- if (post_req->resource)
+ else if (post_req->type == JSON && post_req->resource)
{
+ // Query a JSON post.
err = kore_pgsql_query_params(
&sql,
"SELECT JSON_AGG(ROW_TO_JSON(row)) FROM ("
@@ -309,8 +237,9 @@ render_posts_json(struct post_request *post_req)
KORE_PGSQL_PARAM_TEXT(post_req->resource)
);
}
- else
+ else if (post_req->type == JSON)
{
+ // Query all JSON posts.
err = kore_pgsql_query(
&sql,
"SELECT JSON_AGG(ROW_TO_JSON(row)) FROM ("
@@ -319,7 +248,7 @@ render_posts_json(struct post_request *post_req)
"ORDER BY updated_at DESC"
") row;"
);
- }
+ } // else { ...
if (err == KORE_RESULT_ERROR)
{
@@ -328,19 +257,65 @@ render_posts_json(struct post_request *post_req)
goto out;
}
- // XXX Always tuples from the above Postgres queries, need to check the length for results.
- if (kore_pgsql_getlength(&sql, 0, 0) == 0)
+ if (post_req->type == HTML)
{
- post_req->resp_status = HTTP_STATUS_NOT_FOUND;
- goto out;
+ // Iterate over posts and render them.
+ rows = kore_pgsql_ntuples(&sql);
+
+ if (rows == 0)
+ {
+ post_req->resp_status = HTTP_STATUS_NOT_FOUND;
+ goto out;
+ }
+
+ for (row = 0; row < rows; row++)
+ {
+ id = kore_pgsql_getvalue(&sql, row, 0);
+ title = kore_pgsql_getvalue(&sql, row, 1);
+ created_at = kore_pgsql_getvalue(&sql, row, 2);
+ body = kore_pgsql_getvalue(&sql, row, 3);
+ kore_log(LOG_DEBUG, "id: '%s'; title '%s'", id, title);
+
+ // Allocate a buffer to render the markdown as HTML into.
+ struct kore_buf *html_buf = kore_buf_alloc(0);
+
+ // Render MD.
+ err = render_md(body, html_buf);
+ if (err == KORE_RESULT_ERROR)
+ {
+ kore_log(LOG_ERR, "Error rendering markdown for entry %s.", id);
+ kore_buf_free(html_buf);
+ continue;
+ }
+
+ // Append rendered MD post.
+ kore_buf_appendf(
+ post_req->resp_buf,
+ (const char *) asset_post_html,
+ title,
+ created_at,
+ kore_buf_stringify(html_buf, NULL)
+ );
+
+ kore_buf_free(html_buf);
+ }
}
+ else if (post_req->type == JSON)
+ {
+ // XXX Always tuples from the above Postgres queries, need to check the length for results.
+ if (kore_pgsql_getlength(&sql, 0, 0) == 0)
+ {
+ post_req->resp_status = HTTP_STATUS_NOT_FOUND;
+ goto out;
+ }
- json = kore_pgsql_getvalue(&sql, 0, 0);
- kore_buf_append(
- post_req->resp_buf,
- json,
- strlen(json)
- );
+ json = kore_pgsql_getvalue(&sql, 0, 0);
+ kore_buf_append(
+ post_req->resp_buf,
+ json,
+ strlen(json)
+ );
+ } // else { ...
out: ;