3 namespace BookStack\Http\Controllers\Api;
5 use BookStack\Entities\Models\Bookshelf;
6 use BookStack\Entities\Repos\BookshelfRepo;
8 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
9 use Illuminate\Http\Request;
10 use Illuminate\Validation\ValidationException;
12 class BookshelfApiController extends ApiController
14 protected BookshelfRepo $bookshelfRepo;
18 'name' => ['required', 'string', 'max:255'],
19 'description' => ['string', 'max:1000'],
24 'name' => ['string', 'min:1', 'max:255'],
25 'description' => ['string', 'max:1000'],
32 * BookshelfApiController constructor.
34 public function __construct(BookshelfRepo $bookshelfRepo)
36 $this->bookshelfRepo = $bookshelfRepo;
40 * Get a listing of shelves visible to the user.
42 public function list()
44 $shelves = Bookshelf::visible();
46 return $this->apiListingResponse($shelves, [
47 'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by', 'image_id',
52 * Create a new shelf in the system.
53 * An array of books IDs can be provided in the request. These
54 * will be added to the shelf in the same order as provided.
56 * @throws ValidationException
58 public function create(Request $request)
60 $this->checkPermission('bookshelf-create-all');
61 $requestData = $this->validate($request, $this->rules['create']);
63 $bookIds = $request->get('books', []);
64 $shelf = $this->bookshelfRepo->create($requestData, $bookIds);
66 return response()->json($shelf);
70 * View the details of a single shelf.
72 public function read(string $id)
74 $shelf = Bookshelf::visible()->with([
75 'tags', 'cover', 'createdBy', 'updatedBy', 'ownedBy',
76 'books' => function (BelongsToMany $query) {
77 $query->scopes('visible')->get(['id', 'name', 'slug']);
81 return response()->json($shelf);
85 * Update the details of a single shelf.
86 * An array of books IDs can be provided in the request. These
87 * will be added to the shelf in the same order as provided and overwrite
88 * any existing book assignments.
90 * @throws ValidationException
92 public function update(Request $request, string $id)
94 $shelf = Bookshelf::visible()->findOrFail($id);
95 $this->checkOwnablePermission('bookshelf-update', $shelf);
97 $requestData = $this->validate($request, $this->rules['update']);
98 $bookIds = $request->get('books', null);
100 $shelf = $this->bookshelfRepo->update($shelf, $requestData, $bookIds);
102 return response()->json($shelf);
106 * Delete a single shelf.
107 * This will typically send the shelf to the recycle bin.
111 public function delete(string $id)
113 $shelf = Bookshelf::visible()->findOrFail($id);
114 $this->checkOwnablePermission('bookshelf-delete', $shelf);
116 $this->bookshelfRepo->destroy($shelf);
118 return response('', 204);