3 namespace BookStack\Entities\Controllers;
5 use BookStack\Entities\Models\Bookshelf;
6 use BookStack\Entities\Repos\BookshelfRepo;
7 use BookStack\Http\ApiController;
9 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
10 use Illuminate\Http\Request;
11 use Illuminate\Validation\ValidationException;
13 class BookshelfApiController extends ApiController
15 public function __construct(
16 protected BookshelfRepo $bookshelfRepo
21 * Get a listing of shelves visible to the user.
23 public function list()
25 $shelves = Bookshelf::visible();
27 return $this->apiListingResponse($shelves, [
28 'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
33 * Create a new shelf in the system.
34 * An array of books IDs can be provided in the request. These
35 * will be added to the shelf in the same order as provided.
36 * The cover image of a shelf can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
37 * If the 'image' property is null then the shelf cover image will be removed.
39 * @throws ValidationException
41 public function create(Request $request)
43 $this->checkPermission('bookshelf-create-all');
44 $requestData = $this->validate($request, $this->rules()['create']);
46 $bookIds = $request->get('books', []);
47 $shelf = $this->bookshelfRepo->create($requestData, $bookIds);
49 return response()->json($this->forJsonDisplay($shelf));
53 * View the details of a single shelf.
55 public function read(string $id)
57 $shelf = Bookshelf::visible()->findOrFail($id);
58 $shelf = $this->forJsonDisplay($shelf);
60 'createdBy', 'updatedBy', 'ownedBy',
61 'books' => function (BelongsToMany $query) {
62 $query->scopes('visible')->get(['id', 'name', 'slug']);
66 return response()->json($shelf);
70 * Update the details of a single shelf.
71 * An array of books IDs can be provided in the request. These
72 * will be added to the shelf in the same order as provided and overwrite
73 * any existing book assignments.
74 * The cover image of a shelf can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
75 * If the 'image' property is null then the shelf cover image will be removed.
77 * @throws ValidationException
79 public function update(Request $request, string $id)
81 $shelf = Bookshelf::visible()->findOrFail($id);
82 $this->checkOwnablePermission('bookshelf-update', $shelf);
84 $requestData = $this->validate($request, $this->rules()['update']);
85 $bookIds = $request->get('books', null);
87 $shelf = $this->bookshelfRepo->update($shelf, $requestData, $bookIds);
89 return response()->json($this->forJsonDisplay($shelf));
93 * Delete a single shelf.
94 * This will typically send the shelf to the recycle bin.
98 public function delete(string $id)
100 $shelf = Bookshelf::visible()->findOrFail($id);
101 $this->checkOwnablePermission('bookshelf-delete', $shelf);
103 $this->bookshelfRepo->destroy($shelf);
105 return response('', 204);
108 protected function forJsonDisplay(Bookshelf $shelf): Bookshelf
110 $shelf = clone $shelf;
111 $shelf->unsetRelations()->refresh();
113 $shelf->load(['tags', 'cover']);
114 $shelf->makeVisible('description_html')
115 ->setAttribute('description_html', $shelf->descriptionHtml());
120 protected function rules(): array
124 'name' => ['required', 'string', 'max:255'],
125 'description' => ['string', 'max:1900'],
126 'description_html' => ['string', 'max:2000'],
127 'books' => ['array'],
129 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
132 'name' => ['string', 'min:1', 'max:255'],
133 'description' => ['string', 'max:1900'],
134 'description_html' => ['string', 'max:2000'],
135 'books' => ['array'],
137 'image' => array_merge(['nullable'], $this->getImageValidationRules()),