]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/BookshelfApiController.php
Organised activity types and moved most to repos
[bookstack] / app / Http / Controllers / Api / BookshelfApiController.php
1 <?php namespace BookStack\Http\Controllers\Api;
2
3 use BookStack\Actions\ActivityType;
4 use BookStack\Facades\Activity;
5 use BookStack\Entities\Repos\BookshelfRepo;
6 use BookStack\Entities\Bookshelf;
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     /**
16      * @var BookshelfRepo
17      */
18     protected $bookshelfRepo;
19
20     protected $rules = [
21         'create' => [
22             'name' => 'required|string|max:255',
23             'description' => 'string|max:1000',
24             'books' => 'array',
25         ],
26         'update' => [
27             'name' => 'string|min:1|max:255',
28             'description' => 'string|max:1000',
29             'books' => 'array',
30         ],
31     ];
32
33     /**
34      * BookshelfApiController constructor.
35      * @param BookshelfRepo $bookshelfRepo
36      */
37     public function __construct(BookshelfRepo $bookshelfRepo)
38     {
39         $this->bookshelfRepo = $bookshelfRepo;
40     }
41
42     /**
43      * Get a listing of shelves visible to the user.
44      */
45     public function list()
46     {
47         $shelves = Bookshelf::visible();
48         return $this->apiListingResponse($shelves, [
49             'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'image_id',
50         ]);
51     }
52
53     /**
54      * Create a new shelf in the system.
55      * An array of books IDs can be provided in the request. These
56      * will be added to the shelf in the same order as provided.
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',
77             'books' => function (BelongsToMany $query) {
78                 $query->visible()->get(['id', 'name', 'slug']);
79             }
80         ])->findOrFail($id);
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      * @throws ValidationException
90      */
91     public function update(Request $request, string $id)
92     {
93         $shelf = Bookshelf::visible()->findOrFail($id);
94         $this->checkOwnablePermission('bookshelf-update', $shelf);
95
96         $requestData = $this->validate($request, $this->rules['update']);
97         $bookIds = $request->get('books', null);
98
99         $shelf = $this->bookshelfRepo->update($shelf, $requestData, $bookIds);
100         return response()->json($shelf);
101     }
102
103
104
105     /**
106      * Delete a single shelf from the system.
107      * @throws Exception
108      */
109     public function delete(string $id)
110     {
111         $shelf = Bookshelf::visible()->findOrFail($id);
112         $this->checkOwnablePermission('bookshelf-delete', $shelf);
113
114         $this->bookshelfRepo->destroy($shelf);
115         return response('', 204);
116     }
117 }