From 2d92280f0e7e605911da199757fa33d2b5089b0a Mon Sep 17 00:00:00 2001 From: Michael McVady Date: Tue, 16 Jan 2024 21:32:22 -0600 Subject: Support creating new entries --- src/clog.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 78 insertions(+), 11 deletions(-) (limited to 'src/clog.c') diff --git a/src/clog.c b/src/clog.c index 30de75a..04f5bb2 100644 --- a/src/clog.c +++ b/src/clog.c @@ -16,11 +16,11 @@ #include "queries.h" // FIXME: Compilation fail if this is a .c file. KORE_SECCOMP_FILTER("clog", - KORE_SYSCALL_ALLOW(bind), - KORE_SYSCALL_ALLOW(getdents64), - KORE_SYSCALL_ALLOW(newfstatat), - KORE_SYSCALL_ALLOW(sendmmsg), - KORE_SYSCALL_ALLOW(uname) + KORE_SYSCALL_ALLOW(bind), + KORE_SYSCALL_ALLOW(getdents64), + KORE_SYSCALL_ALLOW(newfstatat), + KORE_SYSCALL_ALLOW(sendmmsg), + KORE_SYSCALL_ALLOW(uname) ) enum query_status { QUERY_STATUS_OK, QUERY_STATUS_ERROR, QUERY_STATUS_NOT_FOUND }; @@ -68,6 +68,8 @@ int get_index(struct http_request *req); int get_entry(struct http_request *req); int edit_entry(struct http_request *req); int update_entry(struct http_request *req); +int get_new_entry_form(struct http_request *req); +int save_new_entry(struct http_request *req); int sql_select(struct entry_query *eq); int sql_update(const char *id, const char *title, const char *body); @@ -364,7 +366,7 @@ int edit_entry(struct http_request *req) { kore_buf_appendf( content, (const char *) asset_entry_edit_html, - eq.entries[0]->id, eq.entries[0]->id, eq.entries[0]->title, eq.entries[0]->body + eq.entries[0]->id, eq.entries[0]->title, eq.entries[0]->body ); // Render MD. @@ -447,6 +449,63 @@ out: ; return KORE_RESULT_OK; } +int get_new_entry_form(struct http_request *req) { + struct kore_buf *content = NULL; + + content = kore_buf_alloc(0); + + kore_buf_appendf( + content, (const char *) asset_entry_edit_html, + "", "", "" + ); + + http_ok_resp(req, HTTP_STATUS_OK, content); + + kore_free(content); + + return KORE_RESULT_OK; +} + +int save_new_entry(struct http_request *req) { + int err = 0; + + char *title = NULL; + char *body = NULL; + + http_populate_post(req); + + if (http_argument_get_string(req, "title", &title)) { + kore_log(LOG_INFO, "form title %s.", title); + } + else { + kore_log(LOG_ERR, "Error no title"); + http_err_resp(req, HTTP_STATUS_BAD_REQUEST); + goto out; + } + + if (http_argument_get_string(req, "body", &body)) { + kore_log(LOG_INFO, "form body %s.", body); + } + else { + kore_log(LOG_ERR, "Error no body"); + http_err_resp(req, HTTP_STATUS_BAD_REQUEST); + goto out; + } + + err = sql_update(NULL, title, body); + if (err == KORE_RESULT_ERROR) { + kore_log(LOG_ERR, "Error saving new entry."); + http_err_resp(req, HTTP_STATUS_INTERNAL_ERROR); + goto out; + } + + http_err_resp(req, HTTP_STATUS_CREATED); + +out: ; + + return KORE_RESULT_OK; +} + int sql_select(struct entry_query *eq) { int err = KORE_RESULT_OK; @@ -502,7 +561,7 @@ int sql_select(struct entry_query *eq) { // Iterate over entries and render them. eq->num_entries = kore_pgsql_ntuples(&sql); for (i = 0; i < eq->num_entries; i++) { - // Fetch & copy data to entry + // Fetch & copy data to entry entry = kore_malloc(sizeof(struct entry)); entry->id = kore_strdup(kore_pgsql_getvalue(&sql, i, 0)); entry->title = kore_strdup(kore_pgsql_getvalue(&sql, i, 1)); @@ -535,10 +594,18 @@ int sql_update(const char *id, const char *title, const char *body) { goto out; } - err = kore_pgsql_query_params( - &sql, q_update_entry, 0, 4, KORE_PGSQL_PARAM_TEXT(title), KORE_PGSQL_PARAM_TEXT(body), - KORE_PGSQL_PARAM_TEXT(body), KORE_PGSQL_PARAM_TEXT(id) - ); + if (id) { + err = kore_pgsql_query_params( + &sql, q_update_entry, 0, 4, KORE_PGSQL_PARAM_TEXT(title), KORE_PGSQL_PARAM_TEXT(body), + KORE_PGSQL_PARAM_TEXT(body), KORE_PGSQL_PARAM_TEXT(id) + ); + } + else { + err = kore_pgsql_query_params( + &sql, q_insert_entry, 0, 3, KORE_PGSQL_PARAM_TEXT(title), KORE_PGSQL_PARAM_TEXT(body), + KORE_PGSQL_PARAM_TEXT(body) + ); + } if (err == KORE_RESULT_ERROR) { kore_pgsql_logerror(&sql); goto out; -- cgit v1.2.3