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->visibleForList();
29 return $this->apiListingResponse($shelves, [
30 'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
35 * Create a new shelf in the system.
36 * An array of books IDs can be provided in the request. These
37 * will be added to the shelf in the same order as provided.
38 * The cover image of a shelf can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
39 * If the 'image' property is null then the shelf cover image will be removed.
41 * @throws ValidationException
43 public function create(Request $request)
45 $this->checkPermission('bookshelf-create-all');
46 $requestData = $this->validate($request, $this->rules()['create']);
48 $bookIds = $request->get('books', []);
49 $shelf = $this->bookshelfRepo->create($requestData, $bookIds);
51 return response()->json($this->forJsonDisplay($shelf));
55 * View the details of a single shelf.
57 public function read(string $id)
59 $shelf = $this->queries->findVisibleByIdOrFail(intval($id));
60 $shelf = $this->forJsonDisplay($shelf);
62 '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 = $this->queries->findVisibleByIdOrFail(intval($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($this->forJsonDisplay($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 = $this->queries->findVisibleByIdOrFail(intval($id));
103 $this->checkOwnablePermission('bookshelf-delete', $shelf);
105 $this->bookshelfRepo->destroy($shelf);
107 return response('', 204);
110 protected function forJsonDisplay(Bookshelf $shelf): Bookshelf
112 $shelf = clone $shelf;
113 $shelf->unsetRelations()->refresh();
115 $shelf->load(['tags', 'cover']);
116 $shelf->makeVisible('description_html')
117 ->setAttribute('description_html', $shelf->descriptionHtml());
122 protected function rules(): array
126 'name' => ['required', 'string', 'max:255'],
127 'description' => ['string', 'max:1900'],
128 'description_html' => ['string', 'max:2000'],
129 'books' => ['array'],
131 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
134 'name' => ['string', 'min:1', 'max:255'],
135 'description' => ['string', 'max:1900'],
136 'description_html' => ['string', 'max:2000'],
137 'books' => ['array'],
139 'image' => array_merge(['nullable'], $this->getImageValidationRules()),