]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/BookshelfApiController.php
Merge pull request #3499 from BookStackApp/convert_hierachy
[bookstack] / app / Http / Controllers / Api / BookshelfApiController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Api;
4
5 use BookStack\Entities\Models\Bookshelf;
6 use BookStack\Entities\Repos\BookshelfRepo;
7 use Exception;
8 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
9 use Illuminate\Http\Request;
10 use Illuminate\Validation\ValidationException;
11
12 class BookshelfApiController extends ApiController
13 {
14     protected BookshelfRepo $bookshelfRepo;
15
16     /**
17      * BookshelfApiController constructor.
18      */
19     public function __construct(BookshelfRepo $bookshelfRepo)
20     {
21         $this->bookshelfRepo = $bookshelfRepo;
22     }
23
24     /**
25      * Get a listing of shelves visible to the user.
26      */
27     public function list()
28     {
29         $shelves = Bookshelf::visible();
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($shelf);
54     }
55
56     /**
57      * View the details of a single shelf.
58      */
59     public function read(string $id)
60     {
61         $shelf = Bookshelf::visible()->with([
62             'tags', 'cover', 'createdBy', 'updatedBy', 'ownedBy',
63             'books' => function (BelongsToMany $query) {
64                 $query->scopes('visible')->get(['id', 'name', 'slug']);
65             },
66         ])->findOrFail($id);
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 = Bookshelf::visible()->findOrFail($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($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 = Bookshelf::visible()->findOrFail($id);
103         $this->checkOwnablePermission('bookshelf-delete', $shelf);
104
105         $this->bookshelfRepo->destroy($shelf);
106
107         return response('', 204);
108     }
109
110     protected function rules(): array
111     {
112         return [
113             'create' => [
114                 'name'        => ['required', 'string', 'max:255'],
115                 'description' => ['string', 'max:1000'],
116                 'books'       => ['array'],
117                 'tags'        => ['array'],
118                 'image'       => array_merge(['nullable'], $this->getImageValidationRules()),
119             ],
120             'update' => [
121                 'name'        => ['string', 'min:1', 'max:255'],
122                 'description' => ['string', 'max:1000'],
123                 'books'       => ['array'],
124                 'tags'        => ['array'],
125                 'image'       => array_merge(['nullable'], $this->getImageValidationRules()),
126             ],
127         ];
128     }
129 }