aboutsummaryrefslogtreecommitdiff
path: root/src/clog.c
diff options
context:
space:
mode:
authorMichael McVady <femtonaut@gmail.com>2023-01-02 20:11:22 -0600
committerMichael McVady <femtonaut@gmail.com>2023-01-03 10:54:24 -0600
commit03eafcbb94df67484e80480f30660022e7142608 (patch)
tree7829053df43b34d62d2ba5ab1dfd9e3b4916b040 /src/clog.c
parent1ed181620d489d860bda150acba05d884fec147e (diff)
Clean up query logic
Diffstat (limited to 'src/clog.c')
-rw-r--r--src/clog.c46
1 files changed, 9 insertions, 37 deletions
diff --git a/src/clog.c b/src/clog.c
index 5083d8a..78ca73b 100644
--- a/src/clog.c
+++ b/src/clog.c
@@ -5,6 +5,9 @@
#include "assets.h"
+// FIXME: Why does compilation fail if this is a .c file?
+#include "queries.h"
+
#include "../lib/md4c/src/entity.h"
#include "../lib/md4c/src/entity.c"
#include "../lib/md4c/src/md4c.h"
@@ -199,56 +202,25 @@ render_posts_query(struct post_request *post_req)
}
// Query for posts, check for error.
- if (post_req->type == HTML && post_req->resource)
+ if (post_req->resource)
{
- // Query an HTML post.
+ // Query a post.
err = kore_pgsql_query_params(
&sql,
- "SELECT id, title, created_at::DATE, body "
- "FROM posts "
- "WHERE id = $1;",
+ post_req->type == HTML ? query_html_post : query_json_post,
0, // return string data
1, // param count
KORE_PGSQL_PARAM_TEXT(post_req->resource)
);
}
- else if (post_req->type == HTML)
+ else
{
- // Query all HTML posts.
+ // Query all posts.
err = kore_pgsql_query(
&sql,
- "SELECT id, title, created_at::DATE, body "
- "FROM posts "
- "ORDER BY updated_at DESC;"
+ post_req->type == HTML ? query_html_posts : query_json_posts
);
}
- 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 ("
- "SELECT id, title, body, created_at, updated_at "
- "FROM posts "
- "WHERE id = $1"
- ") row;",
- 0, // string data
- 1, // param count
- KORE_PGSQL_PARAM_TEXT(post_req->resource)
- );
- }
- else if (post_req->type == JSON)
- {
- // Query all JSON posts.
- err = kore_pgsql_query(
- &sql,
- "SELECT JSON_AGG(ROW_TO_JSON(row)) FROM ("
- "SELECT id, title, body, created_at, updated_at "
- "FROM posts "
- "ORDER BY updated_at DESC"
- ") row;"
- );
- } // else { ...
if (err == KORE_RESULT_ERROR)
{