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