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