Skip to content

Commit 222c665

Browse files
committed
Queries: Extracted PageRepo queries to own class
Started new class for PageRevisions too as part of these changes
1 parent 8e78b4c commit 222c665

21 files changed

+219
-196
lines changed

app/Entities/Controllers/BookController.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public function store(Request $request, string $shelfSlug = null)
124124
*/
125125
public function show(Request $request, ActivityQueries $activities, string $slug)
126126
{
127-
$book = $this->queries->findVisibleBySlug($slug);
127+
$book = $this->queries->findVisibleBySlugOrFail($slug);
128128
$bookChildren = (new BookContents($book))->getTree(true);
129129
$bookParentShelves = $book->shelves()->scopes('visible')->get();
130130

@@ -151,7 +151,7 @@ public function show(Request $request, ActivityQueries $activities, string $slug
151151
*/
152152
public function edit(string $slug)
153153
{
154-
$book = $this->queries->findVisibleBySlug($slug);
154+
$book = $this->queries->findVisibleBySlugOrFail($slug);
155155
$this->checkOwnablePermission('book-update', $book);
156156
$this->setPageTitle(trans('entities.books_edit_named', ['bookName' => $book->getShortName()]));
157157

@@ -167,7 +167,7 @@ public function edit(string $slug)
167167
*/
168168
public function update(Request $request, string $slug)
169169
{
170-
$book = $this->queries->findVisibleBySlug($slug);
170+
$book = $this->queries->findVisibleBySlugOrFail($slug);
171171
$this->checkOwnablePermission('book-update', $book);
172172

173173
$validated = $this->validate($request, [
@@ -194,7 +194,7 @@ public function update(Request $request, string $slug)
194194
*/
195195
public function showDelete(string $bookSlug)
196196
{
197-
$book = $this->queries->findVisibleBySlug($bookSlug);
197+
$book = $this->queries->findVisibleBySlugOrFail($bookSlug);
198198
$this->checkOwnablePermission('book-delete', $book);
199199
$this->setPageTitle(trans('entities.books_delete_named', ['bookName' => $book->getShortName()]));
200200

@@ -208,7 +208,7 @@ public function showDelete(string $bookSlug)
208208
*/
209209
public function destroy(string $bookSlug)
210210
{
211-
$book = $this->queries->findVisibleBySlug($bookSlug);
211+
$book = $this->queries->findVisibleBySlugOrFail($bookSlug);
212212
$this->checkOwnablePermission('book-delete', $book);
213213

214214
$this->bookRepo->destroy($book);
@@ -223,7 +223,7 @@ public function destroy(string $bookSlug)
223223
*/
224224
public function showCopy(string $bookSlug)
225225
{
226-
$book = $this->queries->findVisibleBySlug($bookSlug);
226+
$book = $this->queries->findVisibleBySlugOrFail($bookSlug);
227227
$this->checkOwnablePermission('book-view', $book);
228228

229229
session()->flashInput(['name' => $book->name]);
@@ -240,7 +240,7 @@ public function showCopy(string $bookSlug)
240240
*/
241241
public function copy(Request $request, Cloner $cloner, string $bookSlug)
242242
{
243-
$book = $this->queries->findVisibleBySlug($bookSlug);
243+
$book = $this->queries->findVisibleBySlugOrFail($bookSlug);
244244
$this->checkOwnablePermission('book-view', $book);
245245
$this->checkPermission('book-create-all');
246246

@@ -256,7 +256,7 @@ public function copy(Request $request, Cloner $cloner, string $bookSlug)
256256
*/
257257
public function convertToShelf(HierarchyTransformer $transformer, string $bookSlug)
258258
{
259-
$book = $this->queries->findVisibleBySlug($bookSlug);
259+
$book = $this->queries->findVisibleBySlugOrFail($bookSlug);
260260
$this->checkOwnablePermission('book-update', $book);
261261
$this->checkOwnablePermission('book-delete', $book);
262262
$this->checkPermission('bookshelf-create-all');

app/Entities/Controllers/BookExportController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function __construct(
2323
*/
2424
public function pdf(string $bookSlug)
2525
{
26-
$book = $this->queries->findVisibleBySlug($bookSlug);
26+
$book = $this->queries->findVisibleBySlugOrFail($bookSlug);
2727
$pdfContent = $this->exportFormatter->bookToPdf($book);
2828

2929
return $this->download()->directly($pdfContent, $bookSlug . '.pdf');
@@ -36,7 +36,7 @@ public function pdf(string $bookSlug)
3636
*/
3737
public function html(string $bookSlug)
3838
{
39-
$book = $this->queries->findVisibleBySlug($bookSlug);
39+
$book = $this->queries->findVisibleBySlugOrFail($bookSlug);
4040
$htmlContent = $this->exportFormatter->bookToContainedHtml($book);
4141

4242
return $this->download()->directly($htmlContent, $bookSlug . '.html');
@@ -47,7 +47,7 @@ public function html(string $bookSlug)
4747
*/
4848
public function plainText(string $bookSlug)
4949
{
50-
$book = $this->queries->findVisibleBySlug($bookSlug);
50+
$book = $this->queries->findVisibleBySlugOrFail($bookSlug);
5151
$textContent = $this->exportFormatter->bookToPlainText($book);
5252

5353
return $this->download()->directly($textContent, $bookSlug . '.txt');
@@ -58,7 +58,7 @@ public function plainText(string $bookSlug)
5858
*/
5959
public function markdown(string $bookSlug)
6060
{
61-
$book = $this->queries->findVisibleBySlug($bookSlug);
61+
$book = $this->queries->findVisibleBySlugOrFail($bookSlug);
6262
$textContent = $this->exportFormatter->bookToMarkdown($book);
6363

6464
return $this->download()->directly($textContent, $bookSlug . '.md');

app/Entities/Controllers/BookSortController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct(
2222
*/
2323
public function show(string $bookSlug)
2424
{
25-
$book = $this->queries->findVisibleBySlug($bookSlug);
25+
$book = $this->queries->findVisibleBySlugOrFail($bookSlug);
2626
$this->checkOwnablePermission('book-update', $book);
2727

2828
$bookChildren = (new BookContents($book))->getTree(false);
@@ -38,7 +38,7 @@ public function show(string $bookSlug)
3838
*/
3939
public function showItem(string $bookSlug)
4040
{
41-
$book = $this->queries->findVisibleBySlug($bookSlug);
41+
$book = $this->queries->findVisibleBySlugOrFail($bookSlug);
4242
$bookChildren = (new BookContents($book))->getTree();
4343

4444
return view('books.parts.sort-box', ['book' => $book, 'bookChildren' => $bookChildren]);
@@ -49,7 +49,7 @@ public function showItem(string $bookSlug)
4949
*/
5050
public function update(Request $request, string $bookSlug)
5151
{
52-
$book = $this->queries->findVisibleBySlug($bookSlug);
52+
$book = $this->queries->findVisibleBySlugOrFail($bookSlug);
5353
$this->checkOwnablePermission('book-update', $book);
5454

5555
// Return if no map sent

app/Entities/Controllers/BookshelfController.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function store(Request $request)
103103
*/
104104
public function show(Request $request, ActivityQueries $activities, string $slug)
105105
{
106-
$shelf = $this->queries->findVisibleBySlug($slug);
106+
$shelf = $this->queries->findVisibleBySlugOrFail($slug);
107107
$this->checkOwnablePermission('bookshelf-view', $shelf);
108108

109109
$listOptions = SimpleListOptions::fromRequest($request, 'shelf_books')->withSortOptions([
@@ -141,7 +141,7 @@ public function show(Request $request, ActivityQueries $activities, string $slug
141141
*/
142142
public function edit(string $slug)
143143
{
144-
$shelf = $this->queries->findVisibleBySlug($slug);
144+
$shelf = $this->queries->findVisibleBySlugOrFail($slug);
145145
$this->checkOwnablePermission('bookshelf-update', $shelf);
146146

147147
$shelfBookIds = $shelf->books()->get(['id'])->pluck('id');
@@ -164,7 +164,7 @@ public function edit(string $slug)
164164
*/
165165
public function update(Request $request, string $slug)
166166
{
167-
$shelf = $this->queries->findVisibleBySlug($slug);
167+
$shelf = $this->queries->findVisibleBySlugOrFail($slug);
168168
$this->checkOwnablePermission('bookshelf-update', $shelf);
169169
$validated = $this->validate($request, [
170170
'name' => ['required', 'string', 'max:255'],
@@ -190,7 +190,7 @@ public function update(Request $request, string $slug)
190190
*/
191191
public function showDelete(string $slug)
192192
{
193-
$shelf = $this->queries->findVisibleBySlug($slug);
193+
$shelf = $this->queries->findVisibleBySlugOrFail($slug);
194194
$this->checkOwnablePermission('bookshelf-delete', $shelf);
195195

196196
$this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
@@ -205,7 +205,7 @@ public function showDelete(string $slug)
205205
*/
206206
public function destroy(string $slug)
207207
{
208-
$shelf = $this->queries->findVisibleBySlug($slug);
208+
$shelf = $this->queries->findVisibleBySlugOrFail($slug);
209209
$this->checkOwnablePermission('bookshelf-delete', $shelf);
210210

211211
$this->shelfRepo->destroy($shelf);

app/Entities/Controllers/ChapterController.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function __construct(
3737
*/
3838
public function create(string $bookSlug)
3939
{
40-
$book = $this->entityQueries->books->findVisibleBySlug($bookSlug);
40+
$book = $this->entityQueries->books->findVisibleBySlugOrFail($bookSlug);
4141
$this->checkOwnablePermission('chapter-create', $book);
4242

4343
$this->setPageTitle(trans('entities.chapters_create'));
@@ -62,7 +62,7 @@ public function store(Request $request, string $bookSlug)
6262
'default_template_id' => ['nullable', 'integer'],
6363
]);
6464

65-
$book = $this->entityQueries->books->findVisibleBySlug($bookSlug);
65+
$book = $this->entityQueries->books->findVisibleBySlugOrFail($bookSlug);
6666
$this->checkOwnablePermission('chapter-create', $book);
6767

6868
$chapter = $this->chapterRepo->create($validated, $book);
@@ -75,7 +75,7 @@ public function store(Request $request, string $bookSlug)
7575
*/
7676
public function show(string $bookSlug, string $chapterSlug)
7777
{
78-
$chapter = $this->queries->findVisibleBySlugs($bookSlug, $chapterSlug);
78+
$chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
7979
$this->checkOwnablePermission('chapter-view', $chapter);
8080

8181
$sidebarTree = (new BookContents($chapter->book))->getTree();
@@ -103,7 +103,7 @@ public function show(string $bookSlug, string $chapterSlug)
103103
*/
104104
public function edit(string $bookSlug, string $chapterSlug)
105105
{
106-
$chapter = $this->queries->findVisibleBySlugs($bookSlug, $chapterSlug);
106+
$chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
107107
$this->checkOwnablePermission('chapter-update', $chapter);
108108

109109
$this->setPageTitle(trans('entities.chapters_edit_named', ['chapterName' => $chapter->getShortName()]));
@@ -125,7 +125,7 @@ public function update(Request $request, string $bookSlug, string $chapterSlug)
125125
'default_template_id' => ['nullable', 'integer'],
126126
]);
127127

128-
$chapter = $this->queries->findVisibleBySlugs($bookSlug, $chapterSlug);
128+
$chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
129129
$this->checkOwnablePermission('chapter-update', $chapter);
130130

131131
$this->chapterRepo->update($chapter, $validated);
@@ -140,7 +140,7 @@ public function update(Request $request, string $bookSlug, string $chapterSlug)
140140
*/
141141
public function showDelete(string $bookSlug, string $chapterSlug)
142142
{
143-
$chapter = $this->queries->findVisibleBySlugs($bookSlug, $chapterSlug);
143+
$chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
144144
$this->checkOwnablePermission('chapter-delete', $chapter);
145145

146146
$this->setPageTitle(trans('entities.chapters_delete_named', ['chapterName' => $chapter->getShortName()]));
@@ -156,7 +156,7 @@ public function showDelete(string $bookSlug, string $chapterSlug)
156156
*/
157157
public function destroy(string $bookSlug, string $chapterSlug)
158158
{
159-
$chapter = $this->queries->findVisibleBySlugs($bookSlug, $chapterSlug);
159+
$chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
160160
$this->checkOwnablePermission('chapter-delete', $chapter);
161161

162162
$this->chapterRepo->destroy($chapter);
@@ -171,7 +171,7 @@ public function destroy(string $bookSlug, string $chapterSlug)
171171
*/
172172
public function showMove(string $bookSlug, string $chapterSlug)
173173
{
174-
$chapter = $this->queries->findVisibleBySlugs($bookSlug, $chapterSlug);
174+
$chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
175175
$this->setPageTitle(trans('entities.chapters_move_named', ['chapterName' => $chapter->getShortName()]));
176176
$this->checkOwnablePermission('chapter-update', $chapter);
177177
$this->checkOwnablePermission('chapter-delete', $chapter);
@@ -189,7 +189,7 @@ public function showMove(string $bookSlug, string $chapterSlug)
189189
*/
190190
public function move(Request $request, string $bookSlug, string $chapterSlug)
191191
{
192-
$chapter = $this->queries->findVisibleBySlugs($bookSlug, $chapterSlug);
192+
$chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
193193
$this->checkOwnablePermission('chapter-update', $chapter);
194194
$this->checkOwnablePermission('chapter-delete', $chapter);
195195

@@ -218,7 +218,7 @@ public function move(Request $request, string $bookSlug, string $chapterSlug)
218218
*/
219219
public function showCopy(string $bookSlug, string $chapterSlug)
220220
{
221-
$chapter = $this->queries->findVisibleBySlugs($bookSlug, $chapterSlug);
221+
$chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
222222
$this->checkOwnablePermission('chapter-view', $chapter);
223223

224224
session()->flashInput(['name' => $chapter->name]);
@@ -237,7 +237,7 @@ public function showCopy(string $bookSlug, string $chapterSlug)
237237
*/
238238
public function copy(Request $request, Cloner $cloner, string $bookSlug, string $chapterSlug)
239239
{
240-
$chapter = $this->queries->findVisibleBySlugs($bookSlug, $chapterSlug);
240+
$chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
241241
$this->checkOwnablePermission('chapter-view', $chapter);
242242

243243
$entitySelection = $request->get('entity_selection') ?: null;
@@ -263,7 +263,7 @@ public function copy(Request $request, Cloner $cloner, string $bookSlug, string
263263
*/
264264
public function convertToBook(HierarchyTransformer $transformer, string $bookSlug, string $chapterSlug)
265265
{
266-
$chapter = $this->queries->findVisibleBySlugs($bookSlug, $chapterSlug);
266+
$chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
267267
$this->checkOwnablePermission('chapter-update', $chapter);
268268
$this->checkOwnablePermission('chapter-delete', $chapter);
269269
$this->checkPermission('book-create-all');

app/Entities/Controllers/ChapterExportController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __construct(
2525
*/
2626
public function pdf(string $bookSlug, string $chapterSlug)
2727
{
28-
$chapter = $this->queries->findVisibleBySlugs($bookSlug, $chapterSlug);
28+
$chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
2929
$pdfContent = $this->exportFormatter->chapterToPdf($chapter);
3030

3131
return $this->download()->directly($pdfContent, $chapterSlug . '.pdf');
@@ -39,7 +39,7 @@ public function pdf(string $bookSlug, string $chapterSlug)
3939
*/
4040
public function html(string $bookSlug, string $chapterSlug)
4141
{
42-
$chapter = $this->queries->findVisibleBySlugs($bookSlug, $chapterSlug);
42+
$chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
4343
$containedHtml = $this->exportFormatter->chapterToContainedHtml($chapter);
4444

4545
return $this->download()->directly($containedHtml, $chapterSlug . '.html');
@@ -52,7 +52,7 @@ public function html(string $bookSlug, string $chapterSlug)
5252
*/
5353
public function plainText(string $bookSlug, string $chapterSlug)
5454
{
55-
$chapter = $this->queries->findVisibleBySlugs($bookSlug, $chapterSlug);
55+
$chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
5656
$chapterText = $this->exportFormatter->chapterToPlainText($chapter);
5757

5858
return $this->download()->directly($chapterText, $chapterSlug . '.txt');
@@ -65,7 +65,7 @@ public function plainText(string $bookSlug, string $chapterSlug)
6565
*/
6666
public function markdown(string $bookSlug, string $chapterSlug)
6767
{
68-
$chapter = $this->queries->findVisibleBySlugs($bookSlug, $chapterSlug);
68+
$chapter = $this->queries->findVisibleBySlugsOrFail($bookSlug, $chapterSlug);
6969
$chapterText = $this->exportFormatter->chapterToMarkdown($chapter);
7070

7171
return $this->download()->directly($chapterText, $chapterSlug . '.md');

app/Entities/Controllers/PageApiController.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use BookStack\Entities\Models\Book;
66
use BookStack\Entities\Models\Chapter;
77
use BookStack\Entities\Models\Page;
8+
use BookStack\Entities\Queries\PageQueries;
89
use BookStack\Entities\Repos\PageRepo;
910
use BookStack\Exceptions\PermissionsException;
1011
use BookStack\Http\ApiController;
@@ -35,7 +36,8 @@ class PageApiController extends ApiController
3536
];
3637

3738
public function __construct(
38-
protected PageRepo $pageRepo
39+
protected PageRepo $pageRepo,
40+
protected PageQueries $queries,
3941
) {
4042
}
4143

@@ -97,7 +99,7 @@ public function create(Request $request)
9799
*/
98100
public function read(string $id)
99101
{
100-
$page = $this->pageRepo->getById($id, []);
102+
$page = $this->queries->findVisibleByIdOrFail($id);
101103

102104
return response()->json($page->forJsonDisplay());
103105
}
@@ -113,7 +115,7 @@ public function update(Request $request, string $id)
113115
{
114116
$requestData = $this->validate($request, $this->rules['update']);
115117

116-
$page = $this->pageRepo->getById($id, []);
118+
$page = $this->queries->findVisibleByIdOrFail($id);
117119
$this->checkOwnablePermission('page-update', $page);
118120

119121
$parent = null;
@@ -148,7 +150,7 @@ public function update(Request $request, string $id)
148150
*/
149151
public function delete(string $id)
150152
{
151-
$page = $this->pageRepo->getById($id, []);
153+
$page = $this->queries->findVisibleByIdOrFail($id);
152154
$this->checkOwnablePermission('page-delete', $page);
153155

154156
$this->pageRepo->destroy($page);

0 commit comments

Comments
 (0)