3 namespace BookStack\Entities\Controllers;
5 use BookStack\Entities\Models\Chapter;
6 use BookStack\Entities\Queries\ChapterQueries;
7 use BookStack\Entities\Queries\EntityQueries;
8 use BookStack\Entities\Repos\ChapterRepo;
9 use BookStack\Exceptions\PermissionsException;
10 use BookStack\Http\ApiController;
12 use Illuminate\Database\Eloquent\Relations\HasMany;
13 use Illuminate\Http\Request;
15 class ChapterApiController extends ApiController
19 'book_id' => ['required', 'integer'],
20 'name' => ['required', 'string', 'max:255'],
21 'description' => ['string', 'max:1900'],
22 'description_html' => ['string', 'max:2000'],
24 'priority' => ['integer'],
25 'default_template_id' => ['nullable', 'integer'],
28 'book_id' => ['integer'],
29 'name' => ['string', 'min:1', 'max:255'],
30 'description' => ['string', 'max:1900'],
31 'description_html' => ['string', 'max:2000'],
33 'priority' => ['integer'],
34 'default_template_id' => ['nullable', 'integer'],
38 public function __construct(
39 protected ChapterRepo $chapterRepo,
40 protected ChapterQueries $queries,
41 protected EntityQueries $entityQueries,
46 * Get a listing of chapters visible to the user.
48 public function list()
50 $chapters = $this->queries->visibleForList()
51 ->addSelect(['created_by', 'updated_by']);
53 return $this->apiListingResponse($chapters, [
54 'id', 'book_id', 'name', 'slug', 'description', 'priority',
55 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
60 * Create a new chapter in the system.
62 public function create(Request $request)
64 $requestData = $this->validate($request, $this->rules['create']);
66 $bookId = $request->get('book_id');
67 $book = $this->entityQueries->books->findVisibleByIdOrFail(intval($bookId));
68 $this->checkOwnablePermission('chapter-create', $book);
70 $chapter = $this->chapterRepo->create($requestData, $book);
72 return response()->json($this->forJsonDisplay($chapter));
76 * View the details of a single chapter.
78 public function read(string $id)
80 $chapter = $this->queries->findVisibleByIdOrFail(intval($id));
81 $chapter = $this->forJsonDisplay($chapter);
83 $chapter->load(['createdBy', 'updatedBy', 'ownedBy']);
85 // Note: More fields than usual here, for backwards compatibility,
86 // due to previously accidentally including more fields that desired.
87 $pages = $this->entityQueries->pages->visibleForChapterList($chapter->id)
88 ->addSelect(['created_by', 'updated_by', 'revision_count', 'editor'])
90 $chapter->setRelation('pages', $pages);
92 return response()->json($chapter);
96 * Update the details of a single chapter.
97 * Providing a 'book_id' property will essentially move the chapter
98 * into that parent element if you have permissions to do so.
100 public function update(Request $request, string $id)
102 $requestData = $this->validate($request, $this->rules()['update']);
103 $chapter = $this->queries->findVisibleByIdOrFail(intval($id));
104 $this->checkOwnablePermission('chapter-update', $chapter);
106 if ($request->has('book_id') && $chapter->book_id !== intval($requestData['book_id'])) {
107 $this->checkOwnablePermission('chapter-delete', $chapter);
110 $this->chapterRepo->move($chapter, "book:{$requestData['book_id']}");
111 } catch (Exception $exception) {
112 if ($exception instanceof PermissionsException) {
113 $this->showPermissionError();
116 return $this->jsonError(trans('errors.selected_book_not_found'));
120 $updatedChapter = $this->chapterRepo->update($chapter, $requestData);
122 return response()->json($this->forJsonDisplay($updatedChapter));
127 * This will typically send the chapter to the recycle bin.
129 public function delete(string $id)
131 $chapter = $this->queries->findVisibleByIdOrFail(intval($id));
132 $this->checkOwnablePermission('chapter-delete', $chapter);
134 $this->chapterRepo->destroy($chapter);
136 return response('', 204);
139 protected function forJsonDisplay(Chapter $chapter): Chapter
141 $chapter = clone $chapter;
142 $chapter->unsetRelations()->refresh();
144 $chapter->load(['tags']);
145 $chapter->makeVisible('description_html');
146 $chapter->setAttribute('description_html', $chapter->descriptionHtml());
147 $chapter->setAttribute('book_slug', $chapter->book()->first()->slug);