1 <?php namespace BookStack\Http\Controllers\Api;
3 use BookStack\Entities\Repos\BookshelfRepo;
4 use BookStack\Entities\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.
33 * @param BookshelfRepo $bookshelfRepo
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();
46 return $this->apiListingResponse($shelves, [
47 'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_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.
55 * @throws ValidationException
57 public function create(Request $request)
59 $this->checkPermission('bookshelf-create-all');
60 $requestData = $this->validate($request, $this->rules['create']);
62 $bookIds = $request->get('books', []);
63 $shelf = $this->bookshelfRepo->create($requestData, $bookIds);
65 return response()->json($shelf);
69 * View the details of a single shelf.
71 public function read(string $id)
73 $shelf = Bookshelf::visible()->with([
74 'tags', 'cover', 'createdBy', 'updatedBy',
75 'books' => function (BelongsToMany $query) {
76 $query->visible()->get(['id', 'name', 'slug']);
79 return response()->json($shelf);
83 * Update the details of a single shelf.
84 * An array of books IDs can be provided in the request. These
85 * will be added to the shelf in the same order as provided and overwrite
86 * any existing book assignments.
87 * @throws ValidationException
89 public function update(Request $request, string $id)
91 $shelf = Bookshelf::visible()->findOrFail($id);
92 $this->checkOwnablePermission('bookshelf-update', $shelf);
94 $requestData = $this->validate($request, $this->rules['update']);
95 $bookIds = $request->get('books', null);
97 $shelf = $this->bookshelfRepo->update($shelf, $requestData, $bookIds);
98 return response()->json($shelf);
104 * Delete a single shelf from the system.
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);