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