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;
9 use BookStack\Permissions\Permission;
11 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
12 use Illuminate\Http\Request;
13 use Illuminate\Validation\ValidationException;
15 class BookshelfApiController extends ApiController
17 public function __construct(
18 protected BookshelfRepo $bookshelfRepo,
19 protected BookshelfQueries $queries,
24 * Get a listing of shelves visible to the user.
26 public function list()
28 $shelves = $this->queries
30 ->with(['cover:id,name,url'])
31 ->addSelect(['created_by', 'updated_by']);
33 return $this->apiListingResponse($shelves, [
34 'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
39 * Create a new shelf in the system.
40 * An array of books IDs can be provided in the request. These
41 * will be added to the shelf in the same order as provided.
42 * The cover image of a shelf can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
43 * If the 'image' property is null then the shelf cover image will be removed.
45 * @throws ValidationException
47 public function create(Request $request)
49 $this->checkPermission(Permission::BookshelfCreateAll);
50 $requestData = $this->validate($request, $this->rules()['create']);
52 $bookIds = $request->get('books', []);
53 $shelf = $this->bookshelfRepo->create($requestData, $bookIds);
55 return response()->json($this->forJsonDisplay($shelf));
59 * View the details of a single shelf.
61 public function read(string $id)
63 $shelf = $this->queries->findVisibleByIdOrFail(intval($id));
64 $shelf = $this->forJsonDisplay($shelf);
66 'createdBy', 'updatedBy', 'ownedBy',
67 'books' => function (BelongsToMany $query) {
68 $query->scopes('visible')->get(['id', 'name', 'slug']);
72 return response()->json($shelf);
76 * Update the details of a single shelf.
77 * An array of books IDs can be provided in the request. These
78 * will be added to the shelf in the same order as provided and overwrite
79 * any existing book assignments.
80 * The cover image of a shelf can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
81 * If the 'image' property is null then the shelf cover image will be removed.
83 * @throws ValidationException
85 public function update(Request $request, string $id)
87 $shelf = $this->queries->findVisibleByIdOrFail(intval($id));
88 $this->checkOwnablePermission(Permission::BookshelfUpdate, $shelf);
90 $requestData = $this->validate($request, $this->rules()['update']);
91 $bookIds = $request->get('books', null);
93 $shelf = $this->bookshelfRepo->update($shelf, $requestData, $bookIds);
95 return response()->json($this->forJsonDisplay($shelf));
99 * Delete a single shelf.
100 * This will typically send the shelf to the recycle bin.
104 public function delete(string $id)
106 $shelf = $this->queries->findVisibleByIdOrFail(intval($id));
107 $this->checkOwnablePermission(Permission::BookshelfDelete, $shelf);
109 $this->bookshelfRepo->destroy($shelf);
111 return response('', 204);
114 protected function forJsonDisplay(Bookshelf $shelf): Bookshelf
116 $shelf = clone $shelf;
117 $shelf->unsetRelations()->refresh();
119 $shelf->load(['tags', 'cover']);
120 $shelf->makeVisible('description_html')
121 ->setAttribute('description_html', $shelf->descriptionHtml());
126 protected function rules(): array
130 'name' => ['required', 'string', 'max:255'],
131 'description' => ['string', 'max:1900'],
132 'description_html' => ['string', 'max:2000'],
133 'books' => ['array'],
135 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
138 'name' => ['string', 'min:1', 'max:255'],
139 'description' => ['string', 'max:1900'],
140 'description_html' => ['string', 'max:2000'],
141 'books' => ['array'],
143 'image' => array_merge(['nullable'], $this->getImageValidationRules()),