From 98f09fa36fac0048ce2096cf3dddd7e7e1aa1b68 Mon Sep 17 00:00:00 2001 From: Michael McVady Date: Sun, 24 Mar 2024 18:37:24 -0500 Subject: Add delete --- conf/clog.conf | 16 ++++++++---- src/clog.c | 77 ++++++++++++++++++++++++++++++++++++++++------------------ 2 files changed, 64 insertions(+), 29 deletions(-) diff --git a/conf/clog.conf b/conf/clog.conf index 0739f70..c469376 100644 --- a/conf/clog.conf +++ b/conf/clog.conf @@ -58,11 +58,6 @@ domain * { methods get } - route ^/entries/[a-z0-9\-]+/edit$ { - handler edit_entry - methods get - } - route ^/entries/[a-z0-9\-]+$ { handler update_entry methods post @@ -71,6 +66,17 @@ domain * { validate post body v_text } + route ^/entries/[a-z0-9\-]+/delete$ { + handler delete_entry + methods get + } + + route ^/entries/[a-z0-9\-]+/edit$ { + handler edit_entry + methods get + } + + route ^/.*$ { handler redirect } diff --git a/src/clog.c b/src/clog.c index 507b425..7687dff 100644 --- a/src/clog.c +++ b/src/clog.c @@ -70,6 +70,7 @@ int redirect(struct http_request *req); int get_index(struct http_request *req); int get_entry(struct http_request *req); +int delete_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); @@ -329,6 +330,34 @@ out: ; return KORE_RESULT_OK; } +int delete_entry(struct http_request *req) { + int err = 0; + + char *id = NULL; + + id = kore_strdup(req->path + strlen("/entries/")); + id[strlen(id) - strlen("/delete")] = '\0'; + + // Check for valid resource UUID + kore_log(LOG_DEBUG, "Resource id /entries/%s.", id); + err = validate_uuid(eq.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; + } + + int err = sql_delete_entry(id); + + http_ok_resp(req, HTTP_STATUS_OK, content); + +out: ; + kore_free(id); + + return KORE_RESULT_OK; +} + + int edit_entry(struct http_request *req) { int err = 0; @@ -622,36 +651,36 @@ out: ; return err; } -// int sql_delete_entry(const char *id) { -// int err = KORE_RESULT_OK; +int sql_delete_entry(const char *id) { + 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_delete_entry, -// 0, -// 1, -// KORE_PGSQL_PARAM_TEXT(id) -// ); -// if (err == KORE_RESULT_ERROR) { -// kore_pgsql_logerror(&sql); -// goto out; -// } + err = kore_pgsql_query_params( + &sql, + q_delete_entry, + 0, + 1, + 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; +} static int render_md(const char *in, struct kore_buf *out) { // Not a kore err. -- cgit v1.2.3