55use BookStack \Activity \Models \View ;
66use BookStack \Activity \Tools \UserEntityWatchOptions ;
77use BookStack \Entities \Models \Book ;
8+ use BookStack \Entities \Queries \ChapterQueries ;
9+ use BookStack \Entities \Queries \EntityQueries ;
810use BookStack \Entities \Repos \ChapterRepo ;
911use BookStack \Entities \Tools \BookContents ;
1012use BookStack \Entities \Tools \Cloner ;
@@ -24,7 +26,9 @@ class ChapterController extends Controller
2426{
2527 public function __construct (
2628 protected ChapterRepo $ chapterRepo ,
27- protected ReferenceFetcher $ referenceFetcher
29+ protected ChapterQueries $ queries ,
30+ protected EntityQueries $ entityQueries ,
31+ protected ReferenceFetcher $ referenceFetcher ,
2832 ) {
2933 }
3034
@@ -33,12 +37,15 @@ public function __construct(
3337 */
3438 public function create (string $ bookSlug )
3539 {
36- $ book = Book:: visible ()-> where ( ' slug ' , ' = ' , $ bookSlug )-> firstOrFail ( );
40+ $ book = $ this -> entityQueries -> books -> findVisibleBySlug ( $ bookSlug );
3741 $ this ->checkOwnablePermission ('chapter-create ' , $ book );
3842
3943 $ this ->setPageTitle (trans ('entities.chapters_create ' ));
4044
41- return view ('chapters.create ' , ['book ' => $ book , 'current ' => $ book ]);
45+ return view ('chapters.create ' , [
46+ 'book ' => $ book ,
47+ 'current ' => $ book ,
48+ ]);
4249 }
4350
4451 /**
@@ -55,7 +62,7 @@ public function store(Request $request, string $bookSlug)
5562 'default_template_id ' => ['nullable ' , 'integer ' ],
5663 ]);
5764
58- $ book = Book:: visible ()-> where ( ' slug ' , ' = ' , $ bookSlug )-> firstOrFail ( );
65+ $ book = $ this -> entityQueries -> books -> findVisibleBySlug ( $ bookSlug );
5966 $ this ->checkOwnablePermission ('chapter-create ' , $ book );
6067
6168 $ chapter = $ this ->chapterRepo ->create ($ validated , $ book );
@@ -68,7 +75,7 @@ public function store(Request $request, string $bookSlug)
6875 */
6976 public function show (string $ bookSlug , string $ chapterSlug )
7077 {
71- $ chapter = $ this ->chapterRepo -> getBySlug ($ bookSlug , $ chapterSlug );
78+ $ chapter = $ this ->queries -> findVisibleBySlugs ($ bookSlug , $ chapterSlug );
7279 $ this ->checkOwnablePermission ('chapter-view ' , $ chapter );
7380
7481 $ sidebarTree = (new BookContents ($ chapter ->book ))->getTree ();
@@ -96,7 +103,7 @@ public function show(string $bookSlug, string $chapterSlug)
96103 */
97104 public function edit (string $ bookSlug , string $ chapterSlug )
98105 {
99- $ chapter = $ this ->chapterRepo -> getBySlug ($ bookSlug , $ chapterSlug );
106+ $ chapter = $ this ->queries -> findVisibleBySlugs ($ bookSlug , $ chapterSlug );
100107 $ this ->checkOwnablePermission ('chapter-update ' , $ chapter );
101108
102109 $ this ->setPageTitle (trans ('entities.chapters_edit_named ' , ['chapterName ' => $ chapter ->getShortName ()]));
@@ -118,7 +125,7 @@ public function update(Request $request, string $bookSlug, string $chapterSlug)
118125 'default_template_id ' => ['nullable ' , 'integer ' ],
119126 ]);
120127
121- $ chapter = $ this ->chapterRepo -> getBySlug ($ bookSlug , $ chapterSlug );
128+ $ chapter = $ this ->queries -> findVisibleBySlugs ($ bookSlug , $ chapterSlug );
122129 $ this ->checkOwnablePermission ('chapter-update ' , $ chapter );
123130
124131 $ this ->chapterRepo ->update ($ chapter , $ validated );
@@ -133,7 +140,7 @@ public function update(Request $request, string $bookSlug, string $chapterSlug)
133140 */
134141 public function showDelete (string $ bookSlug , string $ chapterSlug )
135142 {
136- $ chapter = $ this ->chapterRepo -> getBySlug ($ bookSlug , $ chapterSlug );
143+ $ chapter = $ this ->queries -> findVisibleBySlugs ($ bookSlug , $ chapterSlug );
137144 $ this ->checkOwnablePermission ('chapter-delete ' , $ chapter );
138145
139146 $ this ->setPageTitle (trans ('entities.chapters_delete_named ' , ['chapterName ' => $ chapter ->getShortName ()]));
@@ -149,7 +156,7 @@ public function showDelete(string $bookSlug, string $chapterSlug)
149156 */
150157 public function destroy (string $ bookSlug , string $ chapterSlug )
151158 {
152- $ chapter = $ this ->chapterRepo -> getBySlug ($ bookSlug , $ chapterSlug );
159+ $ chapter = $ this ->queries -> findVisibleBySlugs ($ bookSlug , $ chapterSlug );
153160 $ this ->checkOwnablePermission ('chapter-delete ' , $ chapter );
154161
155162 $ this ->chapterRepo ->destroy ($ chapter );
@@ -164,7 +171,7 @@ public function destroy(string $bookSlug, string $chapterSlug)
164171 */
165172 public function showMove (string $ bookSlug , string $ chapterSlug )
166173 {
167- $ chapter = $ this ->chapterRepo -> getBySlug ($ bookSlug , $ chapterSlug );
174+ $ chapter = $ this ->queries -> findVisibleBySlugs ($ bookSlug , $ chapterSlug );
168175 $ this ->setPageTitle (trans ('entities.chapters_move_named ' , ['chapterName ' => $ chapter ->getShortName ()]));
169176 $ this ->checkOwnablePermission ('chapter-update ' , $ chapter );
170177 $ this ->checkOwnablePermission ('chapter-delete ' , $ chapter );
@@ -182,7 +189,7 @@ public function showMove(string $bookSlug, string $chapterSlug)
182189 */
183190 public function move (Request $ request , string $ bookSlug , string $ chapterSlug )
184191 {
185- $ chapter = $ this ->chapterRepo -> getBySlug ($ bookSlug , $ chapterSlug );
192+ $ chapter = $ this ->queries -> findVisibleBySlugs ($ bookSlug , $ chapterSlug );
186193 $ this ->checkOwnablePermission ('chapter-update ' , $ chapter );
187194 $ this ->checkOwnablePermission ('chapter-delete ' , $ chapter );
188195
@@ -211,7 +218,7 @@ public function move(Request $request, string $bookSlug, string $chapterSlug)
211218 */
212219 public function showCopy (string $ bookSlug , string $ chapterSlug )
213220 {
214- $ chapter = $ this ->chapterRepo -> getBySlug ($ bookSlug , $ chapterSlug );
221+ $ chapter = $ this ->queries -> findVisibleBySlugs ($ bookSlug , $ chapterSlug );
215222 $ this ->checkOwnablePermission ('chapter-view ' , $ chapter );
216223
217224 session ()->flashInput (['name ' => $ chapter ->name ]);
@@ -230,13 +237,13 @@ public function showCopy(string $bookSlug, string $chapterSlug)
230237 */
231238 public function copy (Request $ request , Cloner $ cloner , string $ bookSlug , string $ chapterSlug )
232239 {
233- $ chapter = $ this ->chapterRepo -> getBySlug ($ bookSlug , $ chapterSlug );
240+ $ chapter = $ this ->queries -> findVisibleBySlugs ($ bookSlug , $ chapterSlug );
234241 $ this ->checkOwnablePermission ('chapter-view ' , $ chapter );
235242
236243 $ entitySelection = $ request ->get ('entity_selection ' ) ?: null ;
237- $ newParentBook = $ entitySelection ? $ this ->chapterRepo -> findParentByIdentifier ($ entitySelection ) : $ chapter ->getParent ();
244+ $ newParentBook = $ entitySelection ? $ this ->entityQueries -> findVisibleByStringIdentifier ($ entitySelection ) : $ chapter ->getParent ();
238245
239- if (is_null ( $ newParentBook) ) {
246+ if (! $ newParentBook instanceof Book ) {
240247 $ this ->showErrorNotification (trans ('errors.selected_book_not_found ' ));
241248
242249 return redirect ($ chapter ->getUrl ('/copy ' ));
@@ -256,7 +263,7 @@ public function copy(Request $request, Cloner $cloner, string $bookSlug, string
256263 */
257264 public function convertToBook (HierarchyTransformer $ transformer , string $ bookSlug , string $ chapterSlug )
258265 {
259- $ chapter = $ this ->chapterRepo -> getBySlug ($ bookSlug , $ chapterSlug );
266+ $ chapter = $ this ->queries -> findVisibleBySlugs ($ bookSlug , $ chapterSlug );
260267 $ this ->checkOwnablePermission ('chapter-update ' , $ chapter );
261268 $ this ->checkOwnablePermission ('chapter-delete ' , $ chapter );
262269 $ this ->checkPermission ('book-create-all ' );
0 commit comments