From 9fb4a9f5cd35161c0a4cafd1ac5126f57c6e7930 Mon Sep 17 00:00:00 2001 From: Meutel Date: Fri, 30 Dec 2016 00:38:31 +0100 Subject: [PATCH] Book list --- db.c | 31 +++++++++++++++++++++++++++---- extern.h | 1 + main.c | 45 +++++++++++++++++++++++++++++---------------- 3 files changed, 57 insertions(+), 20 deletions(-) diff --git a/db.c b/db.c index d3c7447..e262407 100644 --- a/db.c +++ b/db.c @@ -30,21 +30,44 @@ enum stmt { STMT_BOOK, + STMT_BOOKS, STMT__MAX }; static const char *const stmts[STMT__MAX] = { /* STMT_BOOK */ "SELECT books.id AS id,title FROM books WHERE books.id = ?", + /* STMT_BOOKS */ + "SELECT books.id AS id,title FROM books LIMIT ?", }; +int +db_books_load(struct kreq *r, struct book **books, int limit) +{ + if (limit < 0) { + return 0; + } + struct ksqlstmt *stmt; + int i = 0; + ksql_stmt_alloc(r->arg, &stmt, + stmts[STMT_BOOKS], + STMT_BOOKS); + ksql_bind_int(stmt, 0, limit); + while (KSQL_ROW == ksql_stmt_step(stmt)) { + books[i] = kcalloc(1, sizeof(struct book)); + books[i]->id = ksql_stmt_int(stmt, 0); + books[i]->title = kstrdup(ksql_stmt_str(stmt, 1)); + i++; + } + ksql_stmt_free(stmt); + return i; +} + struct book * db_book_load(struct kreq *r, int64_t id) { struct ksqlstmt *stmt; struct book *book; - int i = 0; - ksql_stmt_alloc(r->arg, &stmt, stmts[STMT_BOOK], STMT_BOOK); @@ -54,8 +77,8 @@ db_book_load(struct kreq *r, int64_t id) return(NULL); } book = kcalloc(1, sizeof(struct book)); - book->id = ksql_stmt_int(stmt, i++); - book->title = kstrdup(ksql_stmt_str(stmt, i++)); + book->id = ksql_stmt_int(stmt, 0); + book->title = kstrdup(ksql_stmt_str(stmt, 1)); ksql_stmt_free(stmt); return book; } diff --git a/extern.h b/extern.h index 918f809..466c199 100644 --- a/extern.h +++ b/extern.h @@ -28,6 +28,7 @@ struct book { __BEGIN_DECLS struct book * db_book_load(struct kreq *r, int64_t id); +int db_books_load(struct kreq *r, struct book **books, int limit); void db_close(struct kreq *); int db_open(struct kreq *, const char *); diff --git a/main.c b/main.c index 1f653d7..6654a3f 100644 --- a/main.c +++ b/main.c @@ -86,6 +86,23 @@ http_open(struct kreq *r, enum khttp code) khttp_body(r); } +void +puterror(struct kjsonreq *req, char *message) +{ + kjson_obj_open(req); + kjson_putstringp(req, "msg", message); + kjson_obj_close(req); +} + +void +putbook(struct kjsonreq *req, struct book *b) +{ + kjson_obj_open(req); + kjson_putintp(req, "id", b->id); + kjson_putstringp(req, "title", b->title); + kjson_obj_close(req); +} + static void sendbooks(struct kreq *r) { @@ -93,6 +110,7 @@ sendbooks(struct kreq *r) const char *errid; int64_t id = -1; struct book *b = NULL; + int res, i = 0; if (NULL != r->fieldmap[KEY_ID]) id = r->fieldmap[KEY_ID]->parsed.i; @@ -103,24 +121,19 @@ sendbooks(struct kreq *r) } http_open(r, (id > 0 && NULL == b) ? KHTTP_404 : KHTTP_200); kjson_open(&req, r); - kjson_obj_open(&req); - if (NULL == b) { - kjson_objp_open(&req, "error"); - kjson_putstringp(&req, "msg", "Unknown book"); - kjson_obj_close(&req); + if (id >0 && NULL == b) { + puterror(&req, "Unknown book"); + } else if (NULL != b) { + putbook(&req, b); } else { - kjson_objp_open(&req, "book"); - kjson_putstringp(&req, "data", "books"); - kjson_putintp(&req, "id", b->id); - kjson_putstringp(&req, "title", b->title); - kjson_obj_close(&req); + kjson_array_open(&req); + struct book **books = kcalloc(10, sizeof(struct book)); + res = db_books_load(r, books, 10); + while (i < res) { + putbook(&req, books[i++]); + } + kjson_array_close(&req); } -#ifdef __DEBUG__ - kjson_putstringp(&req, "_path", r->path); - kjson_putstringp(&req, "_pname", r->pname); - kjson_putstringp(&req, "_pagename", r->pagename); -#endif - kjson_obj_close(&req); kjson_close(&req); }