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