3 namespace BookStack\Http\Controllers\Api;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Repos\BookRepo;
7 use Illuminate\Http\Request;
8 use Illuminate\Validation\ValidationException;
10 class BookApiController extends ApiController
14 public function __construct(BookRepo $bookRepo)
16 $this->bookRepo = $bookRepo;
20 * Get a listing of books visible to the user.
22 public function list()
24 $books = Book::visible();
26 return $this->apiListingResponse($books, [
27 'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
32 * Create a new book in the system.
34 * @throws ValidationException
36 public function create(Request $request)
38 $this->checkPermission('book-create-all');
39 $requestData = $this->validate($request, $this->rules['create']);
41 $book = $this->bookRepo->create($requestData);
43 return response()->json($book);
47 * View the details of a single book.
49 public function read(string $id)
51 $book = Book::visible()->with(['tags', 'cover', 'createdBy', 'updatedBy', 'ownedBy'])->findOrFail($id);
53 return response()->json($book);
57 * Update the details of a single book.
59 * @throws ValidationException
61 public function update(Request $request, string $id)
63 $book = Book::visible()->findOrFail($id);
64 $this->checkOwnablePermission('book-update', $book);
66 $requestData = $this->validate($request, $this->rules['update']);
67 $book = $this->bookRepo->update($book, $requestData);
69 return response()->json($book);
73 * Delete a single book.
74 * This will typically send the book to the recycle bin.
78 public function delete(string $id)
80 $book = Book::visible()->findOrFail($id);
81 $this->checkOwnablePermission('book-delete', $book);
83 $this->bookRepo->destroy($book);
85 return response('', 204);
88 protected function rules(): array {
91 'name' => ['required', 'string', 'max:255'],
92 'description' => ['string', 'max:1000'],
94 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
97 'name' => ['string', 'min:1', 'max:255'],
98 'description' => ['string', 'max:1000'],
100 'image' => array_merge(['nullable'], $this->getImageValidationRules()),