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 protected BookshelfRepo $bookshelfRepo;
17 public function __construct(BookshelfRepo $bookshelfRepo)
19 $this->bookshelfRepo = $bookshelfRepo;
23 * Get a listing of shelves visible to the user.
25 public function list()
27 $shelves = Bookshelf::visible();
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($shelf);
55 * View the details of a single shelf.
57 public function read(string $id)
59 $shelf = Bookshelf::visible()->with([
60 'tags', 'cover', '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($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 rules(): array
112 'name' => ['required', 'string', 'max:255'],
113 'description' => ['string', 'max:1000'],
114 'books' => ['array'],
116 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
119 'name' => ['string', 'min:1', 'max:255'],
120 'description' => ['string', 'max:1000'],
121 'books' => ['array'],
123 'image' => array_merge(['nullable'], $this->getImageValidationRules()),