]> BookStack Code Mirror - bookstack/blob - app/Entities/Controllers/BookApiController.php
Input WYSIWYG: Updated API to show/accept html descriptions
[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\Repos\BookRepo;
10 use BookStack\Entities\Tools\BookContents;
11 use BookStack\Http\ApiController;
12 use Illuminate\Http\Request;
13 use Illuminate\Validation\ValidationException;
14
15 class BookApiController extends ApiController
16 {
17     public function __construct(
18         protected BookRepo $bookRepo
19     ) {
20     }
21
22     /**
23      * Get a listing of books visible to the user.
24      */
25     public function list()
26     {
27         $books = Book::visible();
28
29         return $this->apiListingResponse($books, [
30             'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
31         ]);
32     }
33
34     /**
35      * Create a new book in the system.
36      * The cover image of a book can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
37      * If the 'image' property is null then the book cover image will be removed.
38      *
39      * @throws ValidationException
40      */
41     public function create(Request $request)
42     {
43         $this->checkPermission('book-create-all');
44         $requestData = $this->validate($request, $this->rules()['create']);
45
46         $book = $this->bookRepo->create($requestData);
47
48         return response()->json($this->forJsonDisplay($book));
49     }
50
51     /**
52      * View the details of a single book.
53      * The response data will contain 'content' property listing the chapter and pages directly within, in
54      * the same structure as you'd see within the BookStack interface when viewing a book. Top-level
55      * contents will have a 'type' property to distinguish between pages & chapters.
56      */
57     public function read(string $id)
58     {
59         $book = Book::visible()->findOrFail($id);
60         $book = $this->forJsonDisplay($book);
61         $book->load(['createdBy', 'updatedBy', 'ownedBy']);
62
63         $contents = (new BookContents($book))->getTree(true, false)->all();
64         $contentsApiData = (new ApiEntityListFormatter($contents))
65             ->withType()
66             ->withField('pages', function (Entity $entity) {
67                 if ($entity instanceof Chapter) {
68                     return (new ApiEntityListFormatter($entity->pages->all()))->format();
69                 }
70                 return null;
71             })->format();
72         $book->setAttribute('contents', $contentsApiData);
73
74         return response()->json($book);
75     }
76
77     /**
78      * Update the details of a single book.
79      * The cover image of a book can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
80      * If the 'image' property is null then the book cover image will be removed.
81      *
82      * @throws ValidationException
83      */
84     public function update(Request $request, string $id)
85     {
86         $book = Book::visible()->findOrFail($id);
87         $this->checkOwnablePermission('book-update', $book);
88
89         $requestData = $this->validate($request, $this->rules()['update']);
90         $book = $this->bookRepo->update($book, $requestData);
91
92         return response()->json($this->forJsonDisplay($book));
93     }
94
95     /**
96      * Delete a single book.
97      * This will typically send the book to the recycle bin.
98      *
99      * @throws \Exception
100      */
101     public function delete(string $id)
102     {
103         $book = Book::visible()->findOrFail($id);
104         $this->checkOwnablePermission('book-delete', $book);
105
106         $this->bookRepo->destroy($book);
107
108         return response('', 204);
109     }
110
111     protected function forJsonDisplay(Book $book): Book
112     {
113         $book = clone $book;
114         $book->unsetRelations()->refresh();
115
116         $book->load(['tags', 'cover']);
117         $book->makeVisible('description_html')
118             ->setAttribute('description_html', $book->descriptionHtml());
119
120         return $book;
121     }
122
123     protected function rules(): array
124     {
125         return [
126             'create' => [
127                 'name'                => ['required', 'string', 'max:255'],
128                 'description'         => ['string', 'max:1900'],
129                 'description_html'    => ['string', 'max:2000'],
130                 'tags'                => ['array'],
131                 'image'               => array_merge(['nullable'], $this->getImageValidationRules()),
132                 'default_template_id' => ['nullable', 'integer'],
133             ],
134             'update' => [
135                 'name'                => ['string', 'min:1', 'max:255'],
136                 'description'         => ['string', 'max:1900'],
137                 'description_html'    => ['string', 'max:2000'],
138                 'tags'                => ['array'],
139                 'image'               => array_merge(['nullable'], $this->getImageValidationRules()),
140                 'default_template_id' => ['nullable', 'integer'],
141             ],
142         ];
143     }
144 }