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