aboutsummaryrefslogtreecommitdiff
path: root/src/clog.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/clog.c')
-rw-r--r--src/clog.c85
1 files changed, 80 insertions, 5 deletions
diff --git a/src/clog.c b/src/clog.c
index d4eeb36..124c618 100644
--- a/src/clog.c
+++ b/src/clog.c
@@ -231,7 +231,7 @@ render_posts_query(struct post_request *post_req) {
}
// Query for posts, check for error.
- if (post_req->resource) {
+ if (post_req->resource != NULL) {
// Query a post.
err = kore_pgsql_query_params(
&sql,
@@ -320,10 +320,78 @@ toy(struct http_request *req) {
int status = HTTP_STATUS_OK;
+ struct kore_json_item *item = NULL;
+
+ struct kore_json json;
struct kore_pgsql sql;
+ const char *json_str = "{\"id\": \"00000000-0000-0000-0000-000000000000\", "
+ "\"title\": \"title\", \"body\": \"body\"}";
+
+ // const char *q = "INSERT INTO posts (id, title, body) "
+ // "VALUES ('00000000-0000-0000-0000-000000000000', 'title', 'body');";
+ // 'af5bbaec-8d2b-11ed-9bf3-db5637a87ac5'
+
+ const char *q = "INSERT INTO posts (id, title, body) "
+ "VALUES ($1, $2, $3);";
+
+ kore_json_init(&json, json_str, strlen(json_str));
+
kore_pgsql_init(&sql);
+ if (!kore_json_parse(&json)) {
+ status = HTTP_STATUS_INTERNAL_ERROR;
+ kore_log(LOG_ERR, "error parsing json: %s\n", kore_json_strerror());
+ goto out;
+ }
+
+ item = kore_json_find_string(json.root, "id");
+ if (item != NULL) {
+ kore_log(LOG_INFO, "id = '%s'\n", item->data.string);
+ } else {
+ status = HTTP_STATUS_INTERNAL_ERROR;
+ kore_log(LOG_ERR, "error parsing id: %s\n", kore_json_strerror());
+ goto out;
+ }
+
+ // Check for valid resource ID/UUID
+ err = validate_uuid(item->data.string);
+ if (err == KORE_RESULT_ERROR) {
+ status = HTTP_STATUS_NOT_FOUND;
+ kore_log(LOG_ERR, "Invalid post UUID %s.", item->data.string);
+ goto out;
+ }
+
+ item = kore_json_find_string(json.root, "title");
+ if (item != NULL) {
+ kore_log(LOG_INFO, "title = '%s'\n", item->data.string);
+ } else {
+ status = HTTP_STATUS_INTERNAL_ERROR;
+ kore_log(LOG_ERR, "error parsing title: %s\n", kore_json_strerror());
+ goto out;
+ }
+
+ item = kore_json_find_string(json.root, "body");
+ if (item != NULL) {
+ kore_log(LOG_INFO, "body = '%s'\n", item->data.string);
+ } else {
+ status = HTTP_STATUS_INTERNAL_ERROR;
+ kore_log(LOG_ERR, "error parsing body: %s\n", kore_json_strerror());
+ goto out;
+ }
+
+ /*
+ item = kore_json_find_integer_u64(json.root, "foo/integer");
+
+ if (item != NULL) {
+ kore_buf_appendf(&buf,
+ "foo.integer = '%" PRIu64 "'\n", item->data.u64);
+ } else {
+ kore_buf_appendf(&buf, "foo.integer %s\n",
+ kore_json_strerror());
+ }
+ */
+
err = kore_pgsql_setup(&sql, database, KORE_PGSQL_SYNC);
if (err == KORE_RESULT_ERROR) {
status = HTTP_STATUS_INTERNAL_ERROR;
@@ -331,11 +399,17 @@ toy(struct http_request *req) {
goto out;
}
- // Query a post.
- err = kore_pgsql_query(
+ // Insert a post.
+ // err = kore_pgsql_query(
+ err = kore_pgsql_query_params(
&sql,
- "INSERT INTO posts (id, title, body) VALUES ('00000000-0000-0000-0000-000000000000', 'title', 'body');"
- // 'af5bbaec-8d2b-11ed-9bf3-db5637a87ac5'
+ q,
+ 0,
+ 3,
+ KORE_PGSQL_PARAM_TEXT("00000000-0000-0000-0000-000000000000"),
+ // 'af5bbaec-8d2b-11ed-9bf3-db5637a87ac5'
+ KORE_PGSQL_PARAM_TEXT("title"),
+ KORE_PGSQL_PARAM_TEXT("body")
);
if (err == KORE_RESULT_ERROR) {
@@ -347,6 +421,7 @@ toy(struct http_request *req) {
out: ;
kore_pgsql_cleanup(&sql);
+ kore_json_cleanup(&json);
http_response(req, status, NULL, 0);