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
16 'name' => 'required|string|max:255',
17 'description' => 'string|max:1000',
21 'name' => 'string|min:1|max:255',
22 'description' => 'string|max:1000',
27 public function __construct(BookRepo $bookRepo)
29 $this->bookRepo = $bookRepo;
33 * Get a listing of books visible to the user.
35 public function list()
37 $books = Book::visible();
39 return $this->apiListingResponse($books, [
40 'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by', 'image_id',
45 * Create a new book in the system.
47 * @throws ValidationException
49 public function create(Request $request)
51 $this->checkPermission('book-create-all');
52 $requestData = $this->validate($request, $this->rules['create']);
54 $book = $this->bookRepo->create($requestData);
56 return response()->json($book);
60 * View the details of a single book.
62 public function read(string $id)
64 $book = Book::visible()->with(['tags', 'cover', 'createdBy', 'updatedBy', 'ownedBy'])->findOrFail($id);
66 return response()->json($book);
70 * Update the details of a single book.
72 * @throws ValidationException
74 public function update(Request $request, string $id)
76 $book = Book::visible()->findOrFail($id);
77 $this->checkOwnablePermission('book-update', $book);
79 $requestData = $this->validate($request, $this->rules['update']);
80 $book = $this->bookRepo->update($book, $requestData);
82 return response()->json($book);
86 * Delete a single book.
87 * This will typically send the book to the recycle bin.
91 public function delete(string $id)
93 $book = Book::visible()->findOrFail($id);
94 $this->checkOwnablePermission('book-delete', $book);
96 $this->bookRepo->destroy($book);
98 return response('', 204);