]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/BookshelfApiController.php
Merge pull request #2360 from BookStackApp/activity_revamp
[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\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      * @param BookshelfRepo $bookshelfRepo
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         return $this->apiListingResponse($shelves, [
47             'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_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      * @throws ValidationException
56      */
57     public function create(Request $request)
58     {
59         $this->checkPermission('bookshelf-create-all');
60         $requestData = $this->validate($request, $this->rules['create']);
61
62         $bookIds = $request->get('books', []);
63         $shelf = $this->bookshelfRepo->create($requestData, $bookIds);
64
65         return response()->json($shelf);
66     }
67
68     /**
69      * View the details of a single shelf.
70      */
71     public function read(string $id)
72     {
73         $shelf = Bookshelf::visible()->with([
74             'tags', 'cover', 'createdBy', 'updatedBy',
75             'books' => function (BelongsToMany $query) {
76                 $query->visible()->get(['id', 'name', 'slug']);
77             }
78         ])->findOrFail($id);
79         return response()->json($shelf);
80     }
81
82     /**
83      * Update the details of a single shelf.
84      * An array of books IDs can be provided in the request. These
85      * will be added to the shelf in the same order as provided and overwrite
86      * any existing book assignments.
87      * @throws ValidationException
88      */
89     public function update(Request $request, string $id)
90     {
91         $shelf = Bookshelf::visible()->findOrFail($id);
92         $this->checkOwnablePermission('bookshelf-update', $shelf);
93
94         $requestData = $this->validate($request, $this->rules['update']);
95         $bookIds = $request->get('books', null);
96
97         $shelf = $this->bookshelfRepo->update($shelf, $requestData, $bookIds);
98         return response()->json($shelf);
99     }
100
101
102
103     /**
104      * Delete a single shelf from the system.
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 }