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\Queries\PageQueries;
11 use BookStack\Entities\Repos\BookRepo;
12 use BookStack\Entities\Tools\BookContents;
13 use BookStack\Http\ApiController;
14 use Illuminate\Http\Request;
15 use Illuminate\Validation\ValidationException;
17 class BookApiController extends ApiController
19 public function __construct(
20 protected BookRepo $bookRepo,
21 protected BookQueries $queries,
22 protected PageQueries $pageQueries,
27 * Get a listing of books visible to the user.
29 public function list()
31 $books = $this->queries
33 ->addSelect(['created_by', 'updated_by']);
35 return $this->apiListingResponse($books, [
36 'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
41 * Create a new book in the system.
42 * The cover image of a book can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
43 * If the 'image' property is null then the book cover image will be removed.
45 * @throws ValidationException
47 public function create(Request $request)
49 $this->checkPermission('book-create-all');
50 $requestData = $this->validate($request, $this->rules()['create']);
52 $book = $this->bookRepo->create($requestData);
54 return response()->json($this->forJsonDisplay($book));
58 * View the details of a single book.
59 * The response data will contain 'content' property listing the chapter and pages directly within, in
60 * the same structure as you'd see within the BookStack interface when viewing a book. Top-level
61 * contents will have a 'type' property to distinguish between pages & chapters.
63 public function read(string $id)
65 $book = $this->queries->findVisibleByIdOrFail(intval($id));
66 $book = $this->forJsonDisplay($book);
67 $book->load(['createdBy', 'updatedBy', 'ownedBy']);
69 $contents = (new BookContents($book))->getTree(true, false)->all();
70 $contentsApiData = (new ApiEntityListFormatter($contents))
72 ->withField('pages', function (Entity $entity) {
73 if ($entity instanceof Chapter) {
74 $pages = $this->pageQueries->visibleForChapterList($entity->id)->get()->all();
75 return (new ApiEntityListFormatter($pages))->format();
79 $book->setAttribute('contents', $contentsApiData);
81 return response()->json($book);
85 * Update the details of a single book.
86 * The cover image of a book can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
87 * If the 'image' property is null then the book cover image will be removed.
89 * @throws ValidationException
91 public function update(Request $request, string $id)
93 $book = $this->queries->findVisibleByIdOrFail(intval($id));
94 $this->checkOwnablePermission('book-update', $book);
96 $requestData = $this->validate($request, $this->rules()['update']);
97 $book = $this->bookRepo->update($book, $requestData);
99 return response()->json($this->forJsonDisplay($book));
103 * Delete a single book.
104 * This will typically send the book to the recycle bin.
108 public function delete(string $id)
110 $book = $this->queries->findVisibleByIdOrFail(intval($id));
111 $this->checkOwnablePermission('book-delete', $book);
113 $this->bookRepo->destroy($book);
115 return response('', 204);
118 protected function forJsonDisplay(Book $book): Book
121 $book->unsetRelations()->refresh();
123 $book->load(['tags', 'cover']);
124 $book->makeVisible('description_html')
125 ->setAttribute('description_html', $book->descriptionHtml());
130 protected function rules(): array
134 'name' => ['required', 'string', 'max:255'],
135 'description' => ['string', 'max:1900'],
136 'description_html' => ['string', 'max:2000'],
138 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
139 'default_template_id' => ['nullable', 'integer'],
142 'name' => ['string', 'min:1', 'max:255'],
143 'description' => ['string', 'max:1900'],
144 'description_html' => ['string', 'max:2000'],
146 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
147 'default_template_id' => ['nullable', 'integer'],