aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael McVady <femtonaut@gmail.com>2023-02-22 22:23:27 -0600
committerMichael McVady <femtonaut@gmail.com>2023-02-22 22:23:27 -0600
commit174907730bd2175bddc896cb6484918e2e5064f5 (patch)
tree9e78d8a29eb26d2f4c082841b150215ecdd977fe
parentf90a5869678757b7202334988eeabd96017da533 (diff)
Make post UUID optional
-rw-r--r--README.md3
-rw-r--r--assets/error.html1
-rw-r--r--assets/post.html1
-rw-r--r--src/clog.c48
-rw-r--r--src/queries.h7
5 files changed, 35 insertions, 25 deletions
diff --git a/README.md b/README.md
index 6b2533c..3965f47 100644
--- a/README.md
+++ b/README.md
@@ -2,7 +2,7 @@
An attempt to reimplement flog, using the [kore.io](https://kore.io) framework.
-## Building
+## Running
```
make fetch-dependencies
@@ -40,4 +40,3 @@ seccomp_tracing no
* Have `tests.py` load and drop database.
* only do HTTP stuff at the edge.
-
diff --git a/assets/error.html b/assets/error.html
index 789988a..78005bf 100644
--- a/assets/error.html
+++ b/assets/error.html
@@ -2,4 +2,3 @@
<div class="head">
<h1>%s</h1>
</div>
-
diff --git a/assets/post.html b/assets/post.html
index 3749339..bb94b36 100644
--- a/assets/post.html
+++ b/assets/post.html
@@ -10,4 +10,3 @@
</div>
<hr class="hdiv">
</div>
-
diff --git a/src/clog.c b/src/clog.c
index 6364c49..2e2be5a 100644
--- a/src/clog.c
+++ b/src/clog.c
@@ -301,18 +301,13 @@ int post_posts(struct http_request *req) {
if (item != NULL) {
id = item->data.string;
kore_log(LOG_INFO, "id = '%s'\n", id);
- } else {
- status = HTTP_STATUS_BAD_REQUEST;
- kore_log(LOG_ERR, "error parsing id: %s\n", kore_json_strerror());
- goto out;
- }
-
- // Check for valid resource ID/UUID
- err = validate_uuid(id);
- if (err == KORE_RESULT_ERROR) {
- status = HTTP_STATUS_BAD_REQUEST;
- kore_log(LOG_ERR, "Invalid post UUID %s.", id);
- goto out;
+ // Check for valid resource ID/UUID
+ err = validate_uuid(id);
+ if (err == KORE_RESULT_ERROR) {
+ status = HTTP_STATUS_BAD_REQUEST;
+ kore_log(LOG_ERR, "Invalid post UUID %s.", id);
+ goto out;
+ }
}
item = kore_json_find_string(json.root, "title");
@@ -518,15 +513,26 @@ int sql_insert_posts(const char *id, const char *title, const char *body) {
goto out;
}
- err = kore_pgsql_query_params(
- &sql,
- q_insert_posts,
- 0,
- 3,
- KORE_PGSQL_PARAM_TEXT(id),
- KORE_PGSQL_PARAM_TEXT(title),
- KORE_PGSQL_PARAM_TEXT(body)
- );
+ if (id == NULL) {
+ err = kore_pgsql_query_params(
+ &sql,
+ q_insert_posts,
+ 0,
+ 2,
+ KORE_PGSQL_PARAM_TEXT(title),
+ KORE_PGSQL_PARAM_TEXT(body)
+ );
+ } else {
+ err = kore_pgsql_query_params(
+ &sql,
+ q_insert_posts_with_id,
+ 0,
+ 3,
+ KORE_PGSQL_PARAM_TEXT(id),
+ KORE_PGSQL_PARAM_TEXT(title),
+ KORE_PGSQL_PARAM_TEXT(body)
+ );
+ }
if (err == KORE_RESULT_ERROR) {
kore_pgsql_logerror(&sql);
goto out;
diff --git a/src/queries.h b/src/queries.h
index 108b249..809d259 100644
--- a/src/queries.h
+++ b/src/queries.h
@@ -24,6 +24,13 @@ const char *q_select_json_posts = \
const char *q_insert_posts = \
"INSERT INTO posts "
+"(title, body) "
+"VALUES "
+"($1, $2) "
+"RETURNING id;";
+
+const char *q_insert_posts_with_id = \
+"INSERT INTO posts "
"(id, title, body) "
"VALUES "
"($1, $2, $3) "