Mercurial > sci
diff scid/page-api-projects.c @ 18:600204c31bf0
misc: refactor
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 12 Jul 2022 20:20:51 +0200 |
parents | page-api-projects.c@3051ef92173a |
children | f98ea578b1ef |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/scid/page-api-projects.c Tue Jul 12 20:20:51 2022 +0200 @@ -0,0 +1,182 @@ +/* + * page-api-projects.c -- /api/v?/projects route + * + * Copyright (c) 2021 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <assert.h> + +#include "config.h" +#include "db.h" +#include "log.h" +#include "page-api-projects.h" +#include "page.h" +#include "types.h" +#include "util.h" + +static void +list(struct kreq *r, const struct project *projects, size_t projectsz) +{ + struct json_t *doc; + char *dump; + + doc = project_to(projects, projectsz); + dump = json_dumps(doc, JSON_COMPACT); + + khttp_puts(r, dump); + free(dump); + json_decref(doc); +} + +static int +save(const char *json) +{ + struct project 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 (project_from(&res, 1, doc) < 0) + log_warn("api/post: failed to decode parameters"); + else if (db_project_add(&res) < 0) + log_warn("api/post: database save error"); + else + ret = 0; + + json_decref(doc); + + return ret; +} + +static void +push(struct kreq *r, const struct project *p) +{ + struct json_t *json; + char *dump; + + json = project_to(p, 1); + dump = json_dumps(json, JSON_COMPACT); + + 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_puts(r, dump); + khttp_free(r); + + free(dump); + json_decref(json); +} + +static void +get_one(struct kreq *r, const char *name) +{ + struct db_ctx ctx; + struct project project = { + .name = name + }; + + if (db_project_find(&ctx, &project) < 0) + page(r, NULL, KHTTP_500, KMIME_APP_JSON, NULL); + else { + push(r, &project); + db_ctx_finish(&ctx); + } +} + +static void +get_one_id(struct kreq *r, int id) +{ + struct db_ctx ctx; + struct project project = { + .id = id + }; + + if (db_project_find_id(&ctx, &project) < 0) + page(r, NULL, KHTTP_500, KMIME_APP_JSON, NULL); + else { + push(r, &project); + db_ctx_finish(&ctx); + } +} + +static void +get_all(struct kreq *r) +{ + struct db_ctx ctx; + struct project projects[SCI_PROJECT_MAX]; + ssize_t projectsz; + + if ((projectsz = db_project_list(&ctx, projects, UTIL_SIZE(projects))) < 0) + page(r, NULL, KHTTP_500, KMIME_APP_JSON, 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, projects, projectsz); + db_ctx_finish(&ctx); + khttp_free(r); + } +} + +static void +get(struct kreq *r) +{ + char name[128]; + int id; + + if (sscanf(r->path, "v1/projects/%d", &id) == 1) + get_one_id(r, id); + else if (sscanf(r->path, "v1/projects/%127s", name) == 1) + get_one(r, name); + else + get_all(r); +} + +static void +post(struct kreq *r) +{ + if (r->fieldsz < 1) + page(r, NULL, KHTTP_400, KMIME_APP_JSON, NULL); + else if (save(r->fields[0].val) < 0) + page(r, NULL, KHTTP_500, KMIME_APP_JSON, 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); + } +} + +void +page_api_v1_projects(struct kreq *r) +{ + assert(r); + + switch (r->method) { + case KMETHOD_GET: + get(r); + break; + case KMETHOD_POST: + post(r); + break; + default: + page(r, NULL, KHTTP_400, KMIME_APP_JSON, NULL); + break; + } + +}