77use BookStack \Entities \Models \Chapter ;
88use BookStack \Entities \Models \Entity ;
99use BookStack \Entities \Queries \BookQueries ;
10+ use BookStack \Entities \Queries \BookshelfQueries ;
1011use BookStack \Entities \Queries \PageQueries ;
1112use BookStack \Entities \Repos \BookRepo ;
1213use BookStack \Entities \Tools \BookContents ;
1314use BookStack \Http \ApiController ;
1415use BookStack \Permissions \Permission ;
16+ use Illuminate \Database \Eloquent \Builder ;
17+ use Illuminate \Database \Eloquent \Relations \BelongsToMany ;
1518use Illuminate \Http \Request ;
1619use Illuminate \Validation \ValidationException ;
1720
@@ -21,6 +24,7 @@ public function __construct(
2124 protected BookRepo $ bookRepo ,
2225 protected BookQueries $ queries ,
2326 protected PageQueries $ pageQueries ,
27+ protected BookshelfQueries $ shelfQueries ,
2428 ) {
2529 }
2630
@@ -60,13 +64,20 @@ public function create(Request $request)
6064 * View the details of a single book.
6165 * The response data will contain a 'content' property listing the chapter and pages directly within, in
6266 * the same structure as you'd see within the BookStack interface when viewing a book. Top-level
63- * contents will have a 'type' property to distinguish between pages & chapters.
67+ * contents will have a 'type' property to distinguish between pages and chapters.
6468 */
6569 public function read (string $ id )
6670 {
6771 $ book = $ this ->queries ->findVisibleByIdOrFail (intval ($ id ));
6872 $ book = $ this ->forJsonDisplay ($ book );
69- $ book ->load (['createdBy ' , 'updatedBy ' , 'ownedBy ' ]);
73+ $ book ->load ([
74+ 'createdBy ' ,
75+ 'updatedBy ' ,
76+ 'ownedBy ' ,
77+ 'shelves ' => function (BelongsToMany $ query ) {
78+ $ query ->select (['id ' , 'name ' , 'slug ' ])->scopes ('visible ' );
79+ }
80+ ]);
7081
7182 $ contents = (new BookContents ($ book ))->getTree (true , false )->all ();
7283 $ contentsApiData = (new ApiEntityListFormatter ($ contents ))
0 commit comments