]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/BooksApiController.php
Started work on generating API docs
[bookstack] / app / Http / Controllers / Api / BooksApiController.php
1 <?php namespace BookStack\Http\Controllers\Api;
2
3 use BookStack\Entities\Book;
4 use BookStack\Entities\Repos\BookRepo;
5 use BookStack\Facades\Activity;
6 use Illuminate\Http\Request;
7
8 class BooksApiController extends ApiController
9 {
10
11     protected $bookRepo;
12
13     protected $rules = [
14         'create' => [
15             'name' => 'required|string|max:255',
16             'description' => 'string|max:1000',
17         ],
18         'update' => [
19             'name' => 'string|min:1|max:255',
20             'description' => 'string|max:1000',
21         ],
22     ];
23
24     /**
25      * BooksApiController constructor.
26      */
27     public function __construct(BookRepo $bookRepo)
28     {
29         $this->bookRepo = $bookRepo;
30     }
31
32     /**
33      * Get a listing of books visible to the user.
34      */
35     public function index()
36     {
37         $books = Book::visible();
38         return $this->apiListingResponse($books, [
39             'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'image_id',
40         ]);
41     }
42
43     /**
44      * Create a new book.
45      * @throws \Illuminate\Validation\ValidationException
46      */
47     public function create(Request $request)
48     {
49         $this->checkPermission('book-create-all');
50         $requestData = $this->validate($request, $this->rules['create']);
51
52         $book = $this->bookRepo->create($requestData);
53         Activity::add($book, 'book_create', $book->id);
54
55         return response()->json($book);
56     }
57
58     /**
59      * View the details of a single book.
60      */
61     public function read(string $id)
62     {
63         $book = Book::visible()->with(['tags', 'cover', 'createdBy', 'updatedBy'])->findOrFail($id);
64         return response()->json($book);
65     }
66
67     /**
68      * Update the details of a single book.
69      * @throws \Illuminate\Validation\ValidationException
70      */
71     public function update(Request $request, string $id)
72     {
73         $book = Book::visible()->findOrFail($id);
74         $this->checkOwnablePermission('book-update', $book);
75
76         $requestData = $this->validate($request, $this->rules['update']);
77         $book = $this->bookRepo->update($book, $requestData);
78         Activity::add($book, 'book_update', $book->id);
79
80         return response()->json($book);
81     }
82
83     /**
84      * Delete a book from the system.
85      * @throws \BookStack\Exceptions\NotifyException
86      * @throws \Illuminate\Contracts\Container\BindingResolutionException
87      */
88     public function delete(string $id)
89     {
90         $book = Book::visible()->findOrFail($id);
91         $this->checkOwnablePermission('book-delete', $book);
92
93         $this->bookRepo->destroy($book);
94         Activity::addMessage('book_delete', $book->name);
95
96         return response('', 204);
97     }
98 }