X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/2f6ff0734773c4ac009de699a2661971fd585b22..663f81a2b1eba75883fbab6577a386351b86f623:/app/Http/Controllers/Api/BookApiController.php diff --git a/app/Http/Controllers/Api/BookApiController.php b/app/Http/Controllers/Api/BookApiController.php index 8333eba3a..73cac6318 100644 --- a/app/Http/Controllers/Api/BookApiController.php +++ b/app/Http/Controllers/Api/BookApiController.php @@ -1,34 +1,16 @@ - [ - '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; @@ -40,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) @@ -55,7 +39,6 @@ class BookApiController extends ApiController $requestData = $this->validate($request, $this->rules['create']); $book = $this->bookRepo->create($requestData); - Activity::add($book, 'book_create', $book->id); return response()->json($book); } @@ -65,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) @@ -80,15 +65,15 @@ class BookApiController extends ApiController $requestData = $this->validate($request, $this->rules['update']); $book = $this->bookRepo->update($book, $requestData); - Activity::add($book, 'book_update', $book->id); return response()->json($book); } /** - * 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) { @@ -96,8 +81,24 @@ class BookApiController extends ApiController $this->checkOwnablePermission('book-delete', $book); $this->bookRepo->destroy($book); - Activity::addMessage('book_delete', $book->name); 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()), + ], + ]; + } +}