Display book data

This commit is contained in:
Meutel 2016-12-29 23:25:42 +01:00
parent 9d876bdb7c
commit 8606c034e4

19
main.c
View File

@ -92,20 +92,25 @@ sendbooks(struct kreq *r)
struct kjsonreq req;
const char *errid;
int64_t id = -1;
struct book *b = NULL;
http_open(r, KHTTP_200);
kjson_open(&req, r);
kjson_obj_open(&req);
kjson_putstringp(&req, "data", "books");
if (NULL != r->fieldmap[KEY_ID])
id = r->fieldmap[KEY_ID]->parsed.i;
if (r->path[0] != '\0')
id = strtonum(r->path, INT64_MIN, INT64_MAX, &errid);
if (id > 0) {
kjson_putintp(&req, "id", id);
db_book_load(r, id);
b = db_book_load(r, id);
}
http_open(r, (id > 0 && NULL == b) ? KHTTP_404 : KHTTP_200);
kjson_open(&req, r);
kjson_obj_open(&req);
if (NULL == b) {
kjson_putstringp(&req, "data", "error");
} else {
kjson_putstringp(&req, "data", "books");
kjson_putintp(&req, "id", b->id);
kjson_putstringp(&req, "title", b->title);
}
kjson_putstringp(&req, "_path", r->path);
kjson_putstringp(&req, "_pname", r->pname);
kjson_putstringp(&req, "_pagename", r->pagename);