3 namespace BookStack\Entities\Controllers;
5 use BookStack\Entities\Models\Bookshelf;
6 use BookStack\Entities\Queries\BookshelfQueries;
7 use BookStack\Entities\Repos\BookshelfRepo;
8 use BookStack\Http\ApiController;
10 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
11 use Illuminate\Http\Request;
12 use Illuminate\Validation\ValidationException;
14 class BookshelfApiController extends ApiController
16 public function __construct(
17 protected BookshelfRepo $bookshelfRepo,
18 protected BookshelfQueries $queries,
23 * Get a listing of shelves visible to the user.
25 public function list()
27 $shelves = $this->queries
29 ->addSelect(['created_by', 'updated_by']);
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($this->forJsonDisplay($shelf));
57 * View the details of a single shelf.
59 public function read(string $id)
61 $shelf = $this->queries->findVisibleByIdOrFail(intval($id));
62 $shelf = $this->forJsonDisplay($shelf);
64 'createdBy', 'updatedBy', 'ownedBy',
65 'books' => function (BelongsToMany $query) {
66 $query->scopes('visible')->get(['id', 'name', 'slug']);
70 return response()->json($shelf);
74 * Update the details of a single shelf.
75 * An array of books IDs can be provided in the request. These
76 * will be added to the shelf in the same order as provided and overwrite
77 * any existing book assignments.
78 * The cover image of a shelf can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
79 * If the 'image' property is null then the shelf cover image will be removed.
81 * @throws ValidationException
83 public function update(Request $request, string $id)
85 $shelf = $this->queries->findVisibleByIdOrFail(intval($id));
86 $this->checkOwnablePermission('bookshelf-update', $shelf);
88 $requestData = $this->validate($request, $this->rules()['update']);
89 $bookIds = $request->get('books', null);
91 $shelf = $this->bookshelfRepo->update($shelf, $requestData, $bookIds);
93 return response()->json($this->forJsonDisplay($shelf));
97 * Delete a single shelf.
98 * This will typically send the shelf to the recycle bin.
102 public function delete(string $id)
104 $shelf = $this->queries->findVisibleByIdOrFail(intval($id));
105 $this->checkOwnablePermission('bookshelf-delete', $shelf);
107 $this->bookshelfRepo->destroy($shelf);
109 return response('', 204);
112 protected function forJsonDisplay(Bookshelf $shelf): Bookshelf
114 $shelf = clone $shelf;
115 $shelf->unsetRelations()->refresh();
117 $shelf->load(['tags', 'cover']);
118 $shelf->makeVisible('description_html')
119 ->setAttribute('description_html', $shelf->descriptionHtml());
124 protected function rules(): array
128 'name' => ['required', 'string', 'max:255'],
129 'description' => ['string', 'max:1900'],
130 'description_html' => ['string', 'max:2000'],
131 'books' => ['array'],
133 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
136 'name' => ['string', 'min:1', 'max:255'],
137 'description' => ['string', 'max:1900'],
138 'description_html' => ['string', 'max:2000'],
139 'books' => ['array'],
141 'image' => array_merge(['nullable'], $this->getImageValidationRules()),