3 namespace BookStack\Http\Controllers\Api;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Repos\BookRepo;
7 use Illuminate\Http\Request;
8 use Illuminate\Validation\ValidationException;
10 class BookApiController extends ApiController
14 public function __construct(BookRepo $bookRepo)
16 $this->bookRepo = $bookRepo;
20 * Get a listing of books visible to the user.
22 public function list()
24 $books = Book::visible();
26 return $this->apiListingResponse($books, [
27 'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
32 * Create a new book in the system.
33 * The cover image of a book can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
34 * If the 'image' property is null then the book cover image will be removed.
36 * @throws ValidationException
38 public function create(Request $request)
40 $this->checkPermission('book-create-all');
41 $requestData = $this->validate($request, $this->rules()['create']);
43 $book = $this->bookRepo->create($requestData);
45 return response()->json($book);
49 * View the details of a single book.
51 public function read(string $id)
53 $book = Book::visible()->with(['tags', 'cover', 'createdBy', 'updatedBy', 'ownedBy'])->findOrFail($id);
55 return response()->json($book);
59 * Update the details of a single book.
60 * The cover image of a book can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
61 * If the 'image' property is null then the book cover image will be removed.
63 * @throws ValidationException
65 public function update(Request $request, string $id)
67 $book = Book::visible()->findOrFail($id);
68 $this->checkOwnablePermission('book-update', $book);
70 $requestData = $this->validate($request, $this->rules()['update']);
71 $book = $this->bookRepo->update($book, $requestData);
73 return response()->json($book);
77 * Delete a single book.
78 * This will typically send the book to the recycle bin.
82 public function delete(string $id)
84 $book = Book::visible()->findOrFail($id);
85 $this->checkOwnablePermission('book-delete', $book);
87 $this->bookRepo->destroy($book);
89 return response('', 204);
92 protected function rules(): array
96 'name' => ['required', 'string', 'max:255'],
97 'description' => ['string', 'max:1000'],
99 'image' => array_merge(['nullable'], $this->getImageValidationRules()),
102 'name' => ['string', 'min:1', 'max:255'],
103 'description' => ['string', 'max:1000'],
105 'image' => array_merge(['nullable'], $this->getImageValidationRules()),