1 <?php namespace BookStack\Http\Controllers\Api;
3 use BookStack\Facades\Activity;
4 use BookStack\Entities\Repos\BookshelfRepo;
5 use BookStack\Entities\Bookshelf;
7 use Illuminate\Http\Request;
8 use Illuminate\Validation\ValidationException;
11 class BookshelfApiController extends ApiController
17 protected $bookshelfRepo;
21 'name' => 'required|string|max:255',
22 'description' => 'string|max:1000',
25 'name' => 'string|min:1|max:255',
26 'description' => 'string|max:1000',
31 * BookshelfApiController constructor.
32 * @param BookshelfRepo $bookshelfRepo
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 * @throws ValidationException
54 public function create(Request $request)
56 $this->checkPermission('bookshelf-create-all');
57 $requestData = $this->validate($request, $this->rules['create']);
59 $bookIds = $request->get('books', []);
61 $shelf = $this->bookshelfRepo->create($requestData,$bookIds);
62 Activity::add($shelf, 'bookshelf_create', $shelf->id);
64 return response()->json($shelf);
68 * View the details of a single shelf.
70 public function read(string $id)
72 $shelf = Bookshelf::visible()->with(['tags', 'cover', 'createdBy', 'updatedBy'])->findOrFail($id);
73 return response()->json($shelf);
77 * Update the details of a single shelf.
78 * @throws ValidationException
80 public function update(Request $request, string $id)
82 $shelf = Bookshelf::visible()->findOrFail($id);
83 $this->checkOwnablePermission('bookshelf-update', $shelf);
85 $requestData = $this->validate($request, $this->rules['update']);
87 $bookIds = $request->get('books', []);
89 $shelf = $this->bookshelfRepo->update($shelf, $requestData,$bookIds);
90 Activity::add($shelf, 'bookshelf_update', $shelf->id);
92 return response()->json($shelf);
98 * Delete a single shelf from the system.
100 * @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
103 public function delete(string $id)
105 $shelf = Bookshelf::visible()->findOrFail($id);
106 $this->checkOwnablePermission('bookshelf-delete', $shelf);
108 $this->bookshelfRepo->destroy($shelf);
109 Activity::addMessage('bookshelf-delete', $shelf->name);
111 return response('', 204);