aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/clog.c81
-rw-r--r--src/queries.h22
2 files changed, 91 insertions, 12 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.
diff --git a/src/queries.h b/src/queries.h
index cae7d59..108b249 100644
--- a/src/queries.h
+++ b/src/queries.h
@@ -1,23 +1,37 @@
-const char *query_html_post = \
+const char *q_select_html_post = \
"SELECT id, title, created_at::DATE, body "
"FROM posts "
"WHERE id = $1;";
-const char *query_html_posts = \
+const char *q_select_html_posts = \
"SELECT id, title, created_at::DATE, body "
"FROM posts "
"ORDER BY updated_at DESC;";
-const char *query_json_post = \
+const char *q_select_json_post = \
"SELECT JSON_AGG(ROW_TO_JSON(row)) FROM ("
"SELECT id, title, body, created_at, updated_at "
"FROM posts "
"WHERE id = $1"
") row;";
-const char *query_json_posts = \
+const char *q_select_json_posts = \
"SELECT JSON_AGG(ROW_TO_JSON(row)) FROM ("
"SELECT id, title, body, created_at, updated_at "
"FROM posts "
"ORDER BY updated_at DESC"
") row;";
+
+const char *q_insert_posts = \
+"INSERT INTO posts "
+"(id, title, body) "
+"VALUES "
+"($1, $2, $3) "
+"RETURNING id;";
+
+const char *q_delete_posts = \
+"DELETE "
+"FROM posts "
+"WHERE id = $1 "
+"RETURNING id;";
+