Skip to content

Commit 546cfb0

Browse files
committed
Queries: Extracted static page,chapter,shelf queries to classes
1 parent 4834107 commit 546cfb0

15 files changed

+93
-60
lines changed

app/Activity/Controllers/CommentController.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44

55
use BookStack\Activity\CommentRepo;
66
use BookStack\Entities\Models\Page;
7+
use BookStack\Entities\Queries\PageQueries;
78
use BookStack\Http\Controller;
89
use Illuminate\Http\Request;
910
use Illuminate\Validation\ValidationException;
1011

1112
class CommentController extends Controller
1213
{
1314
public function __construct(
14-
protected CommentRepo $commentRepo
15+
protected CommentRepo $commentRepo,
16+
protected PageQueries $pageQueries,
1517
) {
1618
}
1719

@@ -27,7 +29,7 @@ public function savePageComment(Request $request, int $pageId)
2729
'parent_id' => ['nullable', 'integer'],
2830
]);
2931

30-
$page = Page::visible()->find($pageId);
32+
$page = $this->pageQueries->findVisibleById($pageId);
3133
if ($page === null) {
3234
return response('Not found', 404);
3335
}

app/Console/Commands/CopyShelfPermissionsCommand.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace BookStack\Console\Commands;
44

55
use BookStack\Entities\Models\Bookshelf;
6+
use BookStack\Entities\Queries\BookshelfQueries;
67
use BookStack\Entities\Tools\PermissionsUpdater;
78
use Illuminate\Console\Command;
89

@@ -28,7 +29,7 @@ class CopyShelfPermissionsCommand extends Command
2829
/**
2930
* Execute the console command.
3031
*/
31-
public function handle(PermissionsUpdater $permissionsUpdater): int
32+
public function handle(PermissionsUpdater $permissionsUpdater, BookshelfQueries $queries): int
3233
{
3334
$shelfSlug = $this->option('slug');
3435
$cascadeAll = $this->option('all');
@@ -51,11 +52,11 @@ public function handle(PermissionsUpdater $permissionsUpdater): int
5152
return 0;
5253
}
5354

54-
$shelves = Bookshelf::query()->get(['id']);
55+
$shelves = $queries->start()->get(['id']);
5556
}
5657

5758
if ($shelfSlug) {
58-
$shelves = Bookshelf::query()->where('slug', '=', $shelfSlug)->get(['id']);
59+
$shelves = $queries->start()->where('slug', '=', $shelfSlug)->get(['id']);
5960
if ($shelves->count() === 0) {
6061
$this->info('No shelves found with the given slug.');
6162
}

app/Entities/Controllers/BookController.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use BookStack\Activity\Tools\UserEntityWatchOptions;
99
use BookStack\Entities\Models\Bookshelf;
1010
use BookStack\Entities\Queries\BookQueries;
11+
use BookStack\Entities\Queries\BookshelfQueries;
1112
use BookStack\Entities\Repos\BookRepo;
1213
use BookStack\Entities\Tools\BookContents;
1314
use BookStack\Entities\Tools\Cloner;
@@ -29,6 +30,7 @@ public function __construct(
2930
protected ShelfContext $shelfContext,
3031
protected BookRepo $bookRepo,
3132
protected BookQueries $queries,
33+
protected BookshelfQueries $shelfQueries,
3234
protected ReferenceFetcher $referenceFetcher,
3335
) {
3436
}
@@ -75,7 +77,7 @@ public function create(string $shelfSlug = null)
7577

7678
$bookshelf = null;
7779
if ($shelfSlug !== null) {
78-
$bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
80+
$bookshelf = $this->shelfQueries->findVisibleBySlugOrFail($shelfSlug);
7981
$this->checkOwnablePermission('bookshelf-update', $bookshelf);
8082
}
8183

@@ -105,7 +107,7 @@ public function store(Request $request, string $shelfSlug = null)
105107

106108
$bookshelf = null;
107109
if ($shelfSlug !== null) {
108-
$bookshelf = Bookshelf::visible()->where('slug', '=', $shelfSlug)->firstOrFail();
110+
$bookshelf = $this->shelfQueries->findVisibleBySlugOrFail($shelfSlug);
109111
$this->checkOwnablePermission('bookshelf-update', $bookshelf);
110112
}
111113

app/Entities/Controllers/BookshelfApiController.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace BookStack\Entities\Controllers;
44

55
use BookStack\Entities\Models\Bookshelf;
6+
use BookStack\Entities\Queries\BookshelfQueries;
67
use BookStack\Entities\Repos\BookshelfRepo;
78
use BookStack\Http\ApiController;
89
use Exception;
@@ -13,7 +14,8 @@
1314
class BookshelfApiController extends ApiController
1415
{
1516
public function __construct(
16-
protected BookshelfRepo $bookshelfRepo
17+
protected BookshelfRepo $bookshelfRepo,
18+
protected BookshelfQueries $queries,
1719
) {
1820
}
1921

@@ -22,7 +24,7 @@ public function __construct(
2224
*/
2325
public function list()
2426
{
25-
$shelves = Bookshelf::visible();
27+
$shelves = $this->queries->visibleForList();
2628

2729
return $this->apiListingResponse($shelves, [
2830
'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
@@ -54,7 +56,7 @@ public function create(Request $request)
5456
*/
5557
public function read(string $id)
5658
{
57-
$shelf = Bookshelf::visible()->findOrFail($id);
59+
$shelf = $this->queries->findVisibleByIdOrFail(intval($id));
5860
$shelf = $this->forJsonDisplay($shelf);
5961
$shelf->load([
6062
'createdBy', 'updatedBy', 'ownedBy',
@@ -78,7 +80,7 @@ public function read(string $id)
7880
*/
7981
public function update(Request $request, string $id)
8082
{
81-
$shelf = Bookshelf::visible()->findOrFail($id);
83+
$shelf = $this->queries->findVisibleByIdOrFail(intval($id));
8284
$this->checkOwnablePermission('bookshelf-update', $shelf);
8385

8486
$requestData = $this->validate($request, $this->rules()['update']);
@@ -97,7 +99,7 @@ public function update(Request $request, string $id)
9799
*/
98100
public function delete(string $id)
99101
{
100-
$shelf = Bookshelf::visible()->findOrFail($id);
102+
$shelf = $this->queries->findVisibleByIdOrFail(intval($id));
101103
$this->checkOwnablePermission('bookshelf-delete', $shelf);
102104

103105
$this->bookshelfRepo->destroy($shelf);

app/Entities/Controllers/ChapterExportApiController.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,17 @@
33
namespace BookStack\Entities\Controllers;
44

55
use BookStack\Entities\Models\Chapter;
6+
use BookStack\Entities\Queries\ChapterQueries;
67
use BookStack\Entities\Tools\ExportFormatter;
78
use BookStack\Http\ApiController;
89
use Throwable;
910

1011
class ChapterExportApiController extends ApiController
1112
{
12-
protected $exportFormatter;
13-
14-
/**
15-
* ChapterExportController constructor.
16-
*/
17-
public function __construct(ExportFormatter $exportFormatter)
18-
{
19-
$this->exportFormatter = $exportFormatter;
13+
public function __construct(
14+
protected ExportFormatter $exportFormatter,
15+
protected ChapterQueries $queries,
16+
) {
2017
$this->middleware('can:content-export');
2118
}
2219

@@ -27,7 +24,7 @@ public function __construct(ExportFormatter $exportFormatter)
2724
*/
2825
public function exportPdf(int $id)
2926
{
30-
$chapter = Chapter::visible()->findOrFail($id);
27+
$chapter = $this->queries->findVisibleByIdOrFail($id);
3128
$pdfContent = $this->exportFormatter->chapterToPdf($chapter);
3229

3330
return $this->download()->directly($pdfContent, $chapter->slug . '.pdf');
@@ -40,7 +37,7 @@ public function exportPdf(int $id)
4037
*/
4138
public function exportHtml(int $id)
4239
{
43-
$chapter = Chapter::visible()->findOrFail($id);
40+
$chapter = $this->queries->findVisibleByIdOrFail($id);
4441
$htmlContent = $this->exportFormatter->chapterToContainedHtml($chapter);
4542

4643
return $this->download()->directly($htmlContent, $chapter->slug . '.html');
@@ -51,7 +48,7 @@ public function exportHtml(int $id)
5148
*/
5249
public function exportPlainText(int $id)
5350
{
54-
$chapter = Chapter::visible()->findOrFail($id);
51+
$chapter = $this->queries->findVisibleByIdOrFail($id);
5552
$textContent = $this->exportFormatter->chapterToPlainText($chapter);
5653

5754
return $this->download()->directly($textContent, $chapter->slug . '.txt');
@@ -62,7 +59,7 @@ public function exportPlainText(int $id)
6259
*/
6360
public function exportMarkdown(int $id)
6461
{
65-
$chapter = Chapter::visible()->findOrFail($id);
62+
$chapter = $this->queries->findVisibleByIdOrFail($id);
6663
$markdown = $this->exportFormatter->chapterToMarkdown($chapter);
6764

6865
return $this->download()->directly($markdown, $chapter->slug . '.md');

app/Entities/Controllers/PageController.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,9 @@ public function showRecentlyUpdated()
359359
$query->scopes('visible');
360360
};
361361

362-
$pages = Page::visible()->with(['updatedBy', 'book' => $visibleBelongsScope, 'chapter' => $visibleBelongsScope])
362+
$pages = $this->queries->visibleForList()
363+
->addSelect('updated_by')
364+
->with(['updatedBy', 'book' => $visibleBelongsScope, 'chapter' => $visibleBelongsScope])
363365
->orderBy('updated_at', 'desc')
364366
->paginate(20)
365367
->setPath(url('/pages/recently-updated'));

app/Entities/Controllers/PageExportApiController.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
namespace BookStack\Entities\Controllers;
44

55
use BookStack\Entities\Models\Page;
6+
use BookStack\Entities\Queries\PageQueries;
67
use BookStack\Entities\Tools\ExportFormatter;
78
use BookStack\Http\ApiController;
89
use Throwable;
910

1011
class PageExportApiController extends ApiController
1112
{
12-
protected $exportFormatter;
13-
14-
public function __construct(ExportFormatter $exportFormatter)
15-
{
16-
$this->exportFormatter = $exportFormatter;
13+
public function __construct(
14+
protected ExportFormatter $exportFormatter,
15+
protected PageQueries $queries,
16+
) {
1717
$this->middleware('can:content-export');
1818
}
1919

@@ -24,7 +24,7 @@ public function __construct(ExportFormatter $exportFormatter)
2424
*/
2525
public function exportPdf(int $id)
2626
{
27-
$page = Page::visible()->findOrFail($id);
27+
$page = $this->queries->findVisibleByIdOrFail($id);
2828
$pdfContent = $this->exportFormatter->pageToPdf($page);
2929

3030
return $this->download()->directly($pdfContent, $page->slug . '.pdf');
@@ -37,7 +37,7 @@ public function exportPdf(int $id)
3737
*/
3838
public function exportHtml(int $id)
3939
{
40-
$page = Page::visible()->findOrFail($id);
40+
$page = $this->queries->findVisibleByIdOrFail($id);
4141
$htmlContent = $this->exportFormatter->pageToContainedHtml($page);
4242

4343
return $this->download()->directly($htmlContent, $page->slug . '.html');
@@ -48,7 +48,7 @@ public function exportHtml(int $id)
4848
*/
4949
public function exportPlainText(int $id)
5050
{
51-
$page = Page::visible()->findOrFail($id);
51+
$page = $this->queries->findVisibleByIdOrFail($id);
5252
$textContent = $this->exportFormatter->pageToPlainText($page);
5353

5454
return $this->download()->directly($textContent, $page->slug . '.txt');
@@ -59,7 +59,7 @@ public function exportPlainText(int $id)
5959
*/
6060
public function exportMarkdown(int $id)
6161
{
62-
$page = Page::visible()->findOrFail($id);
62+
$page = $this->queries->findVisibleByIdOrFail($id);
6363
$markdown = $this->exportFormatter->pageToMarkdown($page);
6464

6565
return $this->download()->directly($markdown, $page->slug . '.md');

app/Entities/Queries/BookshelfQueries.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ public function findVisibleById(int $id): ?Bookshelf
1818
return $this->start()->scopes('visible')->find($id);
1919
}
2020

21+
public function findVisibleByIdOrFail(int $id): Bookshelf
22+
{
23+
$shelf = $this->findVisibleById($id);
24+
25+
if (is_null($shelf)) {
26+
throw new NotFoundException(trans('errors.bookshelf_not_found'));
27+
}
28+
29+
return $shelf;
30+
}
31+
2132
public function findVisibleBySlugOrFail(string $slug): Bookshelf
2233
{
2334
/** @var ?Bookshelf $shelf */

app/Entities/Repos/BaseRepo.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use BookStack\Entities\Models\HasCoverImage;
1010
use BookStack\Entities\Models\HasHtmlDescription;
1111
use BookStack\Entities\Models\Page;
12+
use BookStack\Entities\Queries\PageQueries;
1213
use BookStack\Exceptions\ImageUploadException;
1314
use BookStack\References\ReferenceStore;
1415
use BookStack\References\ReferenceUpdater;
@@ -23,6 +24,7 @@ public function __construct(
2324
protected ImageRepo $imageRepo,
2425
protected ReferenceUpdater $referenceUpdater,
2526
protected ReferenceStore $referenceStore,
27+
protected PageQueries $pageQueries,
2628
) {
2729
}
2830

@@ -125,8 +127,7 @@ public function updateDefaultTemplate(Book|Chapter $entity, int $templateId): vo
125127
return;
126128
}
127129

128-
$templateExists = Page::query()->visible()
129-
->where('template', '=', true)
130+
$templateExists = $this->pageQueries->visibleTemplates()
130131
->where('id', '=', $templateId)
131132
->exists();
132133

app/Entities/Tools/PageContent.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace BookStack\Entities\Tools;
44

55
use BookStack\Entities\Models\Page;
6+
use BookStack\Entities\Queries\PageQueries;
67
use BookStack\Entities\Tools\Markdown\MarkdownToHtml;
78
use BookStack\Exceptions\ImageUploadException;
89
use BookStack\Facades\Theme;
@@ -21,9 +22,12 @@
2122

2223
class PageContent
2324
{
25+
protected PageQueries $pageQueries;
26+
2427
public function __construct(
2528
protected Page $page
2629
) {
30+
$this->pageQueries = app()->make(PageQueries::class);
2731
}
2832

2933
/**
@@ -331,7 +335,7 @@ protected function getContentProviderClosure(bool $blankIncludes): Closure
331335
return PageIncludeContent::fromHtmlAndTag('', $tag);
332336
}
333337

334-
$matchedPage = Page::visible()->find($tag->getPageId());
338+
$matchedPage = $this->pageQueries->findVisibleById($tag->getPageId());
335339
$content = PageIncludeContent::fromHtmlAndTag($matchedPage->html ?? '', $tag);
336340

337341
if (Theme::hasListeners(ThemeEvents::PAGE_INCLUDE_PARSE)) {

0 commit comments

Comments
 (0)