]> BookStack Code Mirror - bookstack/blobdiff - app/Http/Controllers/Api/BookApiController.php
Added tests to cover convert functionality
[bookstack] / app / Http / Controllers / Api / BookApiController.php
index 8ec9fdc066ff36eeecaf078c7c762f839560ec65..73cac6318a994f65121278d73d8ba319bfd72f14 100644 (file)
@@ -1,35 +1,16 @@
-<?php namespace BookStack\Http\Controllers\Api;
+<?php
 
-use BookStack\Actions\ActivityType;
-use BookStack\Entities\Book;
+namespace BookStack\Http\Controllers\Api;
+
+use BookStack\Entities\Models\Book;
 use BookStack\Entities\Repos\BookRepo;
-use BookStack\Exceptions\NotifyException;
-use BookStack\Facades\Activity;
-use Illuminate\Contracts\Container\BindingResolutionException;
 use Illuminate\Http\Request;
 use Illuminate\Validation\ValidationException;
 
 class BookApiController extends ApiController
 {
-
     protected $bookRepo;
 
-    protected $rules = [
-        'create' => [
-            'name' => 'required|string|max:255',
-            'description' => 'string|max:1000',
-            'tags' => 'array',
-        ],
-        'update' => [
-            'name' => 'string|min:1|max:255',
-            'description' => 'string|max:1000',
-            'tags' => 'array',
-        ],
-    ];
-
-    /**
-     * BooksApiController constructor.
-     */
     public function __construct(BookRepo $bookRepo)
     {
         $this->bookRepo = $bookRepo;
@@ -41,13 +22,15 @@ class BookApiController extends ApiController
     public function list()
     {
         $books = Book::visible();
+
         return $this->apiListingResponse($books, [
-            'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'image_id',
+            'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
         ]);
     }
 
     /**
      * Create a new book in the system.
+     *
      * @throws ValidationException
      */
     public function create(Request $request)
@@ -56,6 +39,7 @@ class BookApiController extends ApiController
         $requestData = $this->validate($request, $this->rules['create']);
 
         $book = $this->bookRepo->create($requestData);
+
         return response()->json($book);
     }
 
@@ -64,12 +48,14 @@ class BookApiController extends ApiController
      */
     public function read(string $id)
     {
-        $book = Book::visible()->with(['tags', 'cover', 'createdBy', 'updatedBy'])->findOrFail($id);
+        $book = Book::visible()->with(['tags', 'cover', 'createdBy', 'updatedBy', 'ownedBy'])->findOrFail($id);
+
         return response()->json($book);
     }
 
     /**
      * Update the details of a single book.
+     *
      * @throws ValidationException
      */
     public function update(Request $request, string $id)
@@ -84,9 +70,10 @@ class BookApiController extends ApiController
     }
 
     /**
-     * Delete a single book from the system.
-     * @throws NotifyException
-     * @throws BindingResolutionException
+     * Delete a single book.
+     * This will typically send the book to the recycle bin.
+     *
+     * @throws \Exception
      */
     public function delete(string $id)
     {
@@ -94,6 +81,24 @@ class BookApiController extends ApiController
         $this->checkOwnablePermission('book-delete', $book);
 
         $this->bookRepo->destroy($book);
+
         return response('', 204);
     }
-}
\ No newline at end of file
+
+    protected function rules(): array {
+        return [
+            'create' => [
+                'name'        => ['required', 'string', 'max:255'],
+                'description' => ['string', 'max:1000'],
+                'tags'        => ['array'],
+                'image'       => array_merge(['nullable'], $this->getImageValidationRules()),
+            ],
+            'update' => [
+                'name'        => ['string', 'min:1', 'max:255'],
+                'description' => ['string', 'max:1000'],
+                'tags'        => ['array'],
+                'image'       => array_merge(['nullable'], $this->getImageValidationRules()),
+            ],
+        ];
+    }
+}