3 namespace BookStack\Http\Controllers\Api;
5 use BookStack\Entities\Models\Bookshelf;
6 use BookStack\Entities\Repos\BookshelfRepo;
8 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
9 use Illuminate\Http\Request;
10 use Illuminate\Validation\ValidationException;
12 class BookshelfApiController extends ApiController
14 protected BookshelfRepo $bookshelfRepo;
16 public function __construct(BookshelfRepo $bookshelfRepo)
18 $this->bookshelfRepo = $bookshelfRepo;
22 * Get a listing of shelves visible to the user.
24 public function list()
26 $shelves = Bookshelf::visible();
28 return $this->apiListingResponse($shelves, [
29 'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
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.
40 * @throws ValidationException
42 public function create(Request $request)
44 $this->checkPermission('bookshelf-create-all');
45 $requestData = $this->validate($request, $this->rules()['create']);
47 $bookIds = $request->get('books', []);
48 $shelf = $this->bookshelfRepo->create($requestData, $bookIds);
50 return response()->json($shelf);
54 * View the details of a single shelf.
56 public function read(string $id)
58 $shelf = Bookshelf::visible()->with([
59 'tags', 'cover', 'createdBy', 'updatedBy', 'ownedBy',
60 'books' => function (BelongsToMany $query) {
61 $query->scopes('visible')->get(['id', 'name', 'slug']);
65 return response()->json($shelf);
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.
76 * @throws ValidationException
78 public function update(Request $request, string $id)
80 $shelf = Bookshelf::visible()->findOrFail($id);
81 $this->checkOwnablePermission('bookshelf-update', $shelf);
83 $requestData = $this->validate($request, $this->rules()['update']);
84 $bookIds = $request->get('books', null);
86 $shelf = $this->bookshelfRepo->update($shelf, $requestData, $bookIds);
88 return response()->json($shelf);
92 * Delete a single shelf.
93 * This will typically send the shelf to the recycle bin.
97 public function delete(string $id)
99 $shelf = Bookshelf::visible()->findOrFail($id);
100 $this->checkOwnablePermission('bookshelf-delete', $shelf);
102 $this->bookshelfRepo->destroy($shelf);
104 return response('', 204);
107 protected function rules(): array
111 'name' => ['required', 'string', 'max:255'],
112 'description' => ['string', 'max:1000'],
113 'books' => ['array'],
115 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
118 'name' => ['string', 'min:1', 'max:255'],
119 'description' => ['string', 'max:1000'],
120 'books' => ['array'],
122 'image' => array_merge(['nullable'], $this->getImageValidationRules()),