3 namespace BookStack\Entities\Controllers;
5 use BookStack\Entities\Models\Chapter;
6 use BookStack\Entities\Queries\BookQueries;
7 use BookStack\Entities\Queries\ChapterQueries;
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 BookQueries $bookQueries,
46 * Get a listing of chapters visible to the user.
48 public function list()
50 $chapters = $this->queries->visibleForList();
52 return $this->apiListingResponse($chapters, [
53 'id', 'book_id', 'name', 'slug', 'description', 'priority',
54 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
59 * Create a new chapter in the system.
61 public function create(Request $request)
63 $requestData = $this->validate($request, $this->rules['create']);
65 $bookId = $request->get('book_id');
66 $book = $this->bookQueries->findVisibleByIdOrFail(intval($bookId));
67 $this->checkOwnablePermission('chapter-create', $book);
69 $chapter = $this->chapterRepo->create($requestData, $book);
71 return response()->json($this->forJsonDisplay($chapter));
75 * View the details of a single chapter.
77 public function read(string $id)
79 $chapter = $this->queries->findVisibleByIdOrFail(intval($id));
80 $chapter = $this->forJsonDisplay($chapter);
83 'createdBy', 'updatedBy', 'ownedBy',
84 'pages' => function (HasMany $query) {
85 $query->scopes('visible')->get(['id', 'name', 'slug']);
89 return response()->json($chapter);
93 * Update the details of a single chapter.
94 * Providing a 'book_id' property will essentially move the chapter
95 * into that parent element if you have permissions to do so.
97 public function update(Request $request, string $id)
99 $requestData = $this->validate($request, $this->rules()['update']);
100 $chapter = $this->queries->findVisibleByIdOrFail(intval($id));
101 $this->checkOwnablePermission('chapter-update', $chapter);
103 if ($request->has('book_id') && $chapter->book_id !== intval($requestData['book_id'])) {
104 $this->checkOwnablePermission('chapter-delete', $chapter);
107 $this->chapterRepo->move($chapter, "book:{$requestData['book_id']}");
108 } catch (Exception $exception) {
109 if ($exception instanceof PermissionsException) {
110 $this->showPermissionError();
113 return $this->jsonError(trans('errors.selected_book_not_found'));
117 $updatedChapter = $this->chapterRepo->update($chapter, $requestData);
119 return response()->json($this->forJsonDisplay($updatedChapter));
124 * This will typically send the chapter to the recycle bin.
126 public function delete(string $id)
128 $chapter = $this->queries->findVisibleByIdOrFail(intval($id));
129 $this->checkOwnablePermission('chapter-delete', $chapter);
131 $this->chapterRepo->destroy($chapter);
133 return response('', 204);
136 protected function forJsonDisplay(Chapter $chapter): Chapter
138 $chapter = clone $chapter;
139 $chapter->unsetRelations()->refresh();
141 $chapter->load(['tags']);
142 $chapter->makeVisible('description_html');
143 $chapter->setAttribute('description_html', $chapter->descriptionHtml());
144 $chapter->setAttribute('book_slug', $chapter->book()->first()->slug);