1 <?php namespace BookStack\Http\Controllers\Api;
3 use BookStack\Actions\ActivityType;
4 use BookStack\Entities\Book;
5 use BookStack\Entities\Chapter;
6 use BookStack\Entities\Repos\ChapterRepo;
7 use BookStack\Facades\Activity;
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();
44 return $this->apiListingResponse($chapters, [
45 'id', 'book_id', 'name', 'slug', 'description', 'priority',
46 'created_at', 'updated_at', 'created_by', 'updated_by',
51 * Create a new chapter in the system.
53 public function create(Request $request)
55 $this->validate($request, $this->rules['create']);
57 $bookId = $request->get('book_id');
58 $book = Book::visible()->findOrFail($bookId);
59 $this->checkOwnablePermission('chapter-create', $book);
61 $chapter = $this->chapterRepo->create($request->all(), $book);
62 return response()->json($chapter->load(['tags']));
66 * View the details of a single chapter.
68 public function read(string $id)
70 $chapter = Chapter::visible()->with(['tags', 'createdBy', 'updatedBy', 'pages' => function (HasMany $query) {
71 $query->visible()->get(['id', 'name', 'slug']);
73 return response()->json($chapter);
77 * Update the details of a single chapter.
79 public function update(Request $request, string $id)
81 $chapter = Chapter::visible()->findOrFail($id);
82 $this->checkOwnablePermission('chapter-update', $chapter);
84 $updatedChapter = $this->chapterRepo->update($chapter, $request->all());
85 return response()->json($updatedChapter->load(['tags']));
89 * Delete a chapter from the system.
91 public function delete(string $id)
93 $chapter = Chapter::visible()->findOrFail($id);
94 $this->checkOwnablePermission('chapter-delete', $chapter);
96 $this->chapterRepo->destroy($chapter);
97 return response('', 204);