3 namespace BookStack\Entities\Controllers;
5 use BookStack\Api\ApiEntityListFormatter;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Chapter;
8 use BookStack\Entities\Models\Entity;
9 use BookStack\Entities\Repos\BookRepo;
10 use BookStack\Entities\Tools\BookContents;
11 use BookStack\Http\Controllers\ApiController;
12 use Illuminate\Http\Request;
13 use Illuminate\Validation\ValidationException;
15 class BookApiController extends ApiController
17 protected BookRepo $bookRepo;
19 public function __construct(BookRepo $bookRepo)
21 $this->bookRepo = $bookRepo;
25 * Get a listing of books visible to the user.
27 public function list()
29 $books = Book::visible();
31 return $this->apiListingResponse($books, [
32 'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
37 * Create a new book in the system.
38 * The cover image of a book can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
39 * If the 'image' property is null then the book cover image will be removed.
41 * @throws ValidationException
43 public function create(Request $request)
45 $this->checkPermission('book-create-all');
46 $requestData = $this->validate($request, $this->rules()['create']);
48 $book = $this->bookRepo->create($requestData);
50 return response()->json($book);
54 * View the details of a single book.
55 * The response data will contain 'content' property listing the chapter and pages directly within, in
56 * the same structure as you'd see within the BookStack interface when viewing a book. Top-level
57 * contents will have a 'type' property to distinguish between pages & chapters.
59 public function read(string $id)
61 $book = Book::visible()->with(['tags', 'cover', 'createdBy', 'updatedBy', 'ownedBy'])->findOrFail($id);
63 $contents = (new BookContents($book))->getTree(true, false)->all();
64 $contentsApiData = (new ApiEntityListFormatter($contents))
66 ->withField('pages', function (Entity $entity) {
67 if ($entity instanceof Chapter) {
68 return (new ApiEntityListFormatter($entity->pages->all()))->format();
72 $book->setAttribute('contents', $contentsApiData);
74 return response()->json($book);
78 * Update the details of a single book.
79 * The cover image of a book can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
80 * If the 'image' property is null then the book cover image will be removed.
82 * @throws ValidationException
84 public function update(Request $request, string $id)
86 $book = Book::visible()->findOrFail($id);
87 $this->checkOwnablePermission('book-update', $book);
89 $requestData = $this->validate($request, $this->rules()['update']);
90 $book = $this->bookRepo->update($book, $requestData);
92 return response()->json($book);
96 * Delete a single book.
97 * This will typically send the book to the recycle bin.
101 public function delete(string $id)
103 $book = Book::visible()->findOrFail($id);
104 $this->checkOwnablePermission('book-delete', $book);
106 $this->bookRepo->destroy($book);
108 return response('', 204);
111 protected function rules(): array
115 'name' => ['required', 'string', 'max:255'],
116 'description' => ['string', 'max:1000'],
118 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
121 'name' => ['string', 'min:1', 'max:255'],
122 'description' => ['string', 'max:1000'],
124 'image' => array_merge(['nullable'], $this->getImageValidationRules()),