aboutsummaryrefslogtreecommitdiff
path: root/src/clog.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/clog.c')
-rw-r--r--src/clog.c66
1 files changed, 44 insertions, 22 deletions
diff --git a/src/clog.c b/src/clog.c
index c2a7abb..cb9e291 100644
--- a/src/clog.c
+++ b/src/clog.c
@@ -56,7 +56,9 @@ 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);
+int toys(struct http_request *req);
+int get_toys(struct http_request *req);
+int post_toys(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 *);
@@ -315,27 +317,43 @@ out: ;
}
int
-toy(struct http_request *req) {
+toys(struct http_request *req) {
+ if (req->method == HTTP_METHOD_GET)
+ get_toys(req);
+ else if (req->method == HTTP_METHOD_POST)
+ post_toys(req);
+
+ return KORE_RESULT_OK;
+}
+
+int
+get_toys(struct http_request *req) {
+
+ http_response(req, HTTP_STATUS_OK, "OK", strlen("OK"));
+
+ return KORE_RESULT_OK;
+}
+
+int
+post_toys(struct http_request *req) {
int err = 0;
int status = HTTP_STATUS_OK;
struct kore_json_item *item = NULL;
+ const char *id = NULL;
+ const char *title = NULL;
+ const char *body = 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 *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_json_init(&json, json_str, strlen(json_str));
+ kore_json_init(&json, req->http_body->data, req->http_body->length);
kore_pgsql_init(&sql);
@@ -347,7 +365,8 @@ toy(struct http_request *req) {
item = kore_json_find_string(json.root, "id");
if (item != NULL) {
- kore_log(LOG_INFO, "id = '%s'\n", item->data.string);
+ id = item->data.string;
+ kore_log(LOG_INFO, "id = '%s'\n", id);
} else {
status = HTTP_STATUS_INTERNAL_ERROR;
kore_log(LOG_ERR, "error parsing id: %s\n", kore_json_strerror());
@@ -355,16 +374,17 @@ toy(struct http_request *req) {
}
// Check for valid resource ID/UUID
- err = validate_uuid(item->data.string);
+ err = validate_uuid(id);
if (err == KORE_RESULT_ERROR) {
status = HTTP_STATUS_NOT_FOUND;
- kore_log(LOG_ERR, "Invalid post UUID %s.", item->data.string);
+ kore_log(LOG_ERR, "Invalid post UUID %s.", id);
goto out;
}
item = kore_json_find_string(json.root, "title");
if (item != NULL) {
- kore_log(LOG_INFO, "title = '%s'\n", item->data.string);
+ title = item->data.string;
+ kore_log(LOG_INFO, "title = '%s'\n", title);
} else {
status = HTTP_STATUS_INTERNAL_ERROR;
kore_log(LOG_ERR, "error parsing title: %s\n", kore_json_strerror());
@@ -373,7 +393,8 @@ toy(struct http_request *req) {
item = kore_json_find_string(json.root, "body");
if (item != NULL) {
- kore_log(LOG_INFO, "body = '%s'\n", item->data.string);
+ body = item->data.string;
+ kore_log(LOG_INFO, "body = '%s'\n", body);
} else {
status = HTTP_STATUS_INTERNAL_ERROR;
kore_log(LOG_ERR, "error parsing body: %s\n", kore_json_strerror());
@@ -391,12 +412,13 @@ toy(struct http_request *req) {
// err = kore_pgsql_query(
err = kore_pgsql_query_params(
&sql,
- q,
+ "INSERT INTO posts (id, title, body) "
+ "VALUES ($1, $2, $3);",
0,
3,
- KORE_PGSQL_PARAM_TEXT("00000000-0000-0000-0000-000000000000"),
- KORE_PGSQL_PARAM_TEXT("title"),
- KORE_PGSQL_PARAM_TEXT("body")
+ KORE_PGSQL_PARAM_TEXT(id),
+ KORE_PGSQL_PARAM_TEXT(title),
+ KORE_PGSQL_PARAM_TEXT(body)
);
if (err == KORE_RESULT_ERROR) {
@@ -407,8 +429,8 @@ toy(struct http_request *req) {
out: ;
- kore_pgsql_cleanup(&sql);
kore_json_cleanup(&json);
+ kore_pgsql_cleanup(&sql);
http_response(req, status, NULL, 0);