]> BookStack Code Mirror - bookstack/blob - app/Entities/Controllers/ChapterApiController.php
Input WYSIWYG: Aligned newline handling with old descriptions
[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\Exceptions\PermissionsException;
9 use BookStack\Http\ApiController;
10 use Exception;
11 use Illuminate\Database\Eloquent\Relations\HasMany;
12 use Illuminate\Http\Request;
13
14 class ChapterApiController extends ApiController
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             'priority'    => ['integer'],
23         ],
24         'update' => [
25             'book_id'     => ['integer'],
26             'name'        => ['string', 'min:1', 'max:255'],
27             'description' => ['string', 'max:1000'],
28             'tags'        => ['array'],
29             'priority'    => ['integer'],
30         ],
31     ];
32
33     public function __construct(
34         protected ChapterRepo $chapterRepo
35     ) {
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         $requestData = $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($requestData, $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->scopes('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      * Providing a 'book_id' property will essentially move the chapter
82      * into that parent element if you have permissions to do so.
83      */
84     public function update(Request $request, string $id)
85     {
86         $requestData = $this->validate($request, $this->rules()['update']);
87         $chapter = Chapter::visible()->findOrFail($id);
88         $this->checkOwnablePermission('chapter-update', $chapter);
89
90         if ($request->has('book_id') && $chapter->book_id !== intval($requestData['book_id'])) {
91             $this->checkOwnablePermission('chapter-delete', $chapter);
92
93             try {
94                 $this->chapterRepo->move($chapter, "book:{$requestData['book_id']}");
95             } catch (Exception $exception) {
96                 if ($exception instanceof  PermissionsException) {
97                     $this->showPermissionError();
98                 }
99
100                 return $this->jsonError(trans('errors.selected_book_not_found'));
101             }
102         }
103
104         $updatedChapter = $this->chapterRepo->update($chapter, $requestData);
105
106         return response()->json($updatedChapter->load(['tags']));
107     }
108
109     /**
110      * Delete a chapter.
111      * This will typically send the chapter to the recycle bin.
112      */
113     public function delete(string $id)
114     {
115         $chapter = Chapter::visible()->findOrFail($id);
116         $this->checkOwnablePermission('chapter-delete', $chapter);
117
118         $this->chapterRepo->destroy($chapter);
119
120         return response('', 204);
121     }
122 }