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 ->with(['cover:id,name,url'])
30 ->addSelect(['created_by', 'updated_by']);
32 return $this->apiListingResponse($shelves, [
33 'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
38 * Create a new shelf in the system.
39 * An array of books IDs can be provided in the request. These
40 * will be added to the shelf in the same order as provided.
41 * The cover image of a shelf can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
42 * If the 'image' property is null then the shelf cover image will be removed.
44 * @throws ValidationException
46 public function create(Request $request)
48 $this->checkPermission('bookshelf-create-all');
49 $requestData = $this->validate($request, $this->rules()['create']);
51 $bookIds = $request->get('books', []);
52 $shelf = $this->bookshelfRepo->create($requestData, $bookIds);
54 return response()->json($this->forJsonDisplay($shelf));
58 * View the details of a single shelf.
60 public function read(string $id)
62 $shelf = $this->queries->findVisibleByIdOrFail(intval($id));
63 $shelf = $this->forJsonDisplay($shelf);
65 'createdBy', 'updatedBy', 'ownedBy',
66 'books' => function (BelongsToMany $query) {
67 $query->scopes('visible')->get(['id', 'name', 'slug']);
71 return response()->json($shelf);
75 * Update the details of a single shelf.
76 * An array of books IDs can be provided in the request. These
77 * will be added to the shelf in the same order as provided and overwrite
78 * any existing book assignments.
79 * The cover image of a shelf can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
80 * If the 'image' property is null then the shelf cover image will be removed.
82 * @throws ValidationException
84 public function update(Request $request, string $id)
86 $shelf = $this->queries->findVisibleByIdOrFail(intval($id));
87 $this->checkOwnablePermission('bookshelf-update', $shelf);
89 $requestData = $this->validate($request, $this->rules()['update']);
90 $bookIds = $request->get('books', null);
92 $shelf = $this->bookshelfRepo->update($shelf, $requestData, $bookIds);
94 return response()->json($this->forJsonDisplay($shelf));
98 * Delete a single shelf.
99 * This will typically send the shelf to the recycle bin.
103 public function delete(string $id)
105 $shelf = $this->queries->findVisibleByIdOrFail(intval($id));
106 $this->checkOwnablePermission('bookshelf-delete', $shelf);
108 $this->bookshelfRepo->destroy($shelf);
110 return response('', 204);
113 protected function forJsonDisplay(Bookshelf $shelf): Bookshelf
115 $shelf = clone $shelf;
116 $shelf->unsetRelations()->refresh();
118 $shelf->load(['tags', 'cover']);
119 $shelf->makeVisible('description_html')
120 ->setAttribute('description_html', $shelf->descriptionHtml());
125 protected function rules(): array
129 'name' => ['required', 'string', 'max:255'],
130 'description' => ['string', 'max:1900'],
131 'description_html' => ['string', 'max:2000'],
132 'books' => ['array'],
134 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
137 'name' => ['string', 'min:1', 'max:255'],
138 'description' => ['string', 'max:1900'],
139 'description_html' => ['string', 'max:2000'],
140 'books' => ['array'],
142 'image' => array_merge(['nullable'], $this->getImageValidationRules()),