]> BookStack Code Mirror - bookstack/commitdiff
API: Added cover to book/shelf list endpoints
authorDan Brown <redacted>
Fri, 13 Dec 2024 14:21:04 +0000 (14:21 +0000)
committerDan Brown <redacted>
Fri, 13 Dec 2024 14:21:04 +0000 (14:21 +0000)
Aligns with what we provide in the UI.
Added/updated tests to cover, and updated API examples.

For 5180.

app/Entities/Controllers/BookApiController.php
app/Entities/Controllers/BookshelfApiController.php
dev/api/responses/books-list.json
dev/api/responses/shelves-list.json
tests/Api/BooksApiTest.php
tests/Api/ShelvesApiTest.php
tests/Helpers/EntityProvider.php

index c1e38e72fe7c2cb5c1402870bce4dda7d90c9b1d..a617ee2da680f97752683ed543a081e24ecb8372 100644 (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, [
index a665bcb6bab7d314c35939f1c7e6aa3e4e902c28..b512f2d05531bc04511ac1dea7a716f52802100c 100644 (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, [
index 0f8458feda29861821108f85b887ec4261b22e7a..50c8c49e6ef1ea123ed1ec5524192e1981f7e87e 100644 (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,
       "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
index 4b1a1b43f930fc27b92694d23ecc0e29c822a86d..d5debfaef1037d29418e151485e098046fc57628 100644 (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
index 0de98dc323bf5ab14f8630075696a97a3f7e6118..084cb59bd5c633a11a512aa2814b2a9a95f8b996 100644 (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,
+                ],
             ],
         ]]);
     }
index be276e110040cd156c68e7d996ee26b458a1373c..ba13c0153b14519023a470f005d3bcbb16cf347f 100644 (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,
+                ],
             ],
         ]]);
     }
index 1897abefa28501009366f76661a925b1fb50214a..22e554f74b5caa08ec049d3668afeca0780fb313 100644 (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;
Morty Proxy This is a proxified and sanitized view of the page, visit original site.