]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/BookshelfApiController.php
added api functionality to handle book Shelves
[bookstack] / app / Http / Controllers / Api / BookshelfApiController.php
1 <?php namespace BookStack\Http\Controllers\Api;
2
3 use BookStack\Facades\Activity;
4 use BookStack\Entities\Repos\BookshelfRepo;
5 use BookStack\Entities\Bookshelf;
6 use Exception;
7 use Illuminate\Http\Request;
8 use Illuminate\Validation\ValidationException;
9
10
11 class BookshelfApiController extends ApiController
12 {
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         ],
24         'update' => [
25             'name' => 'string|min:1|max:255',
26             'description' => 'string|max:1000',
27         ],
28     ];
29
30     /**
31      * BookshelfApiController constructor.
32      * @param BookshelfRepo $bookshelfRepo
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', 'image_id',
47         ]);
48     }
49
50     /**
51      * Create a new shelf in the system.
52      * @throws ValidationException
53      */
54     public function create(Request $request)
55     {
56         $this->checkPermission('bookshelf-create-all');
57         $requestData = $this->validate($request, $this->rules['create']);
58
59         $bookIds = $request->get('books', []);
60
61         $shelf = $this->bookshelfRepo->create($requestData,$bookIds);
62         Activity::add($shelf, 'bookshelf_create', $shelf->id);
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(['tags', 'cover', 'createdBy', 'updatedBy'])->findOrFail($id);
73         return response()->json($shelf);
74     }
75
76     /**
77      * Update the details of a single shelf.
78      * @throws ValidationException
79      */
80     public function update(Request $request, string $id)
81     {
82         $shelf = Bookshelf::visible()->findOrFail($id);
83         $this->checkOwnablePermission('bookshelf-update', $shelf);
84
85         $requestData = $this->validate($request, $this->rules['update']);
86
87         $bookIds = $request->get('books', []);
88
89         $shelf = $this->bookshelfRepo->update($shelf, $requestData,$bookIds);
90         Activity::add($shelf, 'bookshelf_update', $shelf->id);
91
92         return response()->json($shelf);
93     }
94
95
96
97     /**
98      * Delete a single shelf from the system.
99      * @param string $id
100      * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
101      * @throws Exception
102      */
103     public function delete(string $id)
104     {
105         $shelf = Bookshelf::visible()->findOrFail($id);
106         $this->checkOwnablePermission('bookshelf-delete', $shelf);
107
108         $this->bookshelfRepo->destroy($shelf);
109         Activity::addMessage('bookshelf-delete', $shelf->name);
110
111         return response('', 204);
112     }
113 }