]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/BookApiController.php
Applied styleci changes for conversion work
[bookstack] / app / Http / Controllers / Api / BookApiController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Api;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Repos\BookRepo;
7 use Illuminate\Http\Request;
8 use Illuminate\Validation\ValidationException;
9
10 class BookApiController extends ApiController
11 {
12     protected $bookRepo;
13
14     public function __construct(BookRepo $bookRepo)
15     {
16         $this->bookRepo = $bookRepo;
17     }
18
19     /**
20      * Get a listing of books visible to the user.
21      */
22     public function list()
23     {
24         $books = Book::visible();
25
26         return $this->apiListingResponse($books, [
27             'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
28         ]);
29     }
30
31     /**
32      * Create a new book in the system.
33      * The cover image of a book can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
34      * If the 'image' property is null then the book cover image will be removed.
35      *
36      * @throws ValidationException
37      */
38     public function create(Request $request)
39     {
40         $this->checkPermission('book-create-all');
41         $requestData = $this->validate($request, $this->rules()['create']);
42
43         $book = $this->bookRepo->create($requestData);
44
45         return response()->json($book);
46     }
47
48     /**
49      * View the details of a single book.
50      */
51     public function read(string $id)
52     {
53         $book = Book::visible()->with(['tags', 'cover', 'createdBy', 'updatedBy', 'ownedBy'])->findOrFail($id);
54
55         return response()->json($book);
56     }
57
58     /**
59      * Update the details of a single book.
60      * The cover image of a book can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
61      * If the 'image' property is null then the book cover image will be removed.
62      *
63      * @throws ValidationException
64      */
65     public function update(Request $request, string $id)
66     {
67         $book = Book::visible()->findOrFail($id);
68         $this->checkOwnablePermission('book-update', $book);
69
70         $requestData = $this->validate($request, $this->rules()['update']);
71         $book = $this->bookRepo->update($book, $requestData);
72
73         return response()->json($book);
74     }
75
76     /**
77      * Delete a single book.
78      * This will typically send the book to the recycle bin.
79      *
80      * @throws \Exception
81      */
82     public function delete(string $id)
83     {
84         $book = Book::visible()->findOrFail($id);
85         $this->checkOwnablePermission('book-delete', $book);
86
87         $this->bookRepo->destroy($book);
88
89         return response('', 204);
90     }
91
92     protected function rules(): array
93     {
94         return [
95             'create' => [
96                 'name'        => ['required', 'string', 'max:255'],
97                 'description' => ['string', 'max:1000'],
98                 'tags'        => ['array'],
99                 'image'       => array_merge(['nullable'], $this->getImageValidationRules()),
100             ],
101             'update' => [
102                 'name'        => ['string', 'min:1', 'max:255'],
103                 'description' => ['string', 'max:1000'],
104                 'tags'        => ['array'],
105                 'image'       => array_merge(['nullable'], $this->getImageValidationRules()),
106             ],
107         ];
108     }
109 }