BookStack icon indicating copy to clipboard operation
BookStack copied to clipboard

add list of chapters to JSON data in books/read API endpoint

Open pmenzel opened this issue 3 years ago • 2 comments

API Endpoint or Feature

For the sake of completeness, the JSON data returned by the books/read API endpoint /api/books/{id} (docs) should contain a list of chapters in the particular book, similar as shelves/read contains a list of books and chapters/read contains a list of pages.

Use-Case

When getting the info about a particular book via the /api/books/{id} API endpoint, it would be nice to have the list of chapters included in the JSON data.

Additional context

No response

pmenzel avatar Sep 19 '22 20:09 pmenzel

Thanks for the request @pmenzel, I agree we should include this.

Since books also contain pages, and have a pre-defined structure, I'd say we should add a children or contents attribute which contains the entire tree structure, as per what you'd see when viewing a book in BookStack. So this would be an array mixed with chapter and page details, for top-level book items, with chapters also providing some light details for their child pages.

This would save client-side work of having to rebuild the tree structure from other details. If just the chapters are required, then the list can be client-side filtered via a type property like what is supplied on the search endpoint data.

Should be a relatively simple addition so I'll assign to the next feature release.

ssddanbrown avatar Sep 19 '22 21:09 ssddanbrown

Great, thanks! I was writing a Python script to download pages into a folder structure that represents shelves, books, and chapters and stumbled upon this issue.

A workaround would be to use /api/chapters?filter[book_id]=X to get the chapters of each book.
For pages without chapters, it is less straightforward at the moment. I think, one could do /api/pages?filter[book_id]={id} and then filter the JSON data to only keep pages with chapter_id=0, or one could do /api/pages?filter[chapter_id]=0 and then only keep pages with the desired book_id.

So yes, it would be nice to have a contents-field in the JSON from /api/books/{id} that contains the list of chapters and pages without chapters.

I could make a PR with the script to https://github.com/BookStackApp/api-scripts when it's done.

pmenzel avatar Sep 20 '22 10:09 pmenzel

Functionality, as mentioned above, now added as part of 0e94fd44a8925c695441768eee0ecbce7bb239bc and will be part of the next feature release so I'll therefore close this off.

For pages without chapters, it is less straightforward at the moment. I think, one could do /api/pages?filter[book_id]={id} and then filter the JSON data to only keep pages with chapter_id=0, or one could do /api/pages?filter[chapter_id]=0 and then only keep pages with the desired book_id.

On that point, multiples filters should be combined via AND so you could do something like:

/api/pages?filter[book_id]=1&filter[chapter_id]=0

ssddanbrown avatar Sep 29 '22 14:09 ssddanbrown