1 <?php namespace BookStack\Http\Controllers\Api;
3 use BookStack\Entities\Book;
4 use BookStack\Entities\Repos\BookRepo;
5 use BookStack\Facades\Activity;
6 use Illuminate\Http\Request;
8 class BooksApiController extends ApiController
15 'name' => 'required|string|max:255',
16 'description' => 'string|max:1000',
19 'name' => 'string|min:1|max:255',
20 'description' => 'string|max:1000',
25 * BooksApiController constructor.
27 public function __construct(BookRepo $bookRepo)
29 $this->bookRepo = $bookRepo;
33 * Get a listing of books visible to the user.
35 public function index()
37 $books = Book::visible();
38 return $this->apiListingResponse($books, [
39 'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'image_id',
45 * @throws \Illuminate\Validation\ValidationException
47 public function create(Request $request)
49 $this->checkPermission('book-create-all');
50 $requestData = $this->validate($request, $this->rules['create']);
52 $book = $this->bookRepo->create($requestData);
53 Activity::add($book, 'book_create', $book->id);
55 return response()->json($book);
59 * View the details of a single book.
61 public function read(string $id)
63 $book = Book::visible()->with(['tags', 'cover', 'createdBy', 'updatedBy'])->findOrFail($id);
64 return response()->json($book);
68 * Update the details of a single book.
69 * @throws \Illuminate\Validation\ValidationException
71 public function update(Request $request, string $id)
73 $book = Book::visible()->findOrFail($id);
74 $this->checkOwnablePermission('book-update', $book);
76 $requestData = $this->validate($request, $this->rules['update']);
77 $book = $this->bookRepo->update($book, $requestData);
78 Activity::add($book, 'book_update', $book->id);
80 return response()->json($book);
84 * Delete a book from the system.
85 * @throws \BookStack\Exceptions\NotifyException
86 * @throws \Illuminate\Contracts\Container\BindingResolutionException
88 public function delete(string $id)
90 $book = Book::visible()->findOrFail($id);
91 $this->checkOwnablePermission('book-delete', $book);
93 $this->bookRepo->destroy($book);
94 Activity::addMessage('book_delete', $book->name);
96 return response('', 204);