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