Skip to content

Commit 9a12e3a

Browse files
committed
Book API: Added shelves list to show endpoint
For #6006 Added test to cover.
1 parent 6808292 commit 9a12e3a

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

app/Entities/Controllers/BookApiController.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
use BookStack\Entities\Models\Chapter;
88
use BookStack\Entities\Models\Entity;
99
use BookStack\Entities\Queries\BookQueries;
10+
use BookStack\Entities\Queries\BookshelfQueries;
1011
use BookStack\Entities\Queries\PageQueries;
1112
use BookStack\Entities\Repos\BookRepo;
1213
use BookStack\Entities\Tools\BookContents;
1314
use BookStack\Http\ApiController;
1415
use BookStack\Permissions\Permission;
16+
use Illuminate\Database\Eloquent\Builder;
17+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
1518
use Illuminate\Http\Request;
1619
use 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))

app/Entities/Models/Bookshelf.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Bookshelf extends Entity implements HasDescriptionInterface, HasCoverInter
1919

2020
public float $searchFactor = 1.2;
2121

22-
protected $hidden = ['image_id', 'deleted_at', 'description_html', 'priority', 'default_template_id', 'sort_rule_id', 'entity_id', 'entity_type', 'chapter_id', 'book_id'];
22+
protected $hidden = ['pivot', 'image_id', 'deleted_at', 'description_html', 'priority', 'default_template_id', 'sort_rule_id', 'entity_id', 'entity_type', 'chapter_id', 'book_id'];
2323
protected $fillable = ['name'];
2424

2525
/**

tests/Api/BooksApiTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,37 @@ public function test_read_endpoint_contents_nested_pages_has_permissions_applied
188188
$resp->assertJsonMissing(['name' => $customName]);
189189
}
190190

191+
public function test_read_endpoint_lists_visible_shelves_the_book_is_assigned_to()
192+
{
193+
$this->actingAsApiEditor();
194+
$shelf = $this->entities->shelf();
195+
$otherShelf = $this->entities->shelf();
196+
$book = $this->entities->book();
197+
$book->shelves()->detach();
198+
199+
$book->shelves()->attach($shelf);
200+
$book->shelves()->attach($otherShelf);
201+
202+
$this->assertEquals(2, $book->shelves()->count());
203+
204+
$this->permissions->disableEntityInheritedPermissions($otherShelf);
205+
206+
$resp = $this->getJson("{$this->baseEndpoint}/{$book->id}");
207+
$resp->assertOk();
208+
$resp->assertJsonCount(1, 'shelves');
209+
$resp->assertJson([
210+
'shelves' => [
211+
[
212+
'id' => $shelf->id,
213+
'name' => $shelf->name,
214+
'slug' => $shelf->slug,
215+
]
216+
]
217+
]);
218+
$resp->assertJsonMissingPath('shelves.0.description');
219+
$resp->assertJsonMissingPath('shelves.0.pivot');
220+
}
221+
191222
public function test_update_endpoint()
192223
{
193224
$this->actingAsApiEditor();

0 commit comments

Comments
 (0)