aboutsummaryrefslogtreecommitdiff
path: root/src/clog.c
diff options
context:
space:
mode:
authorMichael McVady <femtonaut@gmail.com>2023-01-15 14:01:59 -0600
committerMichael McVady <femtonaut@gmail.com>2023-01-15 14:01:59 -0600
commit4bfab86f636f2392f92037058c783c2634ac2060 (patch)
tree5816a03c9fc7f9a3b993d69a1ecd83b5ec3655e1 /src/clog.c
parent3ff9bdbcb2cae776579dcfe781276172f05f000c (diff)
Hack in DELETE method without testing
Diffstat (limited to 'src/clog.c')
-rw-r--r--src/clog.c81
1 files changed, 73 insertions, 8 deletions
diff --git a/src/clog.c b/src/clog.c
index bdaac38..9f1991a 100644
--- a/src/clog.c
+++ b/src/clog.c
@@ -40,9 +40,10 @@ static const char *accept_json = "application/json";
static const char *database = "db";
static const char * const error_msg[] = {
- [HTTP_STATUS_BAD_REQUEST] = "There was an error processing the request data.",
- [HTTP_STATUS_NOT_FOUND] = "Resource not found.",
- [HTTP_STATUS_INTERNAL_ERROR] = "There was an error processing the request.",
+ [201] = "OK DOKEY",
+ [HTTP_STATUS_BAD_REQUEST] = "There was an error processing the request data.", // 403?
+ [HTTP_STATUS_NOT_FOUND] = "Resource not found.", // 404
+ [HTTP_STATUS_INTERNAL_ERROR] = "There was an error processing the request.", // 500
};
void post_query_init(struct post_query *pq, enum content_type type, const char *id);
@@ -70,8 +71,10 @@ 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 delete_posts_resource(struct http_request *req);
int sql_select_posts(struct post_query *pq);
+int sql_delete_posts(const char *id);
int sql_insert_posts(
const char *id,
const char *title,
@@ -399,7 +402,7 @@ sql_select_posts(struct post_query *pq) {
// Query a post.
err = kore_pgsql_query_params(
&sql,
- pq->type == HTML ? query_html_post : query_json_post,
+ pq->type == HTML ? q_select_html_post : q_select_json_post,
0, // return string data
1, // param count
KORE_PGSQL_PARAM_TEXT(pq->id)
@@ -407,8 +410,8 @@ sql_select_posts(struct post_query *pq) {
} else {
// Query all posts.
err = kore_pgsql_query(
- &sql,
- pq->type == HTML ? query_html_posts : query_json_posts
+ &sql,
+ pq->type == HTML ? q_select_html_posts : q_select_json_posts
);
}
@@ -439,6 +442,37 @@ out: ;
return err;
}
+int
+delete_posts_resource(struct http_request *req) {
+ int err = 0;
+
+ enum content_type type = get_content_type(req);
+ const char *id = req->path + strlen("/posts/");
+
+ // Check for valid resource UUID
+ kore_log(LOG_DEBUG, "Resource id /posts/%s.", id);
+ err = validate_uuid(id);
+ if (err == KORE_RESULT_ERROR) {
+ kore_log(LOG_ERR, "Invalid post id %s.", id);
+ http_err_resp(req, type, HTTP_STATUS_NOT_FOUND);
+ goto out;
+ }
+
+ err = sql_delete_posts(id);
+ // FIXME: should 404 if id doesn't exist.
+ // if (pq.status == QUERY_STATUS_NOT_FOUND)
+ // http_err_resp(req, pq.type, HTTP_STATUS_NOT_FOUND);
+ if (err == KORE_RESULT_ERROR)
+ http_err_resp(req, type, HTTP_STATUS_INTERNAL_ERROR);
+ else
+ // TODO: test this, it should exlode.
+ http_err_resp(req, type, HTTP_STATUS_OK);
+
+out: ;
+
+ return KORE_RESULT_OK;
+}
+
int sql_render_posts(struct kore_pgsql *sql, struct post_query *pq) {
int err = KORE_RESULT_OK;
@@ -520,8 +554,7 @@ sql_insert_posts(
err = kore_pgsql_query_params(
&sql,
- "INSERT INTO posts (id, title, body) "
- "VALUES ($1, $2, $3) RETURNING id;",
+ q_insert_posts,
0,
3,
KORE_PGSQL_PARAM_TEXT(id),
@@ -540,6 +573,38 @@ out: ;
return err;
}
+int
+sql_delete_posts(const char *id) {
+ int err = KORE_RESULT_OK;
+
+ 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_query_params(
+ &sql,
+ q_delete_posts,
+ 0,
+ 1,
+ KORE_PGSQL_PARAM_TEXT(id)
+ );
+ if (err == KORE_RESULT_ERROR) {
+ kore_pgsql_logerror(&sql);
+ goto out;
+ }
+
+out: ;
+
+ kore_pgsql_cleanup(&sql);
+
+ return err;
+}
+
static int
render_md(const char *in, struct kore_buf *out) {
// Not a kore err.