diff options
| author | Michael McVady <femtonaut@gmail.com> | 2024-01-12 15:17:14 -0600 | 
|---|---|---|
| committer | Michael McVady <femtonaut@gmail.com> | 2024-01-12 15:17:14 -0600 | 
| commit | 7d9765df281da6ad6debe07b2047affec3ef2e17 (patch) | |
| tree | 21fd35283ce1f6538a1129811a6f3a491379c84f /src/clog.c | |
| parent | f810fe19b8152a32254133680e3d6f506e2277e6 (diff) | |
Hack in edit
Diffstat (limited to 'src/clog.c')
| -rw-r--r-- | src/clog.c | 118 | 
1 files changed, 88 insertions, 30 deletions
| @@ -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; | 
