Skip to content

Commit

Permalink
API: Added cover to book/shelf list endpoints
Browse files Browse the repository at this point in the history
Aligns with what we provide in the UI.
Added/updated tests to cover, and updated API examples.

For 5180.
  • Loading branch information
ssddanbrown committed Dec 13, 2024
1 parent 19ee1c9 commit 7e1a8e5
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/Entities/Controllers/BookApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function list()
{
$books = $this->queries
->visibleForList()
->with(['cover:id,name,url'])
->addSelect(['created_by', 'updated_by']);

return $this->apiListingResponse($books, [
Expand Down
1 change: 1 addition & 0 deletions app/Entities/Controllers/BookshelfApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function list()
{
$shelves = $this->queries
->visibleForList()
->with(['cover:id,name,url'])
->addSelect(['created_by', 'updated_by']);

return $this->apiListingResponse($shelves, [
Expand Down
10 changes: 8 additions & 2 deletions dev/api/responses/books-list.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"updated_at": "2019-12-11T20:57:31.000000Z",
"created_by": 1,
"updated_by": 1,
"owned_by": 1
"owned_by": 1,
"cover": null
},
{
"id": 2,
Expand All @@ -20,7 +21,12 @@
"updated_at": "2019-12-11T20:57:23.000000Z",
"created_by": 4,
"updated_by": 3,
"owned_by": 3
"owned_by": 3,
"cover": {
"id": 11,
"name": "cat_banner.jpg",
"url": "https://example.com/uploads/images/cover_book/2021-10/cat-banner.jpg"
}
}
],
"total": 14
Expand Down
13 changes: 10 additions & 3 deletions dev/api/responses/shelves-list.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
"updated_at": "2020-04-10T13:00:45.000000Z",
"created_by": 4,
"updated_by": 1,
"owned_by": 1
"owned_by": 1,
"cover": {
"id": 4,
"name": "shelf.jpg",
"url": "https://example.com/uploads/images/cover_bookshelf/2024-12/shelf.jpg"
}
},
{
"id": 9,
Expand All @@ -20,7 +25,8 @@
"updated_at": "2020-04-10T13:00:58.000000Z",
"created_by": 4,
"updated_by": 1,
"owned_by": 1
"owned_by": 1,
"cover": null
},
{
"id": 10,
Expand All @@ -31,7 +37,8 @@
"updated_at": "2020-04-10T13:00:53.000000Z",
"created_by": 4,
"updated_by": 1,
"owned_by": 4
"owned_by": 4,
"cover": null
}
],
"total": 3
Expand Down
23 changes: 23 additions & 0 deletions tests/Api/BooksApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Api;

use BookStack\Entities\Models\Book;
use BookStack\Entities\Repos\BaseRepo;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
Expand All @@ -27,6 +28,28 @@ public function test_index_endpoint_returns_expected_book()
'owned_by' => $firstBook->owned_by,
'created_by' => $firstBook->created_by,
'updated_by' => $firstBook->updated_by,
'cover' => null,
],
]]);
}

public function test_index_endpoint_includes_cover_if_set()
{
$this->actingAsApiEditor();
$book = $this->entities->book();

$baseRepo = $this->app->make(BaseRepo::class);
$image = $this->files->uploadedImage('book_cover');
$baseRepo->updateCoverImage($book, $image);

$resp = $this->getJson($this->baseEndpoint . '?filter[id]=' . $book->id);
$resp->assertJson(['data' => [
[
'id' => $book->id,
'cover' => [
'id' => $book->cover->id,
'url' => $book->cover->url,
],
],
]]);
}
Expand Down
23 changes: 23 additions & 0 deletions tests/Api/ShelvesApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Bookshelf;
use BookStack\Entities\Repos\BaseRepo;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
Expand All @@ -28,6 +29,28 @@ public function test_index_endpoint_returns_expected_shelf()
'owned_by' => $firstBookshelf->owned_by,
'created_by' => $firstBookshelf->created_by,
'updated_by' => $firstBookshelf->updated_by,
'cover' => null,
],
]]);
}

public function test_index_endpoint_includes_cover_if_set()
{
$this->actingAsApiEditor();
$shelf = $this->entities->shelf();

$baseRepo = $this->app->make(BaseRepo::class);
$image = $this->files->uploadedImage('shelf_cover');
$baseRepo->updateCoverImage($shelf, $image);

$resp = $this->getJson($this->baseEndpoint . '?filter[id]=' . $shelf->id);
$resp->assertJson(['data' => [
[
'id' => $shelf->id,
'cover' => [
'id' => $shelf->cover->id,
'url' => $shelf->cover->url,
],
],
]]);
}
Expand Down
1 change: 1 addition & 0 deletions tests/Helpers/EntityProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use BookStack\Entities\Models\Bookshelf;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Entity;
use BookStack\Entities\Models\HasCoverImage;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Repos\BookRepo;
use BookStack\Entities\Repos\BookshelfRepo;
Expand Down

0 comments on commit 7e1a8e5

Please sign in to comment.