]> BookStack Code Mirror - bookstack/blob - app/Entities/Controllers/BookApiController.php
Dockerfile: Don't cache 50MB of lists and use a single layer, make it pretty
[bookstack] / app / Entities / Controllers / BookApiController.php
1 <?php
2
3 namespace BookStack\Entities\Controllers;
4
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;
15
16 class BookApiController extends ApiController
17 {
18     public function __construct(
19         protected BookRepo $bookRepo,
20         protected BookQueries $queries,
21     ) {
22     }
23
24     /**
25      * Get a listing of books visible to the user.
26      */
27     public function list()
28     {
29         $books = $this->queries
30             ->visibleForList()
31             ->addSelect(['created_by', 'updated_by']);
32
33         return $this->apiListingResponse($books, [
34             'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
35         ]);
36     }
37
38     /**
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.
42      *
43      * @throws ValidationException
44      */
45     public function create(Request $request)
46     {
47         $this->checkPermission('book-create-all');
48         $requestData = $this->validate($request, $this->rules()['create']);
49
50         $book = $this->bookRepo->create($requestData);
51
52         return response()->json($this->forJsonDisplay($book));
53     }
54
55     /**
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.
60      */
61     public function read(string $id)
62     {
63         $book = $this->queries->findVisibleByIdOrFail(intval($id));
64         $book = $this->forJsonDisplay($book);
65         $book->load(['createdBy', 'updatedBy', 'ownedBy']);
66
67         $contents = (new BookContents($book))->getTree(true, false)->all();
68         $contentsApiData = (new ApiEntityListFormatter($contents))
69             ->withType()
70             ->withField('pages', function (Entity $entity) {
71                 if ($entity instanceof Chapter) {
72                     return (new ApiEntityListFormatter($entity->pages->all()))->format();
73                 }
74                 return null;
75             })->format();
76         $book->setAttribute('contents', $contentsApiData);
77
78         return response()->json($book);
79     }
80
81     /**
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.
85      *
86      * @throws ValidationException
87      */
88     public function update(Request $request, string $id)
89     {
90         $book = $this->queries->findVisibleByIdOrFail(intval($id));
91         $this->checkOwnablePermission('book-update', $book);
92
93         $requestData = $this->validate($request, $this->rules()['update']);
94         $book = $this->bookRepo->update($book, $requestData);
95
96         return response()->json($this->forJsonDisplay($book));
97     }
98
99     /**
100      * Delete a single book.
101      * This will typically send the book to the recycle bin.
102      *
103      * @throws \Exception
104      */
105     public function delete(string $id)
106     {
107         $book = $this->queries->findVisibleByIdOrFail(intval($id));
108         $this->checkOwnablePermission('book-delete', $book);
109
110         $this->bookRepo->destroy($book);
111
112         return response('', 204);
113     }
114
115     protected function forJsonDisplay(Book $book): Book
116     {
117         $book = clone $book;
118         $book->unsetRelations()->refresh();
119
120         $book->load(['tags', 'cover']);
121         $book->makeVisible('description_html')
122             ->setAttribute('description_html', $book->descriptionHtml());
123
124         return $book;
125     }
126
127     protected function rules(): array
128     {
129         return [
130             'create' => [
131                 'name'                => ['required', 'string', 'max:255'],
132                 'description'         => ['string', 'max:1900'],
133                 'description_html'    => ['string', 'max:2000'],
134                 'tags'                => ['array'],
135                 'image'               => array_merge(['nullable'], $this->getImageValidationRules()),
136                 'default_template_id' => ['nullable', 'integer'],
137             ],
138             'update' => [
139                 'name'                => ['string', 'min:1', 'max:255'],
140                 'description'         => ['string', 'max:1900'],
141                 'description_html'    => ['string', 'max:2000'],
142                 'tags'                => ['array'],
143                 'image'               => array_merge(['nullable'], $this->getImageValidationRules()),
144                 'default_template_id' => ['nullable', 'integer'],
145             ],
146         ];
147     }
148 }