1 <?php namespace BookStack\Http\Controllers\Api;
3 use BookStack\Actions\ActivityType;
4 use BookStack\Entities\Book;
5 use BookStack\Entities\Repos\BookRepo;
6 use BookStack\Exceptions\NotifyException;
7 use BookStack\Facades\Activity;
8 use Illuminate\Contracts\Container\BindingResolutionException;
9 use Illuminate\Http\Request;
10 use Illuminate\Validation\ValidationException;
12 class BookApiController extends ApiController
19 'name' => 'required|string|max:255',
20 'description' => 'string|max:1000',
24 'name' => 'string|min:1|max:255',
25 'description' => 'string|max:1000',
31 * BooksApiController constructor.
33 public function __construct(BookRepo $bookRepo)
35 $this->bookRepo = $bookRepo;
39 * Get a listing of books visible to the user.
41 public function list()
43 $books = Book::visible();
44 return $this->apiListingResponse($books, [
45 'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'image_id',
50 * Create a new book in the system.
51 * @throws ValidationException
53 public function create(Request $request)
55 $this->checkPermission('book-create-all');
56 $requestData = $this->validate($request, $this->rules['create']);
58 $book = $this->bookRepo->create($requestData);
59 return response()->json($book);
63 * View the details of a single book.
65 public function read(string $id)
67 $book = Book::visible()->with(['tags', 'cover', 'createdBy', 'updatedBy'])->findOrFail($id);
68 return response()->json($book);
72 * Update the details of a single book.
73 * @throws ValidationException
75 public function update(Request $request, string $id)
77 $book = Book::visible()->findOrFail($id);
78 $this->checkOwnablePermission('book-update', $book);
80 $requestData = $this->validate($request, $this->rules['update']);
81 $book = $this->bookRepo->update($book, $requestData);
83 return response()->json($book);
87 * Delete a single book from the system.
88 * @throws NotifyException
89 * @throws BindingResolutionException
91 public function delete(string $id)
93 $book = Book::visible()->findOrFail($id);
94 $this->checkOwnablePermission('book-delete', $book);
96 $this->bookRepo->destroy($book);
97 return response('', 204);