diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/clog.c | 188 | ||||
-rw-r--r-- | src/queries.h | 25 |
2 files changed, 106 insertions, 107 deletions
@@ -27,7 +27,7 @@ enum content_type { CONTENT_JSON, CONTENT_X_WWW_FORM_URLENCODED }; enum query_status { QUERY_STATUS_OK, QUERY_STATUS_ERROR, QUERY_STATUS_NOT_FOUND }; -struct post_query { +struct entry_query { char *id; int status; @@ -44,8 +44,8 @@ static const char * const error_msg[] = { [HTTP_STATUS_INTERNAL_ERROR] = "There was an error processing the request.", // 500 }; -void post_query_init(struct post_query *pq, const char *id); -void post_query_cleanup(struct post_query *pq); +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); @@ -62,44 +62,44 @@ int http_err_resp( int redirect(struct http_request *req); -int get_posts(struct http_request *req); -int get_posts_resource(struct http_request *req); -int post_posts(struct http_request *req); -int put_posts(struct http_request *req); -int delete_posts(struct http_request *req); +int get_entries(struct http_request *req); +int get_entry(struct http_request *req); +int post_entry(struct http_request *req); +int put_entry(struct http_request *req); +int delete_entry(struct http_request *req); int v_example_func(struct http_request *req, char *data); -int get_form(struct http_request *req); -int post_form(struct http_request *req); +int get_entry_form(struct http_request *req); +int post_entry_form(struct http_request *req); -int sql_select_posts(struct post_query *pq); -int sql_update_posts(const char *id, const char *title, const char *body); -int sql_delete_posts(const char *id); -int sql_insert_posts(const char *id, const char *title, const char *body); -int sql_render_posts(struct kore_pgsql *sql, struct post_query *pq); +int sql_select_entries(struct entry_query *eq); +int sql_update_entry(const char *id, const char *title, const char *body); +int sql_delete_entry(const char *id); +int sql_insert_entry(const char *id, const char *title, const char *body); +int sql_render_entry(struct kore_pgsql *sql, struct entry_query *eq); static void process_md_output(const MD_CHAR *, MD_SIZE size, void *); static int render_md(const char *, struct kore_buf *); -void post_query_init(struct post_query *pq, const char *id) { +void entry_query_init(struct entry_query *eq, const char *id) { if (id != NULL) - pq->id = kore_strdup(id); + eq->id = kore_strdup(id); else - pq->id = NULL; + eq->id = NULL; - pq->status = QUERY_STATUS_OK; - pq->result = kore_buf_alloc(0); + eq->status = QUERY_STATUS_OK; + eq->result = kore_buf_alloc(0); } -void post_query_cleanup(struct post_query *pq) { - if (pq->id != NULL) - kore_free((void *) pq->id); - pq->id = NULL; +void entry_query_cleanup(struct entry_query *eq) { + if (eq->id != NULL) + kore_free((void *) eq->id); + eq->id = NULL; - if (pq->result != NULL) - kore_buf_free(pq->result); - pq->result = NULL; + if (eq->result != NULL) + kore_buf_free(eq->result); + eq->result = NULL; } int validate_uuid(const char *uuid) { @@ -193,13 +193,13 @@ int v_example_func(struct http_request *req, char *data) { return KORE_RESULT_OK; } -int get_form(struct http_request *req) { +int get_entry_form(struct http_request *req) { struct kore_buf *resp_buf = kore_buf_alloc(0); kore_buf_append( resp_buf, - (const char *) asset_post_edit_html, - asset_len_post_edit_html + (const char *) asset_entry_edit_html, + asset_len_entry_edit_html ); http_ok_resp(req, HTTP_STATUS_OK, resp_buf); @@ -210,7 +210,7 @@ int get_form(struct http_request *req) { } -int post_form(struct http_request *req) { +int post_entry_form(struct http_request *req) { struct kore_buf *html_buf = kore_buf_alloc(0); struct kore_buf *resp_buf = kore_buf_alloc(0); @@ -236,14 +236,14 @@ int post_form(struct http_request *req) { // Render MD. (void) render_md(body, html_buf); // if (err == KORE_RESULT_ERROR) { - // kore_log(LOG_ERR, "Error rendering markdown for post %s.", id); + // kore_log(LOG_ERR, "Error rendering markdown for entry %s.", id); // continue; // } - // Append rendered MD post. + // Append rendered MD entry. kore_buf_appendf( resp_buf, - (const char *) asset_post_html, + (const char *) asset_entry_html, id, title, "1981-12-23 06:02:00", @@ -259,54 +259,54 @@ int post_form(struct http_request *req) { } -int get_posts(struct http_request *req) { - struct post_query pq; +int get_entries(struct http_request *req) { + struct entry_query eq; - post_query_init(&pq, NULL); + entry_query_init(&eq, NULL); - (void) sql_select_posts(&pq); - if (pq.status != QUERY_STATUS_OK) + (void) sql_select_entries(&eq); + if (eq.status != QUERY_STATUS_OK) http_err_resp(req, HTTP_STATUS_INTERNAL_ERROR); else - http_ok_resp(req, HTTP_STATUS_OK, pq.result); + http_ok_resp(req, HTTP_STATUS_OK, eq.result); - post_query_cleanup(&pq); + entry_query_cleanup(&eq); return KORE_RESULT_OK; } -int get_posts_resource(struct http_request *req) { +int get_entry(struct http_request *req) { int err = 0; - struct post_query pq; + struct entry_query eq; - post_query_init(&pq, req->path + strlen("/posts/")); + entry_query_init(&eq, req->path + strlen("/entries/")); // Check for valid resource UUID - kore_log(LOG_DEBUG, "Resource id /posts/%s.", pq.id); - err = validate_uuid(pq.id); + kore_log(LOG_DEBUG, "Resource id /entries/%s.", eq.id); + err = validate_uuid(eq.id); if (err == KORE_RESULT_ERROR) { - kore_log(LOG_ERR, "Invalid post id %s.", pq.id); + kore_log(LOG_ERR, "Invalid entry id %s.", eq.id); http_err_resp(req, HTTP_STATUS_NOT_FOUND); goto out; } - (void) sql_select_posts(&pq); - if (pq.status == QUERY_STATUS_NOT_FOUND) + (void) sql_select_entries(&eq); + if (eq.status == QUERY_STATUS_NOT_FOUND) http_err_resp(req, HTTP_STATUS_NOT_FOUND); - else if (pq.status == QUERY_STATUS_ERROR) + else if (eq.status == QUERY_STATUS_ERROR) http_err_resp(req, HTTP_STATUS_INTERNAL_ERROR); else - http_ok_resp(req, HTTP_STATUS_OK, pq.result); + http_ok_resp(req, HTTP_STATUS_OK, eq.result); out: ; - post_query_cleanup(&pq); + entry_query_cleanup(&eq); return KORE_RESULT_OK; } -int post_posts(struct http_request *req) { +int post_entry(struct http_request *req) { int err = 0; int status = HTTP_STATUS_CREATED; @@ -334,7 +334,7 @@ int post_posts(struct http_request *req) { err = validate_uuid(id); if (err == KORE_RESULT_ERROR) { status = HTTP_STATUS_BAD_REQUEST; - kore_log(LOG_ERR, "Invalid post UUID %s.", id); + kore_log(LOG_ERR, "Invalid entry UUID %s.", id); goto out; } } @@ -359,7 +359,7 @@ int post_posts(struct http_request *req) { goto out; } - err = sql_insert_posts(id, title, body); + err = sql_insert_entry(id, title, body); if (err == KORE_RESULT_ERROR) { status = HTTP_STATUS_INTERNAL_ERROR; @@ -375,7 +375,7 @@ out: ; return KORE_RESULT_OK; } -int put_posts(struct http_request *req) { +int put_entry(struct http_request *req) { int err = 0; int status = HTTP_STATUS_OK; @@ -389,13 +389,13 @@ int put_posts(struct http_request *req) { kore_json_init(&json, req->http_body->data, req->http_body->length); - id = req->path + strlen("/posts/"); + id = req->path + strlen("/entries/"); // Check for valid resource UUID - kore_log(LOG_DEBUG, "Resource id /posts/%s.", id); + kore_log(LOG_DEBUG, "Resource id /entries/%s.", id); err = validate_uuid(id); if (err == KORE_RESULT_ERROR) { - kore_log(LOG_ERR, "Invalid post id %s.", id); + kore_log(LOG_ERR, "Invalid entry id %s.", id); http_err_resp(req, HTTP_STATUS_NOT_FOUND); goto out; } @@ -426,7 +426,7 @@ int put_posts(struct http_request *req) { goto out; } - err = sql_update_posts(id, title, body); + err = sql_update_entry(id, title, body); if (err == KORE_RESULT_ERROR) { status = HTTP_STATUS_INTERNAL_ERROR; @@ -442,54 +442,54 @@ out: ; return KORE_RESULT_OK; } -int sql_select_posts(struct post_query *pq) { +int sql_select_entries(struct entry_query *eq) { int err = KORE_RESULT_OK; struct kore_pgsql sql; kore_pgsql_init(&sql); - pq->status = QUERY_STATUS_OK; + eq->status = QUERY_STATUS_OK; // Set up synchronous database handle. err = kore_pgsql_setup(&sql, database, KORE_PGSQL_SYNC); if (err == KORE_RESULT_ERROR) { - pq->status = QUERY_STATUS_ERROR; + eq->status = QUERY_STATUS_ERROR; kore_pgsql_logerror(&sql); goto out; } - // Query for posts, check for error. - if (pq->id != NULL) { - // Query a post. + // Query for entries, check for error. + if (eq->id != NULL) { + // Query an entry. err = kore_pgsql_query_params( &sql, - q_select_post, + q_select_entry, 0, // return string data 1, // param count - KORE_PGSQL_PARAM_TEXT(pq->id) + KORE_PGSQL_PARAM_TEXT(eq->id) ); } else { - // Query all posts. + // Query all entries. err = kore_pgsql_query( &sql, - q_select_posts + q_select_entries ); } if (err == KORE_RESULT_ERROR) { - pq->status = QUERY_STATUS_ERROR; + eq->status = QUERY_STATUS_ERROR; kore_pgsql_logerror(&sql); goto out; } // TODO: Add test for this; When database is empty, don't return 404 for base request. - if (pq->id != NULL && kore_pgsql_ntuples(&sql) == 0) { - pq->status = QUERY_STATUS_NOT_FOUND; + if (eq->id != NULL && kore_pgsql_ntuples(&sql) == 0) { + eq->status = QUERY_STATUS_NOT_FOUND; goto out; } - (void) sql_render_posts(&sql, pq); + (void) sql_render_entry(&sql, eq); out: ; @@ -498,28 +498,28 @@ out: ; return err; } -int delete_posts(struct http_request *req) { +int delete_entry(struct http_request *req) { int err = 0; - const char *id = req->path + strlen("/posts/"); + const char *id = req->path + strlen("/entries/"); // Check for valid resource UUID - kore_log(LOG_DEBUG, "Resource id /posts/%s.", id); + kore_log(LOG_DEBUG, "Resource id /entries/%s.", id); err = validate_uuid(id); if (err == KORE_RESULT_ERROR) { - kore_log(LOG_ERR, "Invalid post id %s.", id); + kore_log(LOG_ERR, "Invalid entry id %s.", id); http_err_resp(req, HTTP_STATUS_NOT_FOUND); goto out; } - err = sql_delete_posts(id); + err = sql_delete_entry(id); // FIXME: should 404 if id doesn't exist. - // if (pq.status == QUERY_STATUS_NOT_FOUND) + // if (eq.status == QUERY_STATUS_NOT_FOUND) // http_err_resp(req, HTTP_STATUS_NOT_FOUND); if (err == KORE_RESULT_ERROR) http_err_resp(req, HTTP_STATUS_INTERNAL_ERROR); else - // TODO: test this, it should explode. + // TODO: test this; the test should explode. http_err_resp(req, HTTP_STATUS_OK); out: ; @@ -527,7 +527,7 @@ out: ; return KORE_RESULT_OK; } -int sql_render_posts(struct kore_pgsql *sql, struct post_query *pq) { +int sql_render_entry(struct kore_pgsql *sql, struct entry_query *eq) { int err = KORE_RESULT_OK; int row = 0, rows = 0; @@ -542,7 +542,7 @@ int sql_render_posts(struct kore_pgsql *sql, struct post_query *pq) { // Allocate a buffer to render the markdown as HTML into. html_buf = kore_buf_alloc(0); - // Iterate over posts and render them. + // Iterate over entries and render them. rows = kore_pgsql_ntuples(sql); for (row = 0; row < rows; row++) { // Reset buffer. @@ -558,14 +558,14 @@ int sql_render_posts(struct kore_pgsql *sql, struct post_query *pq) { // Render MD. err = render_md(body, html_buf); if (err == KORE_RESULT_ERROR) { - kore_log(LOG_ERR, "Error rendering markdown for post %s.", id); + kore_log(LOG_ERR, "Error rendering markdown for entry %s.", id); continue; } - // Append rendered MD post. + // Append rendered MD entry. kore_buf_appendf( - pq->result, - (const char *) asset_post_html, + eq->result, + (const char *) asset_entry_html, id, title, created_at, @@ -579,7 +579,7 @@ int sql_render_posts(struct kore_pgsql *sql, struct post_query *pq) { return KORE_RESULT_OK; } -int sql_insert_posts(const char *id, const char *title, const char *body) { +int sql_insert_entry(const char *id, const char *title, const char *body) { int err = KORE_RESULT_OK; struct kore_pgsql sql; @@ -594,7 +594,7 @@ int sql_insert_posts(const char *id, const char *title, const char *body) { if (id == NULL) { err = kore_pgsql_query_params( &sql, - q_insert_posts, + q_insert_entry, 0, 2, KORE_PGSQL_PARAM_TEXT(title), @@ -603,7 +603,7 @@ int sql_insert_posts(const char *id, const char *title, const char *body) { } else { err = kore_pgsql_query_params( &sql, - q_insert_posts_with_id, + q_insert_entry_with_id, 0, 3, KORE_PGSQL_PARAM_TEXT(id), @@ -623,7 +623,7 @@ out: ; return err; } -int sql_update_posts(const char *id, const char *title, const char *body) { +int sql_update_entry(const char *id, const char *title, const char *body) { int err = KORE_RESULT_OK; struct kore_pgsql sql; @@ -637,7 +637,7 @@ int sql_update_posts(const char *id, const char *title, const char *body) { err = kore_pgsql_query_params( &sql, - q_update_posts, + q_update_entry, 0, 3, KORE_PGSQL_PARAM_TEXT(title), @@ -656,7 +656,7 @@ out: ; return err; } -int sql_delete_posts(const char *id) { +int sql_delete_entry(const char *id) { int err = KORE_RESULT_OK; struct kore_pgsql sql; @@ -670,7 +670,7 @@ int sql_delete_posts(const char *id) { err = kore_pgsql_query_params( &sql, - q_delete_posts, + q_delete_entry, 0, 1, KORE_PGSQL_PARAM_TEXT(id) diff --git a/src/queries.h b/src/queries.h index ffc9c7b..986f180 100644 --- a/src/queries.h +++ b/src/queries.h @@ -1,36 +1,35 @@ -const char *q_select_post = +const char *q_select_entry = "SELECT id, title, created_at::DATE, body " -"FROM posts " +"FROM entries " "WHERE id = $1;"; -const char *q_select_posts = +const char *q_select_entries = "SELECT id, title, created_at::DATE, body " -"FROM posts " +"FROM entries " "ORDER BY updated_at DESC;"; - -const char *q_insert_posts = -"INSERT INTO posts " +const char *q_insert_entry = +"INSERT INTO entries " "(title, body) " "VALUES " "($1, $2) " "RETURNING id;"; -const char *q_insert_posts_with_id = -"INSERT INTO posts " +const char *q_insert_entry_with_id = +"INSERT INTO entries " "(id, title, body) " "VALUES " "($1, $2, $3) " "RETURNING id;"; -const char *q_update_posts = -"UPDATE posts " +const char *q_update_entry = +"UPDATE entries " "SET title = $1, body = $2, updated_at = NOW() " "WHERE id = $3 " "RETURNING id;"; -const char *q_delete_posts = +const char *q_delete_entry = "DELETE " -"FROM posts " +"FROM entries " "WHERE id = $1 " "RETURNING id;"; |