]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/BooksApiController.php
Filled out base Book API endpoints, added example responses
[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\Facades\Activity;
6 use Illuminate\Http\Request;
7
8 class BooksApiController extends ApiController
9 {
10
11     protected $bookRepo;
12
13     protected $rules = [
14         'create' => [
15             'name' => 'required|string|max:255',
16             'description' => 'string|max:1000',
17         ],
18         'update' => [
19             'name' => 'string|min:1|max:255',
20             'description' => 'string|max:1000',
21         ],
22     ];
23
24     /**
25      * BooksApiController constructor.
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      * @api listing
35      */
36     public function index()
37     {
38         $books = Book::visible();
39         return $this->apiListingResponse($books, [
40             'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'image_id',
41         ]);
42     }
43
44     /**
45      * Create a new book.
46      * @throws \Illuminate\Validation\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         Activity::add($book, 'book_create', $book->id);
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'])->findOrFail($id);
65         return response()->json($book);
66     }
67
68     /**
69      * Update the details of a single book.
70      * @throws \Illuminate\Validation\ValidationException
71      */
72     public function update(Request $request, string $id)
73     {
74         $book = Book::visible()->findOrFail($id);
75         $this->checkOwnablePermission('book-update', $book);
76
77         $requestData = $this->validate($request, $this->rules['update']);
78         $book = $this->bookRepo->update($book, $requestData);
79         Activity::add($book, 'book_update', $book->id);
80
81         return response()->json($book);
82     }
83
84     /**
85      * Delete a book from the system.
86      * @throws \BookStack\Exceptions\NotifyException
87      * @throws \Illuminate\Contracts\Container\BindingResolutionException
88      */
89     public function delete(string $id)
90     {
91         $book = Book::visible()->findOrFail($id);
92         $this->checkOwnablePermission('book-delete', $book);
93
94         $this->bookRepo->destroy($book);
95         Activity::addMessage('book_delete', $book->name);
96
97         return response('', 204);
98     }
99 }