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 BooksApiController extends ApiController
18 'name' => 'required|string|max:255',
19 'description' => 'string|max:1000',
22 'name' => 'string|min:1|max:255',
23 'description' => 'string|max:1000',
28 * BooksApiController constructor.
30 public function __construct(BookRepo $bookRepo)
32 $this->bookRepo = $bookRepo;
36 * Get a listing of books visible to the user.
38 public function list()
40 $books = Book::visible();
41 return $this->apiListingResponse($books, [
42 'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'image_id',
47 * Create a new book in the system.
48 * @throws ValidationException
50 public function create(Request $request)
52 $this->checkPermission('book-create-all');
53 $requestData = $this->validate($request, $this->rules['create']);
55 $book = $this->bookRepo->create($requestData);
56 Activity::add($book, 'book_create', $book->id);
58 return response()->json($book);
62 * View the details of a single book.
64 public function read(string $id)
66 $book = Book::visible()->with(['tags', 'cover', 'createdBy', 'updatedBy'])->findOrFail($id);
67 return response()->json($book);
71 * 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);
81 Activity::add($book, 'book_update', $book->id);
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 Activity::addMessage('book_delete', $book->name);
99 return response('', 204);