API: Added cover to book/shelf list endpoints
Some checks failed
analyse-php / build (push) Waiting to run
lint-php / build (push) Waiting to run
test-migrations / build (8.1) (push) Waiting to run
test-migrations / build (8.2) (push) Waiting to run
test-migrations / build (8.3) (push) Waiting to run
test-migrations / build (8.4) (push) Waiting to run
test-php / build (8.1) (push) Waiting to run
test-php / build (8.2) (push) Waiting to run
test-php / build (8.3) (push) Waiting to run
test-php / build (8.4) (push) Waiting to run
test-js / build (push) Has been cancelled
lint-js / build (push) Has been cancelled

Aligns with what we provide in the UI.
Added/updated tests to cover, and updated API examples.

For 5180.
This commit is contained in:
Dan Brown 2024-12-13 14:21:04 +00:00
parent 19ee1c9be7
commit 7e1a8e5ec6
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
7 changed files with 67 additions and 5 deletions

View File

@ -30,6 +30,7 @@ class BookApiController extends ApiController
{
$books = $this->queries
->visibleForList()
->with(['cover:id,name,url'])
->addSelect(['created_by', 'updated_by']);
return $this->apiListingResponse($books, [

View File

@ -26,6 +26,7 @@ class BookshelfApiController extends ApiController
{
$shelves = $this->queries
->visibleForList()
->with(['cover:id,name,url'])
->addSelect(['created_by', 'updated_by']);
return $this->apiListingResponse($shelves, [

View File

@ -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,
@ -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

View File

@ -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,
@ -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,
@ -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

View File

@ -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;
@ -27,6 +28,28 @@ class BooksApiTest extends TestCase
'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,
],
],
]]);
}

View File

@ -4,6 +4,7 @@ namespace Tests\Api;
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;
@ -28,6 +29,28 @@ class ShelvesApiTest extends TestCase
'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,
],
],
]]);
}

View File

@ -6,6 +6,7 @@ use BookStack\Entities\Models\Book;
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;