JSON list authors

This commit is contained in:
Meutel 2017-01-07 12:19:00 +01:00
parent eee0323f6c
commit 0faf364cd8
3 changed files with 53 additions and 3 deletions

34
db.c
View File

@ -37,6 +37,7 @@ enum stmt {
STMT_BOOKS_COUNT, STMT_BOOKS_COUNT,
STMT_BOOKS_TAGS, STMT_BOOKS_TAGS,
STMT_BOOKS_AUTHORS, STMT_BOOKS_AUTHORS,
STMT_AUTHORS,
STMT__MAX STMT__MAX
}; };
@ -76,6 +77,8 @@ static const char *const stmts[STMT__MAX] = {
"SELECT authors.id, authors.name, books_authors_link.book as book \ "SELECT authors.id, authors.name, books_authors_link.book as book \
FROM authors, books_authors_link WHERE books_authors_link.author = authors.id \ FROM authors, books_authors_link WHERE books_authors_link.author = authors.id \
AND books_authors_link.book IN ( SELECT id FROM books ORDER BY id LIMIT ? OFFSET ?)", AND books_authors_link.book IN ( SELECT id FROM books ORDER BY id LIMIT ? OFFSET ?)",
/* STMT_AUTHORS */
"SELECT id, name FROM authors ORDER BY id LIMIT ? OFFSET ?", // TODO count books
}; };
/* /*
@ -144,6 +147,12 @@ db_author_unfill(Author *p) {
free(p->name); free(p->name);
} }
void
db_author_free(Author *p) {
db_author_unfill(p);
free(p);
}
static void static void
db_book_data_unfill(BookData *p) { db_book_data_unfill(BookData *p) {
if (NULL == p) if (NULL == p)
@ -500,6 +509,31 @@ db_book_load(struct kreq *r, int64_t id)
return book; return book;
} }
int
db_authors_load(struct kreq *r, Author **authors, int limit, int offset)
{
if (limit < 0 || offset < 0) {
return 0;
}
struct ksqlstmt *stmt;
int count = 0;
ksql_stmt_alloc(r->arg, &stmt,
stmts[STMT_AUTHORS],
STMT_AUTHORS);
ksql_bind_int(stmt, 0, limit);
ksql_bind_int(stmt, 1, offset);
while (KSQL_ROW == ksql_stmt_step(stmt)) {
authors[count] = kcalloc(1, sizeof(Author));
authors[count]->id = ksql_stmt_int(stmt, 0);
authors[count]->name =kstrdup(ksql_stmt_str(stmt, 1));
count++;
}
ksql_stmt_free(stmt);
return count;
}
/* /*
* Open the database and stash the resulting handle in the d * Open the database and stash the resulting handle in the d
*/ */

View File

@ -93,6 +93,9 @@ void db_book_free(Book *p);
void db_book_adv_free(BookAdv *p); void db_book_adv_free(BookAdv *p);
void db_book_full_free(BookFull *p); void db_book_full_free(BookFull *p);
int db_authors_load(struct kreq *r, Author **authors, int limit, int offset);
void db_author_free(Author *p);
void db_series_free(Series *p); void db_series_free(Series *p);
void db_close(struct kreq *); void db_close(struct kreq *);

15
main.c
View File

@ -241,12 +241,25 @@ static void
sendauthors(struct kreq *r) sendauthors(struct kreq *r)
{ {
struct kjsonreq req; struct kjsonreq req;
int res, i = 0, page = 1, per = 20;
http_open(r, KHTTP_200); http_open(r, KHTTP_200);
kjson_open(&req, r); kjson_open(&req, r);
kjson_array_open(&req);
Author **authors = kcalloc(per, sizeof(Author));
res = db_authors_load(r, authors, per, per * (page-1));
while (i < res) {
kjson_obj_open(&req); kjson_obj_open(&req);
kjson_putstringp(&req, "data", "authors"); kjson_putintp(&req, "id", authors[i]->id);
kjson_putstringp(&req, "name", authors[i]->name);
kjson_obj_close(&req); kjson_obj_close(&req);
db_author_free(authors[i]);
i++;
}
free(authors);
kjson_array_close(&req);
kjson_close(&req); kjson_close(&req);
} }