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'],
24 'default_template_id' => ['nullable', 'integer'],
27 'book_id' => ['integer'],
28 'name' => ['string', 'min:1', 'max:255'],
29 'description' => ['string', 'max:1900'],
30 'description_html' => ['string', 'max:2000'],
32 'priority' => ['integer'],
33 'default_template_id' => ['nullable', 'integer'],
37 public function __construct(
38 protected ChapterRepo $chapterRepo
43 * Get a listing of chapters visible to the user.
45 public function list()
47 $chapters = Chapter::visible();
49 return $this->apiListingResponse($chapters, [
50 'id', 'book_id', 'name', 'slug', 'description', 'priority',
51 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
56 * Create a new chapter in the system.
58 public function create(Request $request)
60 $requestData = $this->validate($request, $this->rules['create']);
62 $bookId = $request->get('book_id');
63 $book = Book::visible()->findOrFail($bookId);
64 $this->checkOwnablePermission('chapter-create', $book);
66 $chapter = $this->chapterRepo->create($requestData, $book);
68 return response()->json($this->forJsonDisplay($chapter));
72 * View the details of a single chapter.
74 public function read(string $id)
76 $chapter = Chapter::visible()->findOrFail($id);
77 $chapter = $this->forJsonDisplay($chapter);
80 'createdBy', 'updatedBy', 'ownedBy',
81 'pages' => function (HasMany $query) {
82 $query->scopes('visible')->get(['id', 'name', 'slug']);
86 return response()->json($chapter);
90 * Update the details of a single chapter.
91 * Providing a 'book_id' property will essentially move the chapter
92 * into that parent element if you have permissions to do so.
94 public function update(Request $request, string $id)
96 $requestData = $this->validate($request, $this->rules()['update']);
97 $chapter = Chapter::visible()->findOrFail($id);
98 $this->checkOwnablePermission('chapter-update', $chapter);
100 if ($request->has('book_id') && $chapter->book_id !== intval($requestData['book_id'])) {
101 $this->checkOwnablePermission('chapter-delete', $chapter);
104 $this->chapterRepo->move($chapter, "book:{$requestData['book_id']}");
105 } catch (Exception $exception) {
106 if ($exception instanceof PermissionsException) {
107 $this->showPermissionError();
110 return $this->jsonError(trans('errors.selected_book_not_found'));
114 $updatedChapter = $this->chapterRepo->update($chapter, $requestData);
116 return response()->json($this->forJsonDisplay($updatedChapter));
121 * This will typically send the chapter to the recycle bin.
123 public function delete(string $id)
125 $chapter = Chapter::visible()->findOrFail($id);
126 $this->checkOwnablePermission('chapter-delete', $chapter);
128 $this->chapterRepo->destroy($chapter);
130 return response('', 204);
133 protected function forJsonDisplay(Chapter $chapter): Chapter
135 $chapter = clone $chapter;
136 $chapter->unsetRelations()->refresh();
138 $chapter->load(['tags']);
139 $chapter->makeVisible('description_html')
140 ->setAttribute('description_html', $chapter->descriptionHtml());