]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/BookshelfApiController.php
Merge branch 'footer-links' of git://github.com/james-geiger/BookStack into james...
[bookstack] / app / Http / Controllers / Api / BookshelfApiController.php
1 <?php namespace BookStack\Http\Controllers\Api;
2
3 use BookStack\Entities\Repos\BookshelfRepo;
4 use BookStack\Entities\Models\Bookshelf;
5 use Exception;
6 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
7 use Illuminate\Http\Request;
8 use Illuminate\Validation\ValidationException;
9
10 class BookshelfApiController extends ApiController
11 {
12
13     /**
14      * @var BookshelfRepo
15      */
16     protected $bookshelfRepo;
17
18     protected $rules = [
19         'create' => [
20             'name' => 'required|string|max:255',
21             'description' => 'string|max:1000',
22             'books' => 'array',
23         ],
24         'update' => [
25             'name' => 'string|min:1|max:255',
26             'description' => 'string|max:1000',
27             'books' => '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         return $this->apiListingResponse($shelves, [
46             'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by', 'image_id',
47         ]);
48     }
49
50     /**
51      * Create a new shelf in the system.
52      * An array of books IDs can be provided in the request. These
53      * will be added to the shelf in the same order as provided.
54      * @throws ValidationException
55      */
56     public function create(Request $request)
57     {
58         $this->checkPermission('bookshelf-create-all');
59         $requestData = $this->validate($request, $this->rules['create']);
60
61         $bookIds = $request->get('books', []);
62         $shelf = $this->bookshelfRepo->create($requestData, $bookIds);
63
64         return response()->json($shelf);
65     }
66
67     /**
68      * View the details of a single shelf.
69      */
70     public function read(string $id)
71     {
72         $shelf = Bookshelf::visible()->with([
73             'tags', 'cover', 'createdBy', 'updatedBy', 'ownedBy',
74             'books' => function (BelongsToMany $query) {
75                 $query->visible()->get(['id', 'name', 'slug']);
76             }
77         ])->findOrFail($id);
78         return response()->json($shelf);
79     }
80
81     /**
82      * Update the details of a single shelf.
83      * An array of books IDs can be provided in the request. These
84      * will be added to the shelf in the same order as provided and overwrite
85      * any existing book assignments.
86      * @throws ValidationException
87      */
88     public function update(Request $request, string $id)
89     {
90         $shelf = Bookshelf::visible()->findOrFail($id);
91         $this->checkOwnablePermission('bookshelf-update', $shelf);
92
93         $requestData = $this->validate($request, $this->rules['update']);
94         $bookIds = $request->get('books', null);
95
96         $shelf = $this->bookshelfRepo->update($shelf, $requestData, $bookIds);
97         return response()->json($shelf);
98     }
99
100
101
102     /**
103      * Delete a single shelf.
104      * This will typically send the shelf to the recycle bin.
105      * @throws Exception
106      */
107     public function delete(string $id)
108     {
109         $shelf = Bookshelf::visible()->findOrFail($id);
110         $this->checkOwnablePermission('bookshelf-delete', $shelf);
111
112         $this->bookshelfRepo->destroy($shelf);
113         return response('', 204);
114     }
115 }