]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/BookshelfApiController.php
Replaced embeds with images in exports
[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     protected $rules = [
17         'create' => [
18             'name'        => ['required', 'string', 'max:255'],
19             'description' => ['string', 'max:1000'],
20             'books'       => ['array'],
21             'tags'        => ['array'],
22         ],
23         'update' => [
24             'name'        => ['string', 'min:1', 'max:255'],
25             'description' => ['string', 'max:1000'],
26             'books'       => ['array'],
27             'tags'        => ['array'],
28         ],
29     ];
30
31     /**
32      * BookshelfApiController constructor.
33      */
34     public function __construct(BookshelfRepo $bookshelfRepo)
35     {
36         $this->bookshelfRepo = $bookshelfRepo;
37     }
38
39     /**
40      * Get a listing of shelves visible to the user.
41      */
42     public function list()
43     {
44         $shelves = Bookshelf::visible();
45
46         return $this->apiListingResponse($shelves, [
47             'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by', 'image_id',
48         ]);
49     }
50
51     /**
52      * Create a new shelf in the system.
53      * An array of books IDs can be provided in the request. These
54      * will be added to the shelf in the same order as provided.
55      *
56      * @throws ValidationException
57      */
58     public function create(Request $request)
59     {
60         $this->checkPermission('bookshelf-create-all');
61         $requestData = $this->validate($request, $this->rules['create']);
62
63         $bookIds = $request->get('books', []);
64         $shelf = $this->bookshelfRepo->create($requestData, $bookIds);
65
66         return response()->json($shelf);
67     }
68
69     /**
70      * View the details of a single shelf.
71      */
72     public function read(string $id)
73     {
74         $shelf = Bookshelf::visible()->with([
75             'tags', 'cover', 'createdBy', 'updatedBy', 'ownedBy',
76             'books' => function (BelongsToMany $query) {
77                 $query->scopes('visible')->get(['id', 'name', 'slug']);
78             },
79         ])->findOrFail($id);
80
81         return response()->json($shelf);
82     }
83
84     /**
85      * Update the details of a single shelf.
86      * An array of books IDs can be provided in the request. These
87      * will be added to the shelf in the same order as provided and overwrite
88      * any existing book assignments.
89      *
90      * @throws ValidationException
91      */
92     public function update(Request $request, string $id)
93     {
94         $shelf = Bookshelf::visible()->findOrFail($id);
95         $this->checkOwnablePermission('bookshelf-update', $shelf);
96
97         $requestData = $this->validate($request, $this->rules['update']);
98         $bookIds = $request->get('books', null);
99
100         $shelf = $this->bookshelfRepo->update($shelf, $requestData, $bookIds);
101
102         return response()->json($shelf);
103     }
104
105     /**
106      * Delete a single shelf.
107      * This will typically send the shelf to the recycle bin.
108      *
109      * @throws Exception
110      */
111     public function delete(string $id)
112     {
113         $shelf = Bookshelf::visible()->findOrFail($id);
114         $this->checkOwnablePermission('bookshelf-delete', $shelf);
115
116         $this->bookshelfRepo->destroy($shelf);
117
118         return response('', 204);
119     }
120 }