]> BookStack Code Mirror - bookstack/blob - app/Entities/Controllers/ChapterApiController.php
Cleaned up namespacing in routes
[bookstack] / app / Entities / Controllers / ChapterApiController.php
1 <?php
2
3 namespace BookStack\Entities\Controllers;
4
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;
11
12 class ChapterApiController extends ApiController
13 {
14     protected $chapterRepo;
15
16     protected $rules = [
17         'create' => [
18             'book_id'     => ['required', 'integer'],
19             'name'        => ['required', 'string', 'max:255'],
20             'description' => ['string', 'max:1000'],
21             'tags'        => ['array'],
22         ],
23         'update' => [
24             'book_id'     => ['integer'],
25             'name'        => ['string', 'min:1', 'max:255'],
26             'description' => ['string', 'max:1000'],
27             'tags'        => ['array'],
28         ],
29     ];
30
31     /**
32      * ChapterController constructor.
33      */
34     public function __construct(ChapterRepo $chapterRepo)
35     {
36         $this->chapterRepo = $chapterRepo;
37     }
38
39     /**
40      * Get a listing of chapters visible to the user.
41      */
42     public function list()
43     {
44         $chapters = Chapter::visible();
45
46         return $this->apiListingResponse($chapters, [
47             'id', 'book_id', 'name', 'slug', 'description', 'priority',
48             'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
49         ]);
50     }
51
52     /**
53      * Create a new chapter in the system.
54      */
55     public function create(Request $request)
56     {
57         $this->validate($request, $this->rules['create']);
58
59         $bookId = $request->get('book_id');
60         $book = Book::visible()->findOrFail($bookId);
61         $this->checkOwnablePermission('chapter-create', $book);
62
63         $chapter = $this->chapterRepo->create($request->all(), $book);
64
65         return response()->json($chapter->load(['tags']));
66     }
67
68     /**
69      * View the details of a single chapter.
70      */
71     public function read(string $id)
72     {
73         $chapter = Chapter::visible()->with(['tags', 'createdBy', 'updatedBy', 'ownedBy', 'pages' => function (HasMany $query) {
74             $query->scopes('visible')->get(['id', 'name', 'slug']);
75         }])->findOrFail($id);
76
77         return response()->json($chapter);
78     }
79
80     /**
81      * Update the details of a single chapter.
82      */
83     public function update(Request $request, string $id)
84     {
85         $chapter = Chapter::visible()->findOrFail($id);
86         $this->checkOwnablePermission('chapter-update', $chapter);
87
88         $updatedChapter = $this->chapterRepo->update($chapter, $request->all());
89
90         return response()->json($updatedChapter->load(['tags']));
91     }
92
93     /**
94      * Delete a chapter.
95      * This will typically send the chapter to the recycle bin.
96      */
97     public function delete(string $id)
98     {
99         $chapter = Chapter::visible()->findOrFail($id);
100         $this->checkOwnablePermission('chapter-delete', $chapter);
101
102         $this->chapterRepo->destroy($chapter);
103
104         return response('', 204);
105     }
106 }