]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/BookApiController.php
Apply fixes from StyleCI
[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     protected $rules = [
15         'create' => [
16             'name'        => 'required|string|max:255',
17             'description' => 'string|max:1000',
18             'tags'        => 'array',
19         ],
20         'update' => [
21             'name'        => 'string|min:1|max:255',
22             'description' => 'string|max:1000',
23             'tags'        => 'array',
24         ],
25     ];
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 list()
36     {
37         $books = Book::visible();
38
39         return $this->apiListingResponse($books, [
40             'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by', 'image_id',
41         ]);
42     }
43
44     /**
45      * Create a new book in the system.
46      *
47      * @throws ValidationException
48      */
49     public function create(Request $request)
50     {
51         $this->checkPermission('book-create-all');
52         $requestData = $this->validate($request, $this->rules['create']);
53
54         $book = $this->bookRepo->create($requestData);
55
56         return response()->json($book);
57     }
58
59     /**
60      * View the details of a single book.
61      */
62     public function read(string $id)
63     {
64         $book = Book::visible()->with(['tags', 'cover', 'createdBy', 'updatedBy', 'ownedBy'])->findOrFail($id);
65
66         return response()->json($book);
67     }
68
69     /**
70      * Update the details of a single book.
71      *
72      * @throws ValidationException
73      */
74     public function update(Request $request, string $id)
75     {
76         $book = Book::visible()->findOrFail($id);
77         $this->checkOwnablePermission('book-update', $book);
78
79         $requestData = $this->validate($request, $this->rules['update']);
80         $book = $this->bookRepo->update($book, $requestData);
81
82         return response()->json($book);
83     }
84
85     /**
86      * Delete a single book.
87      * This will typically send the book to the recycle bin.
88      *
89      * @throws \Exception
90      */
91     public function delete(string $id)
92     {
93         $book = Book::visible()->findOrFail($id);
94         $this->checkOwnablePermission('book-delete', $book);
95
96         $this->bookRepo->destroy($book);
97
98         return response('', 204);
99     }
100 }