1 <?php namespace BookStack\Http\Controllers\Api;
3 use BookStack\Entities\Book;
4 use BookStack\Entities\Repos\BookRepo;
5 use BookStack\Exceptions\NotifyException;
6 use BookStack\Facades\Activity;
7 use Illuminate\Contracts\Container\BindingResolutionException;
8 use Illuminate\Http\Request;
9 use Illuminate\Validation\ValidationException;
11 class BookApiController extends ApiController
18 'name' => 'required|string|max:255',
19 'description' => 'string|max:1000',
23 'name' => 'string|min:1|max:255',
24 'description' => 'string|max:1000',
30 * BooksApiController constructor.
32 public function __construct(BookRepo $bookRepo)
34 $this->bookRepo = $bookRepo;
38 * Get a listing of books visible to the user.
40 public function list()
42 $books = Book::visible();
43 return $this->apiListingResponse($books, [
44 'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'image_id',
49 * Create a new book in the system.
50 * @throws ValidationException
52 public function create(Request $request)
54 $this->checkPermission('book-create-all');
55 $requestData = $this->validate($request, $this->rules['create']);
57 $book = $this->bookRepo->create($requestData);
58 Activity::add($book, 'book_create', $book->id);
60 return response()->json($book);
64 * View the details of a single book.
66 public function read(string $id)
68 $book = Book::visible()->with(['tags', 'cover', 'createdBy', 'updatedBy'])->findOrFail($id);
69 return response()->json($book);
73 * Update the details of a single book.
74 * @throws ValidationException
76 public function update(Request $request, string $id)
78 $book = Book::visible()->findOrFail($id);
79 $this->checkOwnablePermission('book-update', $book);
81 $requestData = $this->validate($request, $this->rules['update']);
82 $book = $this->bookRepo->update($book, $requestData);
83 Activity::add($book, 'book_update', $book->id);
85 return response()->json($book);
89 * Delete a single book from the system.
90 * @throws NotifyException
91 * @throws BindingResolutionException
93 public function delete(string $id)
95 $book = Book::visible()->findOrFail($id);
96 $this->checkOwnablePermission('book-delete', $book);
98 $this->bookRepo->destroy($book);
99 Activity::addMessage('book_delete', $book->name);
101 return response('', 204);