]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/BooksApiController.php
Move logFailedAccess into Activity
[bookstack] / app / Http / Controllers / Api / BooksApiController.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 BooksApiController 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         ],
21         'update' => [
22             'name' => 'string|min:1|max:255',
23             'description' => 'string|max:1000',
24         ],
25     ];
26
27     /**
28      * BooksApiController constructor.
29      */
30     public function __construct(BookRepo $bookRepo)
31     {
32         $this->bookRepo = $bookRepo;
33     }
34
35     /**
36      * Get a listing of books visible to the user.
37      */
38     public function list()
39     {
40         $books = Book::visible();
41         return $this->apiListingResponse($books, [
42             'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'image_id',
43         ]);
44     }
45
46     /**
47      * Create a new book in the system.
48      * @throws ValidationException
49      */
50     public function create(Request $request)
51     {
52         $this->checkPermission('book-create-all');
53         $requestData = $this->validate($request, $this->rules['create']);
54
55         $book = $this->bookRepo->create($requestData);
56         Activity::add($book, 'book_create', $book->id);
57
58         return response()->json($book);
59     }
60
61     /**
62      * View the details of a single book.
63      */
64     public function read(string $id)
65     {
66         $book = Book::visible()->with(['tags', 'cover', 'createdBy', 'updatedBy'])->findOrFail($id);
67         return response()->json($book);
68     }
69
70     /**
71      * Update the details of a single book.
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         Activity::add($book, 'book_update', $book->id);
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         Activity::addMessage('book_delete', $book->name);
98
99         return response('', 204);
100     }
101 }