aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael McVady <femtonaut@gmail.com>2023-01-05 21:50:57 -0600
committerMichael McVady <femtonaut@gmail.com>2023-01-05 21:50:57 -0600
commit141fcc9db67753a77331080f94296ad15cad72af (patch)
tree90e112de5621fa743012ecbf562a1304b198bb2e /src
parentb8b74ce23c645b1dd860acce90ba3355660cc6a0 (diff)
Flesh out toy endpoint more
Diffstat (limited to 'src')
-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);