From 98f09fa36fac0048ce2096cf3dddd7e7e1aa1b68 Mon Sep 17 00:00:00 2001 From: Michael McVady Date: Sun, 24 Mar 2024 18:37:24 -0500 Subject: Add delete --- src/clog.c | 77 ++++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 53 insertions(+), 24 deletions(-) (limited to 'src/clog.c') 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