]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/BookApiController.php
Merge branch 'show-tags' of https://github.com/burnoutberni/BookStack into burnoutber...
[bookstack] / app / Http / Controllers / Api / BookApiController.php
1 <?php namespace BookStack\Http\Controllers\Api;
2
3 use BookStack\Entities\Models\Book;
4 use BookStack\Entities\Repos\BookRepo;
5 use BookStack\Exceptions\NotifyException;
6 use Illuminate\Contracts\Container\BindingResolutionException;
7 use Illuminate\Http\Request;
8 use Illuminate\Validation\ValidationException;
9
10 class BookApiController extends ApiController
11 {
12
13     protected $bookRepo;
14
15     protected $rules = [
16         'create' => [
17             'name' => 'required|string|max:255',
18             'description' => 'string|max:1000',
19             'tags' => 'array',
20         ],
21         'update' => [
22             'name' => 'string|min:1|max:255',
23             'description' => 'string|max:1000',
24             'tags' => 'array',
25         ],
26     ];
27
28     public function __construct(BookRepo $bookRepo)
29     {
30         $this->bookRepo = $bookRepo;
31     }
32
33     /**
34      * Get a listing of books visible to the user.
35      */
36     public function list()
37     {
38         $books = Book::visible();
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      * @throws ValidationException
47      */
48     public function create(Request $request)
49     {
50         $this->checkPermission('book-create-all');
51         $requestData = $this->validate($request, $this->rules['create']);
52
53         $book = $this->bookRepo->create($requestData);
54         return response()->json($book);
55     }
56
57     /**
58      * View the details of a single book.
59      */
60     public function read(string $id)
61     {
62         $book = Book::visible()->with(['tags', 'cover', 'createdBy', 'updatedBy', 'ownedBy'])->findOrFail($id);
63         return response()->json($book);
64     }
65
66     /**
67      * Update the details of a single book.
68      * @throws ValidationException
69      */
70     public function update(Request $request, string $id)
71     {
72         $book = Book::visible()->findOrFail($id);
73         $this->checkOwnablePermission('book-update', $book);
74
75         $requestData = $this->validate($request, $this->rules['update']);
76         $book = $this->bookRepo->update($book, $requestData);
77
78         return response()->json($book);
79     }
80
81     /**
82      * Delete a single book.
83      * This will typically send the book to the recycle bin.
84      * @throws \Exception
85      */
86     public function delete(string $id)
87     {
88         $book = Book::visible()->findOrFail($id);
89         $this->checkOwnablePermission('book-delete', $book);
90
91         $this->bookRepo->destroy($book);
92         return response('', 204);
93     }
94 }