Skip to content

Commit 7e1a8e5

Browse files
committed
API: Added cover to book/shelf list endpoints
Aligns with what we provide in the UI. Added/updated tests to cover, and updated API examples. For 5180.
1 parent 19ee1c9 commit 7e1a8e5

File tree

7 files changed

+67
-5
lines changed

7 files changed

+67
-5
lines changed

app/Entities/Controllers/BookApiController.php

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public function list()
3030
{
3131
$books = $this->queries
3232
->visibleForList()
33+
->with(['cover:id,name,url'])
3334
->addSelect(['created_by', 'updated_by']);
3435

3536
return $this->apiListingResponse($books, [

app/Entities/Controllers/BookshelfApiController.php

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function list()
2626
{
2727
$shelves = $this->queries
2828
->visibleForList()
29+
->with(['cover:id,name,url'])
2930
->addSelect(['created_by', 'updated_by']);
3031

3132
return $this->apiListingResponse($shelves, [

dev/api/responses/books-list.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"updated_at": "2019-12-11T20:57:31.000000Z",
1010
"created_by": 1,
1111
"updated_by": 1,
12-
"owned_by": 1
12+
"owned_by": 1,
13+
"cover": null
1314
},
1415
{
1516
"id": 2,
@@ -20,7 +21,12 @@
2021
"updated_at": "2019-12-11T20:57:23.000000Z",
2122
"created_by": 4,
2223
"updated_by": 3,
23-
"owned_by": 3
24+
"owned_by": 3,
25+
"cover": {
26+
"id": 11,
27+
"name": "cat_banner.jpg",
28+
"url": "https://example.com/uploads/images/cover_book/2021-10/cat-banner.jpg"
29+
}
2430
}
2531
],
2632
"total": 14

dev/api/responses/shelves-list.json

+10-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
"updated_at": "2020-04-10T13:00:45.000000Z",
1010
"created_by": 4,
1111
"updated_by": 1,
12-
"owned_by": 1
12+
"owned_by": 1,
13+
"cover": {
14+
"id": 4,
15+
"name": "shelf.jpg",
16+
"url": "https://example.com/uploads/images/cover_bookshelf/2024-12/shelf.jpg"
17+
}
1318
},
1419
{
1520
"id": 9,
@@ -20,7 +25,8 @@
2025
"updated_at": "2020-04-10T13:00:58.000000Z",
2126
"created_by": 4,
2227
"updated_by": 1,
23-
"owned_by": 1
28+
"owned_by": 1,
29+
"cover": null
2430
},
2531
{
2632
"id": 10,
@@ -31,7 +37,8 @@
3137
"updated_at": "2020-04-10T13:00:53.000000Z",
3238
"created_by": 4,
3339
"updated_by": 1,
34-
"owned_by": 4
40+
"owned_by": 4,
41+
"cover": null
3542
}
3643
],
3744
"total": 3

tests/Api/BooksApiTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests\Api;
44

55
use BookStack\Entities\Models\Book;
6+
use BookStack\Entities\Repos\BaseRepo;
67
use Carbon\Carbon;
78
use Illuminate\Support\Facades\DB;
89
use Tests\TestCase;
@@ -27,6 +28,28 @@ public function test_index_endpoint_returns_expected_book()
2728
'owned_by' => $firstBook->owned_by,
2829
'created_by' => $firstBook->created_by,
2930
'updated_by' => $firstBook->updated_by,
31+
'cover' => null,
32+
],
33+
]]);
34+
}
35+
36+
public function test_index_endpoint_includes_cover_if_set()
37+
{
38+
$this->actingAsApiEditor();
39+
$book = $this->entities->book();
40+
41+
$baseRepo = $this->app->make(BaseRepo::class);
42+
$image = $this->files->uploadedImage('book_cover');
43+
$baseRepo->updateCoverImage($book, $image);
44+
45+
$resp = $this->getJson($this->baseEndpoint . '?filter[id]=' . $book->id);
46+
$resp->assertJson(['data' => [
47+
[
48+
'id' => $book->id,
49+
'cover' => [
50+
'id' => $book->cover->id,
51+
'url' => $book->cover->url,
52+
],
3053
],
3154
]]);
3255
}

tests/Api/ShelvesApiTest.php

+23
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use BookStack\Entities\Models\Book;
66
use BookStack\Entities\Models\Bookshelf;
7+
use BookStack\Entities\Repos\BaseRepo;
78
use Carbon\Carbon;
89
use Illuminate\Support\Facades\DB;
910
use Tests\TestCase;
@@ -28,6 +29,28 @@ public function test_index_endpoint_returns_expected_shelf()
2829
'owned_by' => $firstBookshelf->owned_by,
2930
'created_by' => $firstBookshelf->created_by,
3031
'updated_by' => $firstBookshelf->updated_by,
32+
'cover' => null,
33+
],
34+
]]);
35+
}
36+
37+
public function test_index_endpoint_includes_cover_if_set()
38+
{
39+
$this->actingAsApiEditor();
40+
$shelf = $this->entities->shelf();
41+
42+
$baseRepo = $this->app->make(BaseRepo::class);
43+
$image = $this->files->uploadedImage('shelf_cover');
44+
$baseRepo->updateCoverImage($shelf, $image);
45+
46+
$resp = $this->getJson($this->baseEndpoint . '?filter[id]=' . $shelf->id);
47+
$resp->assertJson(['data' => [
48+
[
49+
'id' => $shelf->id,
50+
'cover' => [
51+
'id' => $shelf->cover->id,
52+
'url' => $shelf->cover->url,
53+
],
3154
],
3255
]]);
3356
}

tests/Helpers/EntityProvider.php

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use BookStack\Entities\Models\Bookshelf;
77
use BookStack\Entities\Models\Chapter;
88
use BookStack\Entities\Models\Entity;
9+
use BookStack\Entities\Models\HasCoverImage;
910
use BookStack\Entities\Models\Page;
1011
use BookStack\Entities\Repos\BookRepo;
1112
use BookStack\Entities\Repos\BookshelfRepo;

0 commit comments

Comments
 (0)