]> BookStack Code Mirror - bookstack/blob - app/Entities/Controllers/ChapterApiController.php
Chapters API: Added missing book_slug field
[bookstack] / app / Entities / Controllers / ChapterApiController.php
1 <?php
2
3 namespace BookStack\Entities\Controllers;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Entities\Repos\ChapterRepo;
8 use BookStack\Exceptions\PermissionsException;
9 use BookStack\Http\ApiController;
10 use Exception;
11 use Illuminate\Database\Eloquent\Relations\HasMany;
12 use Illuminate\Http\Request;
13
14 class ChapterApiController extends ApiController
15 {
16     protected $rules = [
17         'create' => [
18             'book_id'          => ['required', 'integer'],
19             'name'             => ['required', 'string', 'max:255'],
20             'description'      => ['string', 'max:1900'],
21             'description_html' => ['string', 'max:2000'],
22             'tags'             => ['array'],
23             'priority'         => ['integer'],
24         ],
25         'update' => [
26             'book_id'          => ['integer'],
27             'name'             => ['string', 'min:1', 'max:255'],
28             'description'      => ['string', 'max:1900'],
29             'description_html' => ['string', 'max:2000'],
30             'tags'             => ['array'],
31             'priority'         => ['integer'],
32         ],
33     ];
34
35     public function __construct(
36         protected ChapterRepo $chapterRepo
37     ) {
38     }
39
40     /**
41      * Get a listing of chapters visible to the user.
42      */
43     public function list()
44     {
45         $chapters = Chapter::visible();
46
47         return $this->apiListingResponse($chapters, [
48             'id', 'book_id', 'name', 'slug', 'description', 'priority',
49             'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
50         ]);
51     }
52
53     /**
54      * Create a new chapter in the system.
55      */
56     public function create(Request $request)
57     {
58         $requestData = $this->validate($request, $this->rules['create']);
59
60         $bookId = $request->get('book_id');
61         $book = Book::visible()->findOrFail($bookId);
62         $this->checkOwnablePermission('chapter-create', $book);
63
64         $chapter = $this->chapterRepo->create($requestData, $book);
65
66         return response()->json($this->forJsonDisplay($chapter));
67     }
68
69     /**
70      * View the details of a single chapter.
71      */
72     public function read(string $id)
73     {
74         $chapter = Chapter::visible()->findOrFail($id);
75         $chapter = $this->forJsonDisplay($chapter);
76
77         $chapter->load([
78             'createdBy', 'updatedBy', 'ownedBy',
79             'pages' => function (HasMany $query) {
80                 $query->scopes('visible')->get(['id', 'name', 'slug']);
81             }
82         ]);
83
84         return response()->json($chapter);
85     }
86
87     /**
88      * Update the details of a single chapter.
89      * Providing a 'book_id' property will essentially move the chapter
90      * into that parent element if you have permissions to do so.
91      */
92     public function update(Request $request, string $id)
93     {
94         $requestData = $this->validate($request, $this->rules()['update']);
95         $chapter = Chapter::visible()->findOrFail($id);
96         $this->checkOwnablePermission('chapter-update', $chapter);
97
98         if ($request->has('book_id') && $chapter->book_id !== intval($requestData['book_id'])) {
99             $this->checkOwnablePermission('chapter-delete', $chapter);
100
101             try {
102                 $this->chapterRepo->move($chapter, "book:{$requestData['book_id']}");
103             } catch (Exception $exception) {
104                 if ($exception instanceof PermissionsException) {
105                     $this->showPermissionError();
106                 }
107
108                 return $this->jsonError(trans('errors.selected_book_not_found'));
109             }
110         }
111
112         $updatedChapter = $this->chapterRepo->update($chapter, $requestData);
113
114         return response()->json($this->forJsonDisplay($updatedChapter));
115     }
116
117     /**
118      * Delete a chapter.
119      * This will typically send the chapter to the recycle bin.
120      */
121     public function delete(string $id)
122     {
123         $chapter = Chapter::visible()->findOrFail($id);
124         $this->checkOwnablePermission('chapter-delete', $chapter);
125
126         $this->chapterRepo->destroy($chapter);
127
128         return response('', 204);
129     }
130
131     protected function forJsonDisplay(Chapter $chapter): Chapter
132     {
133         $chapter = clone $chapter;
134         $chapter->unsetRelations()->refresh();
135
136         $chapter->load(['tags']);
137         $chapter->makeVisible('description_html');
138         $chapter->setAttribute('description_html', $chapter->descriptionHtml());
139         $chapter->setAttribute('book_slug', $chapter->book()->first()->slug);
140
141         return $chapter;
142     }
143 }