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\Http\ApiController;
9 use Illuminate\Database\Eloquent\Relations\HasMany;
10 use Illuminate\Http\Request;
12 class ChapterApiController extends ApiController
14 protected $chapterRepo;
18 'book_id' => ['required', 'integer'],
19 'name' => ['required', 'string', 'max:255'],
20 'description' => ['string', 'max:1000'],
24 'book_id' => ['integer'],
25 'name' => ['string', 'min:1', 'max:255'],
26 'description' => ['string', 'max:1000'],
32 * ChapterController constructor.
34 public function __construct(ChapterRepo $chapterRepo)
36 $this->chapterRepo = $chapterRepo;
40 * Get a listing of chapters visible to the user.
42 public function list()
44 $chapters = Chapter::visible();
46 return $this->apiListingResponse($chapters, [
47 'id', 'book_id', 'name', 'slug', 'description', 'priority',
48 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
53 * Create a new chapter in the system.
55 public function create(Request $request)
57 $this->validate($request, $this->rules['create']);
59 $bookId = $request->get('book_id');
60 $book = Book::visible()->findOrFail($bookId);
61 $this->checkOwnablePermission('chapter-create', $book);
63 $chapter = $this->chapterRepo->create($request->all(), $book);
65 return response()->json($chapter->load(['tags']));
69 * View the details of a single chapter.
71 public function read(string $id)
73 $chapter = Chapter::visible()->with(['tags', 'createdBy', 'updatedBy', 'ownedBy', 'pages' => function (HasMany $query) {
74 $query->scopes('visible')->get(['id', 'name', 'slug']);
77 return response()->json($chapter);
81 * Update the details of a single chapter.
83 public function update(Request $request, string $id)
85 $chapter = Chapter::visible()->findOrFail($id);
86 $this->checkOwnablePermission('chapter-update', $chapter);
88 $updatedChapter = $this->chapterRepo->update($chapter, $request->all());
90 return response()->json($updatedChapter->load(['tags']));
95 * This will typically send the chapter to the recycle bin.
97 public function delete(string $id)
99 $chapter = Chapter::visible()->findOrFail($id);
100 $this->checkOwnablePermission('chapter-delete', $chapter);
102 $this->chapterRepo->destroy($chapter);
104 return response('', 204);