3 namespace BookStack\Entities\Controllers;
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;
11 use Illuminate\Database\Eloquent\Relations\HasMany;
12 use Illuminate\Http\Request;
14 class ChapterApiController extends ApiController
18 'book_id' => ['required', 'integer'],
19 'name' => ['required', 'string', 'max:255'],
20 'description' => ['string', 'max:1900'],
21 'description_html' => ['string', 'max:2000'],
23 'priority' => ['integer'],
26 'book_id' => ['integer'],
27 'name' => ['string', 'min:1', 'max:255'],
28 'description' => ['string', 'max:1900'],
29 'description_html' => ['string', 'max:2000'],
31 'priority' => ['integer'],
35 public function __construct(
36 protected ChapterRepo $chapterRepo
41 * Get a listing of chapters visible to the user.
43 public function list()
45 $chapters = Chapter::visible();
47 return $this->apiListingResponse($chapters, [
48 'id', 'book_id', 'name', 'slug', 'description', 'priority',
49 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
54 * Create a new chapter in the system.
56 public function create(Request $request)
58 $requestData = $this->validate($request, $this->rules['create']);
60 $bookId = $request->get('book_id');
61 $book = Book::visible()->findOrFail($bookId);
62 $this->checkOwnablePermission('chapter-create', $book);
64 $chapter = $this->chapterRepo->create($requestData, $book);
66 return response()->json($this->forJsonDisplay($chapter));
70 * View the details of a single chapter.
72 public function read(string $id)
74 $chapter = Chapter::visible()->findOrFail($id);
75 $chapter = $this->forJsonDisplay($chapter);
78 'createdBy', 'updatedBy', 'ownedBy',
79 'pages' => function (HasMany $query) {
80 $query->scopes('visible')->get(['id', 'name', 'slug']);
84 return response()->json($chapter);
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.
92 public function update(Request $request, string $id)
94 $requestData = $this->validate($request, $this->rules()['update']);
95 $chapter = Chapter::visible()->findOrFail($id);
96 $this->checkOwnablePermission('chapter-update', $chapter);
98 if ($request->has('book_id') && $chapter->book_id !== intval($requestData['book_id'])) {
99 $this->checkOwnablePermission('chapter-delete', $chapter);
102 $this->chapterRepo->move($chapter, "book:{$requestData['book_id']}");
103 } catch (Exception $exception) {
104 if ($exception instanceof PermissionsException) {
105 $this->showPermissionError();
108 return $this->jsonError(trans('errors.selected_book_not_found'));
112 $updatedChapter = $this->chapterRepo->update($chapter, $requestData);
114 return response()->json($this->forJsonDisplay($updatedChapter));
119 * This will typically send the chapter to the recycle bin.
121 public function delete(string $id)
123 $chapter = Chapter::visible()->findOrFail($id);
124 $this->checkOwnablePermission('chapter-delete', $chapter);
126 $this->chapterRepo->destroy($chapter);
128 return response('', 204);
131 protected function forJsonDisplay(Chapter $chapter): Chapter
133 $chapter = clone $chapter;
134 $chapter->unsetRelations()->refresh();
136 $chapter->load(['tags']);
137 $chapter->makeVisible('description_html')
138 ->setAttribute('description_html', $chapter->descriptionHtml());