comparison scid/page-api-jobresults.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
comparison
equal deleted inserted replaced
26:7e10cace67a3 27:dae2de19ca5d
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */ 17 */
18 18
19 #include <sys/types.h>
20 #include <assert.h> 19 #include <assert.h>
21 #include <stdarg.h>
22 #include <stdint.h>
23 #include <kcgi.h>
24 20
21 #include "crud.h"
25 #include "db.h" 22 #include "db.h"
26 #include "log.h"
27 #include "page.h" 23 #include "page.h"
28 #include "types.h"
29
30 static int
31 save(const char *json)
32 {
33 struct jobresult res = {0};
34 int ret = -1;
35
36 json_t *doc;
37 json_error_t err;
38
39 if (!(doc = json_loads(json, 0, &err)))
40 log_warn("api/post: invalid JSON input: %s", err.text);
41 else if (jobresult_from(&res, 1, doc) < 0)
42 log_warn("api/post: failed to decode parameters");
43 else if (db_jobresult_add(&res) < 0)
44 log_warn("api/post: database save error");
45 else
46 ret = 0;
47
48 json_decref(doc);
49 jobresult_finish(&res);
50
51 return ret;
52 }
53 24
54 static void 25 static void
55 post(struct kreq *r) 26 post(struct kreq *r)
56 { 27 {
57 if (r->fieldsz < 1) 28 crud_insert(r, db_jobresult_add, "page-api-jobresults");
58 page(r, KHTTP_400, KMIME_APP_JSON, NULL, NULL);
59 else if (save(r->fields[0].val) < 0)
60 page(r, KHTTP_500, KMIME_APP_JSON, NULL, NULL);
61 else {
62 khttp_head(r, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_APP_JSON]);
63 khttp_head(r, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200]);
64 khttp_body(r);
65 khttp_free(r);
66 }
67 } 29 }
68 30
69 void 31 void
70 page_api_v1_jobresults(struct kreq *r) 32 page_api_v1_jobresults(struct kreq *r)
71 { 33 {