diff scid/page-api-todo.c @ 27:dae2de19ca5d

misc: switch to JSON everywhere
author David Demelier <markand@malikania.fr>
date Wed, 03 Aug 2022 15:18:09 +0200
parents 7e10cace67a3
children 695637f1d8a7
line wrap: on
line diff
--- a/scid/page-api-todo.c	Tue Aug 02 13:24:13 2022 +0200
+++ b/scid/page-api-todo.c	Wed Aug 03 15:18:09 2022 +0200
@@ -16,63 +16,13 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#include <sys/types.h>
 #include <assert.h>
-#include <stdarg.h>
-#include <stdint.h>
-#include <string.h>
-
-#include <kcgi.h>
-#include <jansson.h>
-
-#include "config.h"
-#include "db.h"
-#include "log.h"
-#include "page-api-todo.h"
-#include "page.h"
-#include "types.h"
-#include "util.h"
-
-static void
-list(struct kreq *r, const struct job *jobs, size_t jobsz)
-{
-	json_t *doc;
-	char *dump;
-
-	doc = job_to(jobs, jobsz);
-	dump = json_dumps(doc, JSON_COMPACT);
+#include <stdio.h>
 
-	khttp_puts(r, dump);
-	free(dump);
-	json_decref(doc);
-}
-
-#if 0
-
-static int
-save(const char *json)
-{
-	struct jobresult res = {0};
-	int ret = -1;
-
-	json_t *doc;
-	json_error_t err;
-
-	if (!(doc = json_loads(json, 0, &err)))
-		log_warn("api/post: invalid JSON input: %s", err.text);
-	else if (jobresult_from(&res, 1, doc) < 0)
-		log_warn("api/post: failed to decode parameters");
-	else if (db_jobresult_add(&res) < 0)
-		log_warn("api/post: database save error");
-	else
-		ret = 0;
-
-	json_decref(doc);
-
-	return ret;
-}
-
-#endif
+#include "crud.h"
+#include "db.h"
+#include "page.h"
+#include "util.h"
 
 /*
  * GET /api/v1/todo/<worker-name>
@@ -83,21 +33,7 @@
 static void
 get(struct kreq *r)
 {
-	struct job jobs[SCI_JOB_LIST_MAX];
-	ssize_t jobsz;
-
-	if ((jobsz = db_job_todo(jobs, UTIL_SIZE(jobs), util_basename(r->path))) < 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);
-		list(r, jobs, jobsz);
-		khttp_free(r);
-	}
-
-	for (ssize_t i = 0; i < jobsz; ++i)
-		job_finish(&jobs[i]);
+	crud_list(r, db_job_todo(util_basename(r->path)));
 }
 
 void