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.
36 public function index()
38 $books = Book::visible();
39 return $this->apiListingResponse($books, [
40 'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'image_id',
46 * @throws \Illuminate\Validation\ValidationException
48 public function create(Request $request)
50 $this->checkPermission('book-create-all');
51 $requestData = $this->validate($request, $this->rules['create']);
53 $book = $this->bookRepo->create($requestData);
54 Activity::add($book, 'book_create', $book->id);
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'])->findOrFail($id);
65 return response()->json($book);
69 * Update the details of a single book.
70 * @throws \Illuminate\Validation\ValidationException
72 public function update(Request $request, string $id)
74 $book = Book::visible()->findOrFail($id);
75 $this->checkOwnablePermission('book-update', $book);
77 $requestData = $this->validate($request, $this->rules['update']);
78 $book = $this->bookRepo->update($book, $requestData);
79 Activity::add($book, 'book_update', $book->id);
81 return response()->json($book);
85 * Delete a book from the system.
86 * @throws \BookStack\Exceptions\NotifyException
87 * @throws \Illuminate\Contracts\Container\BindingResolutionException
89 public function delete(string $id)
91 $book = Book::visible()->findOrFail($id);
92 $this->checkOwnablePermission('book-delete', $book);
94 $this->bookRepo->destroy($book);
95 Activity::addMessage('book_delete', $book->name);
97 return response('', 204);