Book list

This commit is contained in:
Meutel 2016-12-30 00:38:31 +01:00
parent 9d74bab6ae
commit 9fb4a9f5cd
3 changed files with 57 additions and 20 deletions

31
db.c
View File

@ -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;
}

View File

@ -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 *);

45
main.c
View File

@ -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);
}