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 ->with(['cover:id,name,url'])
34 ->addSelect(['created_by', 'updated_by']);
36 return $this->apiListingResponse($books, [
37 'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
42 * Create a new book in the system.
43 * The cover image of a book can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
44 * If the 'image' property is null then the book cover image will be removed.
46 * @throws ValidationException
48 public function create(Request $request)
50 $this->checkPermission('book-create-all');
51 $requestData = $this->validate($request, $this->rules()['create']);
53 $book = $this->bookRepo->create($requestData);
55 return response()->json($this->forJsonDisplay($book));
59 * View the details of a single book.
60 * The response data will contain 'content' property listing the chapter and pages directly within, in
61 * the same structure as you'd see within the BookStack interface when viewing a book. Top-level
62 * contents will have a 'type' property to distinguish between pages & chapters.
64 public function read(string $id)
66 $book = $this->queries->findVisibleByIdOrFail(intval($id));
67 $book = $this->forJsonDisplay($book);
68 $book->load(['createdBy', 'updatedBy', 'ownedBy']);
70 $contents = (new BookContents($book))->getTree(true, false)->all();
71 $contentsApiData = (new ApiEntityListFormatter($contents))
73 ->withField('pages', function (Entity $entity) {
74 if ($entity instanceof Chapter) {
75 $pages = $this->pageQueries->visibleForChapterList($entity->id)->get()->all();
76 return (new ApiEntityListFormatter($pages))->format();
80 $book->setAttribute('contents', $contentsApiData);
82 return response()->json($book);
86 * Update the details of a single book.
87 * The cover image of a book can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
88 * If the 'image' property is null then the book cover image will be removed.
90 * @throws ValidationException
92 public function update(Request $request, string $id)
94 $book = $this->queries->findVisibleByIdOrFail(intval($id));
95 $this->checkOwnablePermission('book-update', $book);
97 $requestData = $this->validate($request, $this->rules()['update']);
98 $book = $this->bookRepo->update($book, $requestData);
100 return response()->json($this->forJsonDisplay($book));
104 * Delete a single book.
105 * This will typically send the book to the recycle bin.
109 public function delete(string $id)
111 $book = $this->queries->findVisibleByIdOrFail(intval($id));
112 $this->checkOwnablePermission('book-delete', $book);
114 $this->bookRepo->destroy($book);
116 return response('', 204);
119 protected function forJsonDisplay(Book $book): Book
122 $book->unsetRelations()->refresh();
124 $book->load(['tags', 'cover']);
125 $book->makeVisible('description_html')
126 ->setAttribute('description_html', $book->descriptionHtml());
131 protected function rules(): array
135 'name' => ['required', 'string', 'max:255'],
136 'description' => ['string', 'max:1900'],
137 'description_html' => ['string', 'max:2000'],
139 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
140 'default_template_id' => ['nullable', 'integer'],
143 'name' => ['string', 'min:1', 'max:255'],
144 'description' => ['string', 'max:1900'],
145 'description_html' => ['string', 'max:2000'],
147 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
148 'default_template_id' => ['nullable', 'integer'],