1 <?php namespace BookStack\Http\Controllers\Api;
3 use BookStack\Actions\ActivityType;
4 use BookStack\Facades\Activity;
5 use BookStack\Entities\Repos\BookshelfRepo;
6 use BookStack\Entities\Bookshelf;
8 use Illuminate\Database\Eloquent\Relations\BelongsToMany;
9 use Illuminate\Http\Request;
10 use Illuminate\Validation\ValidationException;
12 class BookshelfApiController extends ApiController
18 protected $bookshelfRepo;
22 'name' => 'required|string|max:255',
23 'description' => 'string|max:1000',
27 'name' => 'string|min:1|max:255',
28 'description' => 'string|max:1000',
34 * BookshelfApiController constructor.
35 * @param BookshelfRepo $bookshelfRepo
37 public function __construct(BookshelfRepo $bookshelfRepo)
39 $this->bookshelfRepo = $bookshelfRepo;
43 * Get a listing of shelves visible to the user.
45 public function list()
47 $shelves = Bookshelf::visible();
48 return $this->apiListingResponse($shelves, [
49 'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'image_id',
54 * Create a new shelf in the system.
55 * An array of books IDs can be provided in the request. These
56 * 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',
77 'books' => function (BelongsToMany $query) {
78 $query->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.
89 * @throws ValidationException
91 public function update(Request $request, string $id)
93 $shelf = Bookshelf::visible()->findOrFail($id);
94 $this->checkOwnablePermission('bookshelf-update', $shelf);
96 $requestData = $this->validate($request, $this->rules['update']);
97 $bookIds = $request->get('books', null);
99 $shelf = $this->bookshelfRepo->update($shelf, $requestData, $bookIds);
100 return response()->json($shelf);
106 * Delete a single shelf from the system.
109 public function delete(string $id)
111 $shelf = Bookshelf::visible()->findOrFail($id);
112 $this->checkOwnablePermission('bookshelf-delete', $shelf);
114 $this->bookshelfRepo->destroy($shelf);
115 return response('', 204);