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;
17 * BookshelfApiController constructor.
19 public function __construct(BookshelfRepo $bookshelfRepo)
21 $this->bookshelfRepo = $bookshelfRepo;
25 * Get a listing of shelves visible to the user.
27 public function list()
29 $shelves = Bookshelf::visible();
31 return $this->apiListingResponse($shelves, [
32 'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
37 * Create a new shelf in the system.
38 * An array of books IDs can be provided in the request. These
39 * will be added to the shelf in the same order as provided.
40 * The cover image of a shelf can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
41 * If the 'image' property is null then the shelf cover image will be removed.
43 * @throws ValidationException
45 public function create(Request $request)
47 $this->checkPermission('bookshelf-create-all');
48 $requestData = $this->validate($request, $this->rules()['create']);
50 $bookIds = $request->get('books', []);
51 $shelf = $this->bookshelfRepo->create($requestData, $bookIds);
53 return response()->json($shelf);
57 * View the details of a single shelf.
59 public function read(string $id)
61 $shelf = Bookshelf::visible()->with([
62 'tags', 'cover', 'createdBy', 'updatedBy', 'ownedBy',
63 'books' => function (BelongsToMany $query) {
64 $query->scopes('visible')->get(['id', 'name', 'slug']);
68 return response()->json($shelf);
72 * Update the details of a single shelf.
73 * An array of books IDs can be provided in the request. These
74 * will be added to the shelf in the same order as provided and overwrite
75 * any existing book assignments.
76 * The cover image of a shelf can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
77 * If the 'image' property is null then the shelf cover image will be removed.
79 * @throws ValidationException
81 public function update(Request $request, string $id)
83 $shelf = Bookshelf::visible()->findOrFail($id);
84 $this->checkOwnablePermission('bookshelf-update', $shelf);
86 $requestData = $this->validate($request, $this->rules()['update']);
87 $bookIds = $request->get('books', null);
89 $shelf = $this->bookshelfRepo->update($shelf, $requestData, $bookIds);
91 return response()->json($shelf);
95 * Delete a single shelf.
96 * This will typically send the shelf to the recycle bin.
100 public function delete(string $id)
102 $shelf = Bookshelf::visible()->findOrFail($id);
103 $this->checkOwnablePermission('bookshelf-delete', $shelf);
105 $this->bookshelfRepo->destroy($shelf);
107 return response('', 204);
110 protected function rules(): array
114 'name' => ['required', 'string', 'max:255'],
115 'description' => ['string', 'max:1000'],
116 'books' => ['array'],
118 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
121 'name' => ['string', 'min:1', 'max:255'],
122 'description' => ['string', 'max:1000'],
123 'books' => ['array'],
125 'image' => array_merge(['nullable'], $this->getImageValidationRules()),