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:1000'],
22 'priority' => ['integer'],
25 'book_id' => ['integer'],
26 'name' => ['string', 'min:1', 'max:255'],
27 'description' => ['string', 'max:1000'],
29 'priority' => ['integer'],
33 public function __construct(
34 protected ChapterRepo $chapterRepo
39 * Get a listing of chapters visible to the user.
41 public function list()
43 $chapters = Chapter::visible();
45 return $this->apiListingResponse($chapters, [
46 'id', 'book_id', 'name', 'slug', 'description', 'priority',
47 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
52 * Create a new chapter in the system.
54 public function create(Request $request)
56 $requestData = $this->validate($request, $this->rules['create']);
58 $bookId = $request->get('book_id');
59 $book = Book::visible()->findOrFail($bookId);
60 $this->checkOwnablePermission('chapter-create', $book);
62 $chapter = $this->chapterRepo->create($requestData, $book);
64 return response()->json($chapter->load(['tags']));
68 * View the details of a single chapter.
70 public function read(string $id)
72 $chapter = Chapter::visible()->with(['tags', 'createdBy', 'updatedBy', 'ownedBy', 'pages' => function (HasMany $query) {
73 $query->scopes('visible')->get(['id', 'name', 'slug']);
76 return response()->json($chapter);
80 * Update the details of a single chapter.
81 * Providing a 'book_id' property will essentially move the chapter
82 * into that parent element if you have permissions to do so.
84 public function update(Request $request, string $id)
86 $requestData = $this->validate($request, $this->rules()['update']);
87 $chapter = Chapter::visible()->findOrFail($id);
88 $this->checkOwnablePermission('chapter-update', $chapter);
90 if ($request->has('book_id') && $chapter->book_id !== intval($requestData['book_id'])) {
91 $this->checkOwnablePermission('chapter-delete', $chapter);
94 $this->chapterRepo->move($chapter, "book:{$requestData['book_id']}");
95 } catch (Exception $exception) {
96 if ($exception instanceof PermissionsException) {
97 $this->showPermissionError();
100 return $this->jsonError(trans('errors.selected_book_not_found'));
104 $updatedChapter = $this->chapterRepo->update($chapter, $requestData);
106 return response()->json($updatedChapter->load(['tags']));
111 * This will typically send the chapter to the recycle bin.
113 public function delete(string $id)
115 $chapter = Chapter::visible()->findOrFail($id);
116 $this->checkOwnablePermission('chapter-delete', $chapter);
118 $this->chapterRepo->destroy($chapter);
120 return response('', 204);