]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Api/ChapterApiController.php
Apply fixes from StyleCI
[bookstack] / app / Http / Controllers / Api / ChapterApiController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Api;
4
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;
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
45         return $this->apiListingResponse($chapters, [
46             'id', 'book_id', 'name', 'slug', 'description', 'priority',
47             'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
48         ]);
49     }
50
51     /**
52      * Create a new chapter in the system.
53      */
54     public function create(Request $request)
55     {
56         $this->validate($request, $this->rules['create']);
57
58         $bookId = $request->get('book_id');
59         $book = Book::visible()->findOrFail($bookId);
60         $this->checkOwnablePermission('chapter-create', $book);
61
62         $chapter = $this->chapterRepo->create($request->all(), $book);
63
64         return response()->json($chapter->load(['tags']));
65     }
66
67     /**
68      * View the details of a single chapter.
69      */
70     public function read(string $id)
71     {
72         $chapter = Chapter::visible()->with(['tags', 'createdBy', 'updatedBy', 'ownedBy', 'pages' => function (HasMany $query) {
73             $query->visible()->get(['id', 'name', 'slug']);
74         }])->findOrFail($id);
75
76         return response()->json($chapter);
77     }
78
79     /**
80      * Update the details of a single chapter.
81      */
82     public function update(Request $request, string $id)
83     {
84         $chapter = Chapter::visible()->findOrFail($id);
85         $this->checkOwnablePermission('chapter-update', $chapter);
86
87         $updatedChapter = $this->chapterRepo->update($chapter, $request->all());
88
89         return response()->json($updatedChapter->load(['tags']));
90     }
91
92     /**
93      * Delete a chapter.
94      * This will typically send the chapter to the recycle bin.
95      */
96     public function delete(string $id)
97     {
98         $chapter = Chapter::visible()->findOrFail($id);
99         $this->checkOwnablePermission('chapter-delete', $chapter);
100
101         $this->chapterRepo->destroy($chapter);
102
103         return response('', 204);
104     }
105 }