1 <?php namespace BookStack\Http\Controllers\Api;
3 use BookStack\Entities\Models\Book;
4 use BookStack\Entities\Repos\BookRepo;
5 use BookStack\Exceptions\NotifyException;
6 use Illuminate\Contracts\Container\BindingResolutionException;
7 use Illuminate\Http\Request;
8 use Illuminate\Validation\ValidationException;
10 class BookApiController extends ApiController
17 'name' => 'required|string|max:255',
18 'description' => 'string|max:1000',
22 'name' => 'string|min:1|max:255',
23 'description' => 'string|max:1000',
28 public function __construct(BookRepo $bookRepo)
30 $this->bookRepo = $bookRepo;
34 * Get a listing of books visible to the user.
36 public function list()
38 $books = Book::visible();
39 return $this->apiListingResponse($books, [
40 'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'image_id',
45 * Create a new book in the system.
46 * @throws ValidationException
48 public function create(Request $request)
50 $this->checkPermission('book-create-all');
51 $requestData = $this->validate($request, $this->rules['create']);
53 $book = $this->bookRepo->create($requestData);
54 return response()->json($book);
58 * View the details of a single book.
60 public function read(string $id)
62 $book = Book::visible()->with(['tags', 'cover', 'createdBy', 'updatedBy'])->findOrFail($id);
63 return response()->json($book);
67 * Update the details of a single book.
68 * @throws ValidationException
70 public function update(Request $request, string $id)
72 $book = Book::visible()->findOrFail($id);
73 $this->checkOwnablePermission('book-update', $book);
75 $requestData = $this->validate($request, $this->rules['update']);
76 $book = $this->bookRepo->update($book, $requestData);
78 return response()->json($book);
82 * Delete a single book.
83 * This will typically send the book to the recycle bin.
86 public function delete(string $id)
88 $book = Book::visible()->findOrFail($id);
89 $this->checkOwnablePermission('book-delete', $book);
91 $this->bookRepo->destroy($book);
92 return response('', 204);