3 namespace BookStack\Http\Controllers\Api;
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 Illuminate\Http\Request;
12 use Illuminate\Validation\ValidationException;
14 class BookApiController extends ApiController
16 protected BookRepo $bookRepo;
18 public function __construct(BookRepo $bookRepo)
20 $this->bookRepo = $bookRepo;
24 * Get a listing of books visible to the user.
26 public function list()
28 $books = Book::visible();
30 return $this->apiListingResponse($books, [
31 'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
36 * Create a new book in the system.
37 * The cover image of a book can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
38 * If the 'image' property is null then the book cover image will be removed.
40 * @throws ValidationException
42 public function create(Request $request)
44 $this->checkPermission('book-create-all');
45 $requestData = $this->validate($request, $this->rules()['create']);
47 $book = $this->bookRepo->create($requestData);
49 return response()->json($book);
53 * View the details of a single book.
54 * The response data will contain 'content' property listing the chapter and pages directly within, in
55 * the same structure as you'd see within the BookStack interface when viewing a book. Top-level
56 * contents will have a 'type' property to distinguish between pages & chapters.
58 public function read(string $id)
60 $book = Book::visible()->with(['tags', 'cover', 'createdBy', 'updatedBy', 'ownedBy'])->findOrFail($id);
62 $contents = (new BookContents($book))->getTree(true, false)->all();
63 $contentsApiData = (new ApiEntityListFormatter($contents))
65 ->withField('pages', function (Entity $entity) {
66 if ($entity instanceof Chapter) {
67 return (new ApiEntityListFormatter($entity->pages->all()))->format();
71 $book->setAttribute('contents', $contentsApiData);
73 return response()->json($book);
77 * Update the details of a single book.
78 * The cover image of a book can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
79 * If the 'image' property is null then the book cover image will be removed.
81 * @throws ValidationException
83 public function update(Request $request, string $id)
85 $book = Book::visible()->findOrFail($id);
86 $this->checkOwnablePermission('book-update', $book);
88 $requestData = $this->validate($request, $this->rules()['update']);
89 $book = $this->bookRepo->update($book, $requestData);
91 return response()->json($book);
95 * Delete a single book.
96 * This will typically send the book to the recycle bin.
100 public function delete(string $id)
102 $book = Book::visible()->findOrFail($id);
103 $this->checkOwnablePermission('book-delete', $book);
105 $this->bookRepo->destroy($book);
107 return response('', 204);
110 protected function rules(): array
114 'name' => ['required', 'string', 'max:255'],
115 'description' => ['string', 'max:1000'],
117 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
120 'name' => ['string', 'min:1', 'max:255'],
121 'description' => ['string', 'max:1000'],
123 'image' => array_merge(['nullable'], $this->getImageValidationRules()),