From 7d9765df281da6ad6debe07b2047affec3ef2e17 Mon Sep 17 00:00:00 2001 From: Michael McVady Date: Fri, 12 Jan 2024 15:17:14 -0600 Subject: Hack in edit --- assets/entry_edit.html | 6 +-- assets/error.html | 4 +- conf/clog.conf | 26 +++++------ src/clog.c | 118 ++++++++++++++++++++++++++++++++++++------------- src/queries.h | 10 ++--- 5 files changed, 107 insertions(+), 57 deletions(-) diff --git a/assets/entry_edit.html b/assets/entry_edit.html index 492735a..01e5cd5 100644 --- a/assets/entry_edit.html +++ b/assets/entry_edit.html @@ -1,9 +1,9 @@ -
+
- - + +
diff --git a/assets/error.html b/assets/error.html index 78005bf..f3b5826 100644 --- a/assets/error.html +++ b/assets/error.html @@ -1,4 +1,2 @@ -
-

%s

-
+

%s

diff --git a/conf/clog.conf b/conf/clog.conf index e787ec3..e305ca4 100644 --- a/conf/clog.conf +++ b/conf/clog.conf @@ -18,7 +18,7 @@ privsep worker { seccomp_tracing no validator v_uuid regex ^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$ -# validator v_text function v_example_func +validator v_text function validate_text domain * { attach notls @@ -40,26 +40,20 @@ domain * { methods get } + route ^/entries/[a-z0-9\-]+$ { + handler update_entry + methods post + + validate post id v_uuid + validate post title v_text + validate post body v_text + } + route ^/entries/[a-z0-9\-]+/edit$ { handler edit_entry methods get } - # toy endpoint - # route /form { - # handler get_entry_form - # methods get - # } - - # route /form { - # handler post_entry_form - # methods post - - # validate post id v_uuid - # validate post title v_text - # validate post body v_text - # } - route ^/.*$ { handler redirect } diff --git a/src/clog.c b/src/clog.c index ed50a17..6f54d0f 100644 --- a/src/clog.c +++ b/src/clog.c @@ -59,6 +59,7 @@ void entry_query_init(struct entry_query *eq, const char *id); void entry_query_cleanup(struct entry_query *eq); int validate_uuid(const char *uuid); +int validate_text(struct http_request *req, char *data); int http_ok_resp(struct http_request *req, enum http_status_code status, struct kore_buf *content); int http_err_resp(struct http_request *req, enum http_status_code status); @@ -68,13 +69,14 @@ int redirect(struct http_request *req); 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 v_example_func(struct http_request *req, char *data); int sql_select(struct entry_query *eq); int sql_update(const char *id, const char *title, const char *body); -int sql_delete(const char *id); -int sql_insert(const char *id, const char *title, const char *body); +// int sql_delete(const char *id); +// int sql_insert(const char *id, const char *title, const char *body); static void process_md_output(const MD_CHAR *html, MD_SIZE size, void *buf); static int render_md(const char *in, struct kore_buf *out); @@ -136,6 +138,11 @@ int validate_uuid(const char *uuid) { return KORE_RESULT_OK; } +int validate_text(struct http_request *req, char *data) { + kore_log(LOG_NOTICE, "v_example_func called %s", data); + return KORE_RESULT_OK; +} + int http_ok_resp( struct http_request *req, enum http_status_code status, @@ -180,11 +187,6 @@ int redirect(struct http_request *req) { return KORE_RESULT_OK; } -// int v_example_func(struct http_request *req, char *data) { -// kore_log(LOG_NOTICE, "v_example_func called"); -// return KORE_RESULT_OK; -// } - int get_index(struct http_request *req) { struct entry_query eq; @@ -309,7 +311,7 @@ int edit_entry(struct http_request *req) { id = kore_strdup(req->path + strlen("/entries/")); id[strlen(id) - strlen("/edit")] = '\0'; - + entry_query_init(&eq, (const char*) id); // Check for valid resource UUID @@ -333,7 +335,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]->title, eq.entries[0]->body + eq.entries[0]->id, eq.entries[0]->id, eq.entries[0]->title, eq.entries[0]->body ); // Render MD. @@ -363,6 +365,62 @@ out: ; return KORE_RESULT_OK; } +int update_entry(struct http_request *req) { + int err = 0; + + char *id = NULL; + 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; + } + + id = kore_strdup(req->path + strlen("/entries/")); + kore_log(LOG_DEBUG, "updating entry %s.", id); + + err = validate_uuid(id); + if (err == KORE_RESULT_ERROR) { + kore_log(LOG_ERR, "Invalid entry id %s.", id); + http_err_resp(req, HTTP_STATUS_NOT_FOUND); + goto out; + } + + err = sql_update(id, title, body); + // err = KORE_RESULT_ERROR; + if (err == KORE_RESULT_ERROR) { + kore_log(LOG_ERR, "Error updating entry id %s.", id); + http_err_resp(req, HTTP_STATUS_INTERNAL_ERROR); + goto out; + } + + http_err_resp(req, HTTP_STATUS_CREATED); + +out: ; + + if (id != NULL) { + kore_free(id); + } + + return KORE_RESULT_OK; +} + int sql_select(struct entry_query *eq) { int err = KORE_RESULT_OK; @@ -433,33 +491,33 @@ out: ; return err; } -// int sql_update_entry(const char *id, const char *title, const char *body) { -// int err = KORE_RESULT_OK; +int sql_update(const char *id, const char *title, const char *body) { + int err = KORE_RESULT_OK; -// struct kore_pgsql sql; -// kore_pgsql_init(&sql); + struct kore_pgsql sql; + kore_pgsql_init(&sql); -// err = kore_pgsql_setup(&sql, database, KORE_PGSQL_SYNC); -// if (err == KORE_RESULT_ERROR) { -// kore_pgsql_logerror(&sql); -// goto out; -// } + err = kore_pgsql_setup(&sql, database, KORE_PGSQL_SYNC); + if (err == KORE_RESULT_ERROR) { + kore_pgsql_logerror(&sql); + goto out; + } -// err = kore_pgsql_query_params( -// &sql, q_update_entry, 0, 3, KORE_PGSQL_PARAM_TEXT(title), KORE_PGSQL_PARAM_TEXT(body), -// KORE_PGSQL_PARAM_TEXT(id) -// ); -// if (err == KORE_RESULT_ERROR) { -// kore_pgsql_logerror(&sql); -// goto out; -// } + err = kore_pgsql_query_params( + &sql, q_update_entry, 0, 3, KORE_PGSQL_PARAM_TEXT(title), KORE_PGSQL_PARAM_TEXT(body), + KORE_PGSQL_PARAM_TEXT(id) + ); + if (err == KORE_RESULT_ERROR) { + kore_pgsql_logerror(&sql); + goto out; + } -// out: ; +out: ; -// kore_pgsql_cleanup(&sql); + kore_pgsql_cleanup(&sql); -// return err; -// } + return err; +} // int sql_delete_entry(const char *id) { // int err = KORE_RESULT_OK; diff --git a/src/queries.h b/src/queries.h index 3956129..1246091 100644 --- a/src/queries.h +++ b/src/queries.h @@ -22,11 +22,11 @@ const char *q_select_entries = // "($1, $2, $3) " // "RETURNING id;"; -// const char *q_update_entry = -// "UPDATE entries " -// "SET title = $1, body = $2, updated_at = NOW() " -// "WHERE id = $3 " -// "RETURNING id;"; +const char *q_update_entry = +"UPDATE entries " +"SET title = $1, body = $2, updated_at = NOW() " +"WHERE id = $3 " +"RETURNING id;"; // const char *q_delete_entry = // "DELETE " -- cgit v1.2.3