diff scid/crud.c @ 29:695637f1d8a7

scid: first index page in javascript
author David Demelier <markand@malikania.fr>
date Thu, 04 Aug 2022 14:13:58 +0200
parents dae2de19ca5d
children 081e1c258e64
line wrap: on
line diff
--- a/scid/crud.c	Thu Aug 04 06:09:54 2022 +0200
+++ b/scid/crud.c	Thu Aug 04 14:13:58 2022 +0200
@@ -16,8 +16,11 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+#include <assert.h>
+
+#include "crud.h"
 #include "log.h"
-#include "page.h"
+#include "pageutil.h"
 
 static int
 save(struct kreq *r, int (*saver)(json_t *), const char *topic)
@@ -43,37 +46,26 @@
 void
 crud_insert(struct kreq *r, int (*saver)(json_t *), const char *topic)
 {
+	assert(r);
+	assert(saver);
+	assert(topic);
+
 	if (r->fieldsz < 1)
-		page(r, KHTTP_400, KMIME_APP_JSON, NULL, NULL);
+		pageutil_json(r, KHTTP_400, NULL);
 	else if (save(r, saver, topic) < 0)
-		page(r, KHTTP_500, KMIME_APP_JSON, NULL, NULL);
-	else {
-		khttp_head(r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_APP_JSON]);
-		khttp_head(r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]);
-		khttp_body(r);
-		khttp_free(r);
-	}
+		pageutil_json(r, KHTTP_500, NULL);
+	else
+		/* TODO: Maybe we should send the updated model. */
+		pageutil_json(r, KHTTP_200, NULL);
 }
 
 void
 crud_list(struct kreq *r, json_t *doc)
 {
-	char *str;
+	assert(r);
 
 	if (!doc)
-		page(r, KHTTP_500, KMIME_APP_JSON, NULL, NULL);
-	else {
-		if (!(str = json_dumps(doc, JSON_COMPACT)))
-			page(r, KHTTP_500, KMIME_APP_JSON, NULL, NULL);
-		else {
-			khttp_head(r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_APP_JSON]);
-			khttp_head(r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]);
-			khttp_body(r);
-			khttp_printf(r, "%s", str);
-			khttp_free(r);
-			json_decref(doc);
-		}
-
-		free(str);
-	}
+		pageutil_json(r, KHTTP_500, NULL);
+	else
+		pageutil_json(r, KHTTP_200, doc);
 }