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