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
17 protected $bookshelfRepo;
21 'name' => ['required', 'string', 'max:255'],
22 'description' => ['string', 'max:1000'],
26 'name' => ['string', 'min:1', 'max:255'],
27 'description' => ['string', 'max:1000'],
33 * BookshelfApiController constructor.
35 public function __construct(BookshelfRepo $bookshelfRepo)
37 $this->bookshelfRepo = $bookshelfRepo;
41 * Get a listing of shelves visible to the user.
43 public function list()
45 $shelves = Bookshelf::visible();
47 return $this->apiListingResponse($shelves, [
48 'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by', 'image_id',
53 * Create a new shelf in the system.
54 * An array of books IDs can be provided in the request. These
55 * will be added to the shelf in the same order as provided.
57 * @throws ValidationException
59 public function create(Request $request)
61 $this->checkPermission('bookshelf-create-all');
62 $requestData = $this->validate($request, $this->rules['create']);
64 $bookIds = $request->get('books', []);
65 $shelf = $this->bookshelfRepo->create($requestData, $bookIds);
67 return response()->json($shelf);
71 * View the details of a single shelf.
73 public function read(string $id)
75 $shelf = Bookshelf::visible()->with([
76 'tags', 'cover', 'createdBy', 'updatedBy', 'ownedBy',
77 'books' => function (BelongsToMany $query) {
78 $query->scopes('visible')->get(['id', 'name', 'slug']);
82 return response()->json($shelf);
86 * Update the details of a single shelf.
87 * An array of books IDs can be provided in the request. These
88 * will be added to the shelf in the same order as provided and overwrite
89 * any existing book assignments.
91 * @throws ValidationException
93 public function update(Request $request, string $id)
95 $shelf = Bookshelf::visible()->findOrFail($id);
96 $this->checkOwnablePermission('bookshelf-update', $shelf);
98 $requestData = $this->validate($request, $this->rules['update']);
99 $bookIds = $request->get('books', null);
101 $shelf = $this->bookshelfRepo->update($shelf, $requestData, $bookIds);
103 return response()->json($shelf);
107 * Delete a single shelf.
108 * This will typically send the shelf to the recycle bin.
112 public function delete(string $id)
114 $shelf = Bookshelf::visible()->findOrFail($id);
115 $this->checkOwnablePermission('bookshelf-delete', $shelf);
117 $this->bookshelfRepo->destroy($shelf);
119 return response('', 204);