Tags in json

This commit is contained in:
Meutel 2017-01-03 19:48:22 +01:00
parent 24b6ca74ee
commit f23cab4b01
2 changed files with 17 additions and 3 deletions

6
db.c
View File

@ -246,7 +246,7 @@ db_assign_book_tags(struct booktag *list)
p = list; p = list;
while (NULL != p) { while (NULL != p) {
if (NULL != p->b && NULL == p->b->tags && p->b->tsize > 0) { if (NULL != p->b && NULL == p->b->tags && p->b->tsize > 0) {
p->b->tags = kcalloc(p->b->tsize, sizeof(char)); p->b->tags = kcalloc(p->b->tsize, sizeof(char*));
p->b->tsize = 0; // use as counter for insert p->b->tsize = 0; // use as counter for insert
} }
p->b->tags[p->b->tsize] = p->tag; p->b->tags[p->b->tsize] = p->tag;
@ -362,6 +362,7 @@ db_books_load(struct kreq *r, BookAdv **books, int limit, int offset)
books[count] = kcalloc(1, sizeof(BookAdv)); books[count] = kcalloc(1, sizeof(BookAdv));
db_book_fill(&books[count]->b,stmt); db_book_fill(&books[count]->b,stmt);
books[count]->asize = 0; books[count]->asize = 0;
books[count]->tsize = 0;
count++; count++;
} }
ksql_stmt_free(stmt); ksql_stmt_free(stmt);
@ -372,8 +373,6 @@ db_books_load(struct kreq *r, BookAdv **books, int limit, int offset)
p_t = db_load_books_tags(r, books, count, limit, offset); p_t = db_load_books_tags(r, books, count, limit, offset);
db_assign_book_tags(p_t); db_assign_book_tags(p_t);
ksql_stmt_free(stmt);
return count; return count;
} }
@ -393,6 +392,7 @@ db_book_full_tags(BookAdv *b, struct kreq *r, int64_t id)
item->tag = kstrdup(ksql_stmt_str(stmt, 0)); item->tag = kstrdup(ksql_stmt_str(stmt, 0));
item->b = b; item->b = b;
item->b->tsize++; item->b->tsize++;
item->p = p; item->p = p;
p = item; p = item;
} }

14
main.c
View File

@ -143,6 +143,18 @@ putbookdata(struct kjsonreq *req, BookFull *b)
} }
} }
static void
puttags(struct kjsonreq *req, BookAdv *b)
{
if (b->tsize > 0) {
kjson_arrayp_open(req, "tags");
for (size_t i=0; i<b->tsize;i++) {
kjson_putstring(req, b->tags[i]);
}
kjson_array_close(req);
}
}
static void static void
putauthors(struct kjsonreq *req, BookAdv *b) putauthors(struct kjsonreq *req, BookAdv *b)
{ {
@ -202,6 +214,7 @@ sendbooks(struct kreq *r)
} else if (NULL != b) { } else if (NULL != b) {
putbookfull(&req, b); putbookfull(&req, b);
putauthors(&req, &b->ba); putauthors(&req, &b->ba);
puttags(&req, &b->ba);
putbookdata(&req, b); putbookdata(&req, b);
kjson_obj_close(&req); kjson_obj_close(&req);
db_book_full_free(b); db_book_full_free(b);
@ -213,6 +226,7 @@ sendbooks(struct kreq *r)
Book b = books[i]->b; Book b = books[i]->b;
putbook(&req, &b); putbook(&req, &b);
putauthors(&req, books[i]); putauthors(&req, books[i]);
puttags(&req, books[i]);
kjson_obj_close(&req); kjson_obj_close(&req);
db_book_adv_free(books[i]); db_book_adv_free(books[i]);
i++; i++;