]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/BookApiController.php
Added force option for update-url command
[bookstack] / app / Http / Controllers / Api / BookApiController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Api;
4
5 use BookStack\Api\ApiEntityListFormatter;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Chapter;
8 use BookStack\Entities\Models\Entity;
9 use BookStack\Entities\Repos\BookRepo;
10 use BookStack\Entities\Tools\BookContents;
11 use Illuminate\Http\Request;
12 use Illuminate\Validation\ValidationException;
13
14 class BookApiController extends ApiController
15 {
16     protected BookRepo $bookRepo;
17
18     public function __construct(BookRepo $bookRepo)
19     {
20         $this->bookRepo = $bookRepo;
21     }
22
23     /**
24      * Get a listing of books visible to the user.
25      */
26     public function list()
27     {
28         $books = Book::visible();
29
30         return $this->apiListingResponse($books, [
31             'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
32         ]);
33     }
34
35     /**
36      * Create a new book in the system.
37      * The cover image of a book can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
38      * If the 'image' property is null then the book cover image will be removed.
39      *
40      * @throws ValidationException
41      */
42     public function create(Request $request)
43     {
44         $this->checkPermission('book-create-all');
45         $requestData = $this->validate($request, $this->rules()['create']);
46
47         $book = $this->bookRepo->create($requestData);
48
49         return response()->json($book);
50     }
51
52     /**
53      * View the details of a single book.
54      * The response data will contain 'content' property listing the chapter and pages directly within, in
55      * the same structure as you'd see within the BookStack interface when viewing a book. Top-level
56      * contents will have a 'type' property to distinguish between pages & chapters.
57      */
58     public function read(string $id)
59     {
60         $book = Book::visible()->with(['tags', 'cover', 'createdBy', 'updatedBy', 'ownedBy'])->findOrFail($id);
61
62         $contents = (new BookContents($book))->getTree(true, false)->all();
63         $contentsApiData = (new ApiEntityListFormatter($contents))
64             ->withType()
65             ->withField('pages', function (Entity $entity) {
66                 if ($entity instanceof Chapter) {
67                     return (new ApiEntityListFormatter($entity->pages->all()))->format();
68                 }
69                 return null;
70             })->format();
71         $book->setAttribute('contents', $contentsApiData);
72
73         return response()->json($book);
74     }
75
76     /**
77      * Update the details of a single book.
78      * The cover image of a book can be set by sending a file via an 'image' property within a 'multipart/form-data' request.
79      * If the 'image' property is null then the book cover image will be removed.
80      *
81      * @throws ValidationException
82      */
83     public function update(Request $request, string $id)
84     {
85         $book = Book::visible()->findOrFail($id);
86         $this->checkOwnablePermission('book-update', $book);
87
88         $requestData = $this->validate($request, $this->rules()['update']);
89         $book = $this->bookRepo->update($book, $requestData);
90
91         return response()->json($book);
92     }
93
94     /**
95      * Delete a single book.
96      * This will typically send the book to the recycle bin.
97      *
98      * @throws \Exception
99      */
100     public function delete(string $id)
101     {
102         $book = Book::visible()->findOrFail($id);
103         $this->checkOwnablePermission('book-delete', $book);
104
105         $this->bookRepo->destroy($book);
106
107         return response('', 204);
108     }
109
110     protected function rules(): array
111     {
112         return [
113             'create' => [
114                 'name'        => ['required', 'string', 'max:255'],
115                 'description' => ['string', 'max:1000'],
116                 'tags'        => ['array'],
117                 'image'       => array_merge(['nullable'], $this->getImageValidationRules()),
118             ],
119             'update' => [
120                 'name'        => ['string', 'min:1', 'max:255'],
121                 'description' => ['string', 'max:1000'],
122                 'tags'        => ['array'],
123                 'image'       => array_merge(['nullable'], $this->getImageValidationRules()),
124             ],
125         ];
126     }
127 }