]> BookStack Code Mirror - bookstack/blob - app/Entities/Controllers/ChapterApiController.php
Thumbnails: Fixed thumnail orientation
[bookstack] / app / Entities / Controllers / ChapterApiController.php
1 <?php
2
3 namespace BookStack\Entities\Controllers;
4
5 use BookStack\Entities\Models\Chapter;
6 use BookStack\Entities\Queries\ChapterQueries;
7 use BookStack\Entities\Queries\EntityQueries;
8 use BookStack\Entities\Repos\ChapterRepo;
9 use BookStack\Exceptions\PermissionsException;
10 use BookStack\Http\ApiController;
11 use Exception;
12 use Illuminate\Database\Eloquent\Relations\HasMany;
13 use Illuminate\Http\Request;
14
15 class ChapterApiController extends ApiController
16 {
17     protected $rules = [
18         'create' => [
19             'book_id'             => ['required', 'integer'],
20             'name'                => ['required', 'string', 'max:255'],
21             'description'         => ['string', 'max:1900'],
22             'description_html'    => ['string', 'max:2000'],
23             'tags'                => ['array'],
24             'priority'            => ['integer'],
25             'default_template_id' => ['nullable', 'integer'],
26         ],
27         'update' => [
28             'book_id'             => ['integer'],
29             'name'                => ['string', 'min:1', 'max:255'],
30             'description'         => ['string', 'max:1900'],
31             'description_html'    => ['string', 'max:2000'],
32             'tags'                => ['array'],
33             'priority'            => ['integer'],
34             'default_template_id' => ['nullable', 'integer'],
35         ],
36     ];
37
38     public function __construct(
39         protected ChapterRepo $chapterRepo,
40         protected ChapterQueries $queries,
41         protected EntityQueries $entityQueries,
42     ) {
43     }
44
45     /**
46      * Get a listing of chapters visible to the user.
47      */
48     public function list()
49     {
50         $chapters = $this->queries->visibleForList()
51             ->addSelect(['created_by', 'updated_by']);
52
53         return $this->apiListingResponse($chapters, [
54             'id', 'book_id', 'name', 'slug', 'description', 'priority',
55             'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
56         ]);
57     }
58
59     /**
60      * Create a new chapter in the system.
61      */
62     public function create(Request $request)
63     {
64         $requestData = $this->validate($request, $this->rules['create']);
65
66         $bookId = $request->get('book_id');
67         $book = $this->entityQueries->books->findVisibleByIdOrFail(intval($bookId));
68         $this->checkOwnablePermission('chapter-create', $book);
69
70         $chapter = $this->chapterRepo->create($requestData, $book);
71
72         return response()->json($this->forJsonDisplay($chapter));
73     }
74
75     /**
76      * View the details of a single chapter.
77      */
78     public function read(string $id)
79     {
80         $chapter = $this->queries->findVisibleByIdOrFail(intval($id));
81         $chapter = $this->forJsonDisplay($chapter);
82
83         $chapter->load(['createdBy', 'updatedBy', 'ownedBy']);
84
85         // Note: More fields than usual here, for backwards compatibility,
86         // due to previously accidentally including more fields that desired.
87         $pages = $this->entityQueries->pages->visibleForChapterList($chapter->id)
88             ->addSelect(['created_by', 'updated_by', 'revision_count', 'editor'])
89             ->get();
90         $chapter->setRelation('pages', $pages);
91
92         return response()->json($chapter);
93     }
94
95     /**
96      * Update the details of a single chapter.
97      * Providing a 'book_id' property will essentially move the chapter
98      * into that parent element if you have permissions to do so.
99      */
100     public function update(Request $request, string $id)
101     {
102         $requestData = $this->validate($request, $this->rules()['update']);
103         $chapter = $this->queries->findVisibleByIdOrFail(intval($id));
104         $this->checkOwnablePermission('chapter-update', $chapter);
105
106         if ($request->has('book_id') && $chapter->book_id !== intval($requestData['book_id'])) {
107             $this->checkOwnablePermission('chapter-delete', $chapter);
108
109             try {
110                 $this->chapterRepo->move($chapter, "book:{$requestData['book_id']}");
111             } catch (Exception $exception) {
112                 if ($exception instanceof PermissionsException) {
113                     $this->showPermissionError();
114                 }
115
116                 return $this->jsonError(trans('errors.selected_book_not_found'));
117             }
118         }
119
120         $updatedChapter = $this->chapterRepo->update($chapter, $requestData);
121
122         return response()->json($this->forJsonDisplay($updatedChapter));
123     }
124
125     /**
126      * Delete a chapter.
127      * This will typically send the chapter to the recycle bin.
128      */
129     public function delete(string $id)
130     {
131         $chapter = $this->queries->findVisibleByIdOrFail(intval($id));
132         $this->checkOwnablePermission('chapter-delete', $chapter);
133
134         $this->chapterRepo->destroy($chapter);
135
136         return response('', 204);
137     }
138
139     protected function forJsonDisplay(Chapter $chapter): Chapter
140     {
141         $chapter = clone $chapter;
142         $chapter->unsetRelations()->refresh();
143
144         $chapter->load(['tags']);
145         $chapter->makeVisible('description_html');
146         $chapter->setAttribute('description_html', $chapter->descriptionHtml());
147         $chapter->setAttribute('book_slug', $chapter->book()->first()->slug);
148
149         return $chapter;
150     }
151 }