]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/BookshelfApiController.php
Started work on hierachy conversion actions
[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      *
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($shelf);
52     }
53
54     /**
55      * View the details of a single shelf.
56      */
57     public function read(string $id)
58     {
59         $shelf = Bookshelf::visible()->with([
60             'tags', 'cover', 'createdBy', 'updatedBy', 'ownedBy',
61             'books' => function (BelongsToMany $query) {
62                 $query->scopes('visible')->get(['id', 'name', 'slug']);
63             },
64         ])->findOrFail($id);
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      *
75      * @throws ValidationException
76      */
77     public function update(Request $request, string $id)
78     {
79         $shelf = Bookshelf::visible()->findOrFail($id);
80         $this->checkOwnablePermission('bookshelf-update', $shelf);
81
82         $requestData = $this->validate($request, $this->rules['update']);
83         $bookIds = $request->get('books', null);
84
85         $shelf = $this->bookshelfRepo->update($shelf, $requestData, $bookIds);
86
87         return response()->json($shelf);
88     }
89
90     /**
91      * Delete a single shelf.
92      * This will typically send the shelf to the recycle bin.
93      *
94      * @throws Exception
95      */
96     public function delete(string $id)
97     {
98         $shelf = Bookshelf::visible()->findOrFail($id);
99         $this->checkOwnablePermission('bookshelf-delete', $shelf);
100
101         $this->bookshelfRepo->destroy($shelf);
102
103         return response('', 204);
104     }
105
106     protected function rules(): array
107     {
108         return [
109             'create' => [
110                 'name'        => ['required', 'string', 'max:255'],
111                 'description' => ['string', 'max:1000'],
112                 'books'       => ['array'],
113                 'tags'        => ['array'],
114                 'image'       => array_merge(['nullable'], $this->getImageValidationRules()),
115             ],
116             'update' => [
117                 'name'        => ['string', 'min:1', 'max:255'],
118                 'description' => ['string', 'max:1000'],
119                 'books'       => ['array'],
120                 'tags'        => ['array'],
121                 'image'       => array_merge(['nullable'], $this->getImageValidationRules()),
122             ],
123         ];
124     }
125 }