3 namespace BookStack\Http\Controllers\Api;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Chapter;
7 use BookStack\Entities\Repos\ChapterRepo;
8 use Illuminate\Database\Eloquent\Relations\HasMany;
9 use Illuminate\Http\Request;
11 class ChapterApiController extends ApiController
13 protected $chapterRepo;
17 'book_id' => 'required|integer',
18 'name' => 'required|string|max:255',
19 'description' => 'string|max:1000',
23 'book_id' => 'integer',
24 'name' => 'string|min:1|max:255',
25 'description' => 'string|max:1000',
31 * ChapterController constructor.
33 public function __construct(ChapterRepo $chapterRepo)
35 $this->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 $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($request->all(), $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->visible()->get(['id', 'name', 'slug']);
76 return response()->json($chapter);
80 * Update the details of a single chapter.
82 public function update(Request $request, string $id)
84 $chapter = Chapter::visible()->findOrFail($id);
85 $this->checkOwnablePermission('chapter-update', $chapter);
87 $updatedChapter = $this->chapterRepo->update($chapter, $request->all());
89 return response()->json($updatedChapter->load(['tags']));
94 * This will typically send the chapter to the recycle bin.
96 public function delete(string $id)
98 $chapter = Chapter::visible()->findOrFail($id);
99 $this->checkOwnablePermission('chapter-delete', $chapter);
101 $this->chapterRepo->destroy($chapter);
103 return response('', 204);