1 <?php namespace BookStack\Http\Controllers\Api;
3 use BookStack\Entities\Repos\BookshelfRepo;
4 use BookStack\Entities\Models\Bookshelf;
6 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
7 use Illuminate\Http\Request;
8 use Illuminate\Validation\ValidationException;
10 class BookshelfApiController extends ApiController
16 protected $bookshelfRepo;
20 'name' => 'required|string|max:255',
21 'description' => 'string|max:1000',
25 'name' => 'string|min:1|max:255',
26 '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();
45 return $this->apiListingResponse($shelves, [
46 'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'image_id',
51 * Create a new shelf in the system.
52 * An array of books IDs can be provided in the request. These
53 * will be added to the shelf in the same order as provided.
54 * @throws ValidationException
56 public function create(Request $request)
58 $this->checkPermission('bookshelf-create-all');
59 $requestData = $this->validate($request, $this->rules['create']);
61 $bookIds = $request->get('books', []);
62 $shelf = $this->bookshelfRepo->create($requestData, $bookIds);
64 return response()->json($shelf);
68 * View the details of a single shelf.
70 public function read(string $id)
72 $shelf = Bookshelf::visible()->with([
73 'tags', 'cover', 'createdBy', 'updatedBy',
74 'books' => function (BelongsToMany $query) {
75 $query->visible()->get(['id', 'name', 'slug']);
78 return response()->json($shelf);
82 * Update the details of a single shelf.
83 * An array of books IDs can be provided in the request. These
84 * will be added to the shelf in the same order as provided and overwrite
85 * any existing book assignments.
86 * @throws ValidationException
88 public function update(Request $request, string $id)
90 $shelf = Bookshelf::visible()->findOrFail($id);
91 $this->checkOwnablePermission('bookshelf-update', $shelf);
93 $requestData = $this->validate($request, $this->rules['update']);
94 $bookIds = $request->get('books', null);
96 $shelf = $this->bookshelfRepo->update($shelf, $requestData, $bookIds);
97 return response()->json($shelf);
103 * Delete a single shelf.
104 * This will typically send the shelf to the recycle bin.
107 public function delete(string $id)
109 $shelf = Bookshelf::visible()->findOrFail($id);
110 $this->checkOwnablePermission('bookshelf-delete', $shelf);
112 $this->bookshelfRepo->destroy($shelf);
113 return response('', 204);