3 namespace BookStack\Entities\Controllers;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Entities\Queries\ChapterQueries;
8 use BookStack\Entities\Queries\EntityQueries;
9 use BookStack\Entities\Repos\ChapterRepo;
10 use BookStack\Exceptions\PermissionsException;
11 use BookStack\Http\ApiController;
12 use BookStack\Permissions\Permission;
14 use Illuminate\Http\Request;
16 class ChapterApiController extends ApiController
18 protected array $rules = [
20 'book_id' => ['required', 'integer'],
21 'name' => ['required', 'string', 'max:255'],
22 'description' => ['string', 'max:1900'],
23 'description_html' => ['string', 'max:2000'],
25 'priority' => ['integer'],
26 'default_template_id' => ['nullable', 'integer'],
29 'book_id' => ['integer'],
30 'name' => ['string', 'min:1', 'max:255'],
31 'description' => ['string', 'max:1900'],
32 'description_html' => ['string', 'max:2000'],
34 'priority' => ['integer'],
35 'default_template_id' => ['nullable', 'integer'],
39 public function __construct(
40 protected ChapterRepo $chapterRepo,
41 protected ChapterQueries $queries,
42 protected EntityQueries $entityQueries,
47 * Get a listing of chapters visible to the user.
49 public function list()
51 $chapters = $this->queries->visibleForList()
52 ->addSelect(['created_by', 'updated_by']);
54 return $this->apiListingResponse($chapters, [
55 'id', 'book_id', 'name', 'slug', 'description', 'priority',
56 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
61 * Create a new chapter in the system.
63 public function create(Request $request)
65 $requestData = $this->validate($request, $this->rules['create']);
67 $bookId = $request->get('book_id');
68 $book = $this->entityQueries->books->findVisibleByIdOrFail(intval($bookId));
69 $this->checkOwnablePermission(Permission::ChapterCreate, $book);
71 $chapter = $this->chapterRepo->create($requestData, $book);
73 return response()->json($this->forJsonDisplay($chapter));
77 * View the details of a single chapter.
79 public function read(string $id)
81 $chapter = $this->queries->findVisibleByIdOrFail(intval($id));
82 $chapter = $this->forJsonDisplay($chapter);
84 $chapter->load(['createdBy', 'updatedBy', 'ownedBy']);
86 // Note: More fields than usual here, for backwards compatibility,
87 // due to previously accidentally including more fields that desired.
88 $pages = $this->entityQueries->pages->visibleForChapterList($chapter->id)
89 ->addSelect(['created_by', 'updated_by', 'revision_count', 'editor'])
91 $chapter->setRelation('pages', $pages);
93 return response()->json($chapter);
97 * Update the details of a single chapter.
98 * Providing a 'book_id' property will essentially move the chapter
99 * into that parent element if you have permissions to do so.
101 public function update(Request $request, string $id)
103 $requestData = $this->validate($request, $this->rules()['update']);
104 $chapter = $this->queries->findVisibleByIdOrFail(intval($id));
105 $this->checkOwnablePermission(Permission::ChapterUpdate, $chapter);
107 if ($request->has('book_id') && $chapter->book_id !== (intval($requestData['book_id']) ?: null)) {
108 $this->checkOwnablePermission(Permission::ChapterDelete, $chapter);
111 $this->chapterRepo->move($chapter, "book:{$requestData['book_id']}");
112 } catch (Exception $exception) {
113 if ($exception instanceof PermissionsException) {
114 $this->showPermissionError();
117 return $this->jsonError(trans('errors.selected_book_not_found'));
121 $updatedChapter = $this->chapterRepo->update($chapter, $requestData);
123 return response()->json($this->forJsonDisplay($updatedChapter));
128 * This will typically send the chapter to the recycle bin.
130 public function delete(string $id)
132 $chapter = $this->queries->findVisibleByIdOrFail(intval($id));
133 $this->checkOwnablePermission(Permission::ChapterDelete, $chapter);
135 $this->chapterRepo->destroy($chapter);
137 return response('', 204);
140 protected function forJsonDisplay(Chapter $chapter): Chapter
142 $chapter = clone $chapter;
143 $chapter->unsetRelations()->refresh();
145 $chapter->load(['tags']);
146 $chapter->makeVisible('description_html');
147 $chapter->setAttribute('description_html', $chapter->descriptionInfo()->getHtml());
149 /** @var Book $book */
150 $book = $chapter->book()->first();
151 $chapter->setAttribute('book_slug', $book->slug);