]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/BookApiController.php
Started work on hierachy conversion actions
[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      *
34      * @throws ValidationException
35      */
36     public function create(Request $request)
37     {
38         $this->checkPermission('book-create-all');
39         $requestData = $this->validate($request, $this->rules['create']);
40
41         $book = $this->bookRepo->create($requestData);
42
43         return response()->json($book);
44     }
45
46     /**
47      * View the details of a single book.
48      */
49     public function read(string $id)
50     {
51         $book = Book::visible()->with(['tags', 'cover', 'createdBy', 'updatedBy', 'ownedBy'])->findOrFail($id);
52
53         return response()->json($book);
54     }
55
56     /**
57      * Update the details of a single book.
58      *
59      * @throws ValidationException
60      */
61     public function update(Request $request, string $id)
62     {
63         $book = Book::visible()->findOrFail($id);
64         $this->checkOwnablePermission('book-update', $book);
65
66         $requestData = $this->validate($request, $this->rules['update']);
67         $book = $this->bookRepo->update($book, $requestData);
68
69         return response()->json($book);
70     }
71
72     /**
73      * Delete a single book.
74      * This will typically send the book to the recycle bin.
75      *
76      * @throws \Exception
77      */
78     public function delete(string $id)
79     {
80         $book = Book::visible()->findOrFail($id);
81         $this->checkOwnablePermission('book-delete', $book);
82
83         $this->bookRepo->destroy($book);
84
85         return response('', 204);
86     }
87
88     protected function rules(): array {
89         return [
90             'create' => [
91                 'name'        => ['required', 'string', 'max:255'],
92                 'description' => ['string', 'max:1000'],
93                 'tags'        => ['array'],
94                 'image'       => array_merge(['nullable'], $this->getImageValidationRules()),
95             ],
96             'update' => [
97                 'name'        => ['string', 'min:1', 'max:255'],
98                 'description' => ['string', 'max:1000'],
99                 'tags'        => ['array'],
100                 'image'       => array_merge(['nullable'], $this->getImageValidationRules()),
101             ],
102         ];
103     }
104 }