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\Queries\BookQueries;
10 use BookStack\Entities\Repos\BookRepo;
11 use BookStack\Entities\Tools\BookContents;
12 use BookStack\Http\ApiController;
13 use Illuminate\Http\Request;
14 use Illuminate\Validation\ValidationException;
16 class BookApiController extends ApiController
18 public function __construct(
19 protected BookRepo $bookRepo,
20 protected BookQueries $queries,
25 * Get a listing of books visible to the user.
27 public function list()
29 $books = $this->queries
31 ->addSelect(['created_by', 'updated_by']);
33 return $this->apiListingResponse($books, [
34 'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
39 * Create a new book in the system.
40 * The cover image of a book can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
41 * If the 'image' property is null then the book cover image will be removed.
43 * @throws ValidationException
45 public function create(Request $request)
47 $this->checkPermission('book-create-all');
48 $requestData = $this->validate($request, $this->rules()['create']);
50 $book = $this->bookRepo->create($requestData);
52 return response()->json($this->forJsonDisplay($book));
56 * View the details of a single book.
57 * The response data will contain 'content' property listing the chapter and pages directly within, in
58 * the same structure as you'd see within the BookStack interface when viewing a book. Top-level
59 * contents will have a 'type' property to distinguish between pages & chapters.
61 public function read(string $id)
63 $book = $this->queries->findVisibleByIdOrFail(intval($id));
64 $book = $this->forJsonDisplay($book);
65 $book->load(['createdBy', 'updatedBy', 'ownedBy']);
67 $contents = (new BookContents($book))->getTree(true, false)->all();
68 $contentsApiData = (new ApiEntityListFormatter($contents))
70 ->withField('pages', function (Entity $entity) {
71 if ($entity instanceof Chapter) {
72 return (new ApiEntityListFormatter($entity->pages->all()))->format();
76 $book->setAttribute('contents', $contentsApiData);
78 return response()->json($book);
82 * Update the details of a single book.
83 * The cover image of a book can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
84 * If the 'image' property is null then the book cover image will be removed.
86 * @throws ValidationException
88 public function update(Request $request, string $id)
90 $book = $this->queries->findVisibleByIdOrFail(intval($id));
91 $this->checkOwnablePermission('book-update', $book);
93 $requestData = $this->validate($request, $this->rules()['update']);
94 $book = $this->bookRepo->update($book, $requestData);
96 return response()->json($this->forJsonDisplay($book));
100 * Delete a single book.
101 * This will typically send the book to the recycle bin.
105 public function delete(string $id)
107 $book = $this->queries->findVisibleByIdOrFail(intval($id));
108 $this->checkOwnablePermission('book-delete', $book);
110 $this->bookRepo->destroy($book);
112 return response('', 204);
115 protected function forJsonDisplay(Book $book): Book
118 $book->unsetRelations()->refresh();
120 $book->load(['tags', 'cover']);
121 $book->makeVisible('description_html')
122 ->setAttribute('description_html', $book->descriptionHtml());
127 protected function rules(): array
131 'name' => ['required', 'string', 'max:255'],
132 'description' => ['string', 'max:1900'],
133 'description_html' => ['string', 'max:2000'],
135 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
136 'default_template_id' => ['nullable', 'integer'],
139 'name' => ['string', 'min:1', 'max:255'],
140 'description' => ['string', 'max:1900'],
141 'description_html' => ['string', 'max:2000'],
143 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
144 'default_template_id' => ['nullable', 'integer'],