]> BookStack Code Mirror - bookstack/blob - app/Entities/Controllers/BookshelfApiController.php
Input WYSIWYG: Updated API to show/accept html descriptions
[bookstack] / app / Entities / Controllers / BookshelfApiController.php
1 <?php
2
3 namespace BookStack\Entities\Controllers;
4
5 use BookStack\Entities\Models\Bookshelf;
6 use BookStack\Entities\Repos\BookshelfRepo;
7 use BookStack\Http\ApiController;
8 use Exception;
9 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
10 use Illuminate\Http\Request;
11 use Illuminate\Validation\ValidationException;
12
13 class BookshelfApiController extends ApiController
14 {
15     public function __construct(
16         protected BookshelfRepo $bookshelfRepo
17     ) {
18     }
19
20     /**
21      * Get a listing of shelves visible to the user.
22      */
23     public function list()
24     {
25         $shelves = Bookshelf::visible();
26
27         return $this->apiListingResponse($shelves, [
28             'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
29         ]);
30     }
31
32     /**
33      * Create a new shelf in the system.
34      * An array of books IDs can be provided in the request. These
35      * will be added to the shelf in the same order as provided.
36      * The cover image of a shelf 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 shelf cover image will be removed.
38      *
39      * @throws ValidationException
40      */
41     public function create(Request $request)
42     {
43         $this->checkPermission('bookshelf-create-all');
44         $requestData = $this->validate($request, $this->rules()['create']);
45
46         $bookIds = $request->get('books', []);
47         $shelf = $this->bookshelfRepo->create($requestData, $bookIds);
48
49         return response()->json($this->forJsonDisplay($shelf));
50     }
51
52     /**
53      * View the details of a single shelf.
54      */
55     public function read(string $id)
56     {
57         $shelf = Bookshelf::visible()->findOrFail($id);
58         $shelf = $this->forJsonDisplay($shelf);
59         $shelf->load([
60             'createdBy', 'updatedBy', 'ownedBy',
61             'books' => function (BelongsToMany $query) {
62                 $query->scopes('visible')->get(['id', 'name', 'slug']);
63             },
64         ]);
65
66         return response()->json($shelf);
67     }
68
69     /**
70      * Update the details of a single shelf.
71      * An array of books IDs can be provided in the request. These
72      * will be added to the shelf in the same order as provided and overwrite
73      * any existing book assignments.
74      * The cover image of a shelf can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
75      * If the 'image' property is null then the shelf cover image will be removed.
76      *
77      * @throws ValidationException
78      */
79     public function update(Request $request, string $id)
80     {
81         $shelf = Bookshelf::visible()->findOrFail($id);
82         $this->checkOwnablePermission('bookshelf-update', $shelf);
83
84         $requestData = $this->validate($request, $this->rules()['update']);
85         $bookIds = $request->get('books', null);
86
87         $shelf = $this->bookshelfRepo->update($shelf, $requestData, $bookIds);
88
89         return response()->json($this->forJsonDisplay($shelf));
90     }
91
92     /**
93      * Delete a single shelf.
94      * This will typically send the shelf to the recycle bin.
95      *
96      * @throws Exception
97      */
98     public function delete(string $id)
99     {
100         $shelf = Bookshelf::visible()->findOrFail($id);
101         $this->checkOwnablePermission('bookshelf-delete', $shelf);
102
103         $this->bookshelfRepo->destroy($shelf);
104
105         return response('', 204);
106     }
107
108     protected function forJsonDisplay(Bookshelf $shelf): Bookshelf
109     {
110         $shelf = clone $shelf;
111         $shelf->unsetRelations()->refresh();
112
113         $shelf->load(['tags', 'cover']);
114         $shelf->makeVisible('description_html')
115             ->setAttribute('description_html', $shelf->descriptionHtml());
116
117         return $shelf;
118     }
119
120     protected function rules(): array
121     {
122         return [
123             'create' => [
124                 'name'             => ['required', 'string', 'max:255'],
125                 'description'      => ['string', 'max:1900'],
126                 'description_html' => ['string', 'max:2000'],
127                 'books'            => ['array'],
128                 'tags'             => ['array'],
129                 'image'            => array_merge(['nullable'], $this->getImageValidationRules()),
130             ],
131             'update' => [
132                 'name'             => ['string', 'min:1', 'max:255'],
133                 'description'      => ['string', 'max:1900'],
134                 'description_html' => ['string', 'max:2000'],
135                 'books'            => ['array'],
136                 'tags'             => ['array'],
137                 'image'            => array_merge(['nullable'], $this->getImageValidationRules()),
138             ],
139         ];
140     }
141 }