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