Skip to content

Commit 4834107

Browse files
committed
Queries: Updated all app book static query uses
1 parent c95f4ca commit 4834107

37 files changed

+278
-162
lines changed

app/App/HomeController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function index(Request $request, ActivityQueries $activities)
4040
$recentFactor = count($draftPages) > 0 ? 0.5 : 1;
4141
$recents = $this->isSignedIn() ?
4242
(new RecentlyViewed())->run(12 * $recentFactor, 1)
43-
: Book::visible()->orderBy('created_at', 'desc')->take(12 * $recentFactor)->get();
43+
: $this->queries->books->visibleForList()->orderBy('created_at', 'desc')->take(12 * $recentFactor)->get();
4444
$favourites = (new TopFavourites())->run(6);
4545
$recentlyUpdatedPages = $this->queries->pages->visibleForList()
4646
->where('draft', false)

app/Entities/Controllers/BookApiController.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use BookStack\Entities\Models\Book;
77
use BookStack\Entities\Models\Chapter;
88
use BookStack\Entities\Models\Entity;
9+
use BookStack\Entities\Queries\BookQueries;
910
use BookStack\Entities\Repos\BookRepo;
1011
use BookStack\Entities\Tools\BookContents;
1112
use BookStack\Http\ApiController;
@@ -15,7 +16,8 @@
1516
class BookApiController extends ApiController
1617
{
1718
public function __construct(
18-
protected BookRepo $bookRepo
19+
protected BookRepo $bookRepo,
20+
protected BookQueries $queries,
1921
) {
2022
}
2123

@@ -24,7 +26,7 @@ public function __construct(
2426
*/
2527
public function list()
2628
{
27-
$books = Book::visible();
29+
$books = $this->queries->visibleForList();
2830

2931
return $this->apiListingResponse($books, [
3032
'id', 'name', 'slug', 'description', 'created_at', 'updated_at', 'created_by', 'updated_by', 'owned_by',
@@ -56,7 +58,7 @@ public function create(Request $request)
5658
*/
5759
public function read(string $id)
5860
{
59-
$book = Book::visible()->findOrFail($id);
61+
$book = $this->queries->findVisibleByIdOrFail(intval($id));
6062
$book = $this->forJsonDisplay($book);
6163
$book->load(['createdBy', 'updatedBy', 'ownedBy']);
6264

@@ -83,7 +85,7 @@ public function read(string $id)
8385
*/
8486
public function update(Request $request, string $id)
8587
{
86-
$book = Book::visible()->findOrFail($id);
88+
$book = $this->queries->findVisibleByIdOrFail(intval($id));
8789
$this->checkOwnablePermission('book-update', $book);
8890

8991
$requestData = $this->validate($request, $this->rules()['update']);
@@ -100,7 +102,7 @@ public function update(Request $request, string $id)
100102
*/
101103
public function delete(string $id)
102104
{
103-
$book = Book::visible()->findOrFail($id);
105+
$book = $this->queries->findVisibleByIdOrFail(intval($id));
104106
$this->checkOwnablePermission('book-delete', $book);
105107

106108
$this->bookRepo->destroy($book);

app/Entities/Controllers/BookExportApiController.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22

33
namespace BookStack\Entities\Controllers;
44

5-
use BookStack\Entities\Models\Book;
5+
use BookStack\Entities\Queries\BookQueries;
66
use BookStack\Entities\Tools\ExportFormatter;
77
use BookStack\Http\ApiController;
88
use Throwable;
99

1010
class BookExportApiController extends ApiController
1111
{
12-
protected $exportFormatter;
13-
14-
public function __construct(ExportFormatter $exportFormatter)
15-
{
16-
$this->exportFormatter = $exportFormatter;
12+
public function __construct(
13+
protected ExportFormatter $exportFormatter,
14+
protected BookQueries $queries,
15+
) {
1716
$this->middleware('can:content-export');
1817
}
1918

@@ -24,7 +23,7 @@ public function __construct(ExportFormatter $exportFormatter)
2423
*/
2524
public function exportPdf(int $id)
2625
{
27-
$book = Book::visible()->findOrFail($id);
26+
$book = $this->queries->findVisibleByIdOrFail($id);
2827
$pdfContent = $this->exportFormatter->bookToPdf($book);
2928

3029
return $this->download()->directly($pdfContent, $book->slug . '.pdf');
@@ -37,7 +36,7 @@ public function exportPdf(int $id)
3736
*/
3837
public function exportHtml(int $id)
3938
{
40-
$book = Book::visible()->findOrFail($id);
39+
$book = $this->queries->findVisibleByIdOrFail($id);
4140
$htmlContent = $this->exportFormatter->bookToContainedHtml($book);
4241

4342
return $this->download()->directly($htmlContent, $book->slug . '.html');
@@ -48,7 +47,7 @@ public function exportHtml(int $id)
4847
*/
4948
public function exportPlainText(int $id)
5049
{
51-
$book = Book::visible()->findOrFail($id);
50+
$book = $this->queries->findVisibleByIdOrFail($id);
5251
$textContent = $this->exportFormatter->bookToPlainText($book);
5352

5453
return $this->download()->directly($textContent, $book->slug . '.txt');
@@ -59,7 +58,7 @@ public function exportPlainText(int $id)
5958
*/
6059
public function exportMarkdown(int $id)
6160
{
62-
$book = Book::visible()->findOrFail($id);
61+
$book = $this->queries->findVisibleByIdOrFail($id);
6362
$markdown = $this->exportFormatter->bookToMarkdown($book);
6463

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

app/Entities/Controllers/BookshelfController.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use BookStack\Activity\ActivityQueries;
66
use BookStack\Activity\Models\View;
7-
use BookStack\Entities\Models\Book;
7+
use BookStack\Entities\Queries\BookQueries;
88
use BookStack\Entities\Queries\BookshelfQueries;
99
use BookStack\Entities\Repos\BookshelfRepo;
1010
use BookStack\Entities\Tools\ShelfContext;
@@ -22,6 +22,7 @@ class BookshelfController extends Controller
2222
public function __construct(
2323
protected BookshelfRepo $shelfRepo,
2424
protected BookshelfQueries $queries,
25+
protected BookQueries $bookQueries,
2526
protected ShelfContext $shelfContext,
2627
protected ReferenceFetcher $referenceFetcher,
2728
) {
@@ -68,7 +69,7 @@ public function index(Request $request)
6869
public function create()
6970
{
7071
$this->checkPermission('bookshelf-create-all');
71-
$books = Book::visible()->orderBy('name')->get(['name', 'id', 'slug', 'created_at', 'updated_at']);
72+
$books = $this->bookQueries->visibleForList()->orderBy('name')->get(['name', 'id', 'slug', 'created_at', 'updated_at']);
7273
$this->setPageTitle(trans('entities.shelves_create'));
7374

7475
return view('shelves.create', ['books' => $books]);
@@ -145,7 +146,10 @@ public function edit(string $slug)
145146
$this->checkOwnablePermission('bookshelf-update', $shelf);
146147

147148
$shelfBookIds = $shelf->books()->get(['id'])->pluck('id');
148-
$books = Book::visible()->whereNotIn('id', $shelfBookIds)->orderBy('name')->get(['name', 'id', 'slug', 'created_at', 'updated_at']);
149+
$books = $this->bookQueries->visibleForList()
150+
->whereNotIn('id', $shelfBookIds)
151+
->orderBy('name')
152+
->get(['name', 'id', 'slug', 'created_at', 'updated_at']);
149153

150154
$this->setPageTitle(trans('entities.shelves_edit_named', ['name' => $shelf->getShortName()]));
151155

app/Entities/Controllers/ChapterApiController.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace BookStack\Entities\Controllers;
44

5-
use BookStack\Entities\Models\Book;
65
use BookStack\Entities\Models\Chapter;
6+
use BookStack\Entities\Queries\BookQueries;
7+
use BookStack\Entities\Queries\ChapterQueries;
78
use BookStack\Entities\Repos\ChapterRepo;
89
use BookStack\Exceptions\PermissionsException;
910
use BookStack\Http\ApiController;
@@ -35,7 +36,9 @@ class ChapterApiController extends ApiController
3536
];
3637

3738
public function __construct(
38-
protected ChapterRepo $chapterRepo
39+
protected ChapterRepo $chapterRepo,
40+
protected ChapterQueries $queries,
41+
protected BookQueries $bookQueries,
3942
) {
4043
}
4144

@@ -44,7 +47,7 @@ public function __construct(
4447
*/
4548
public function list()
4649
{
47-
$chapters = Chapter::visible();
50+
$chapters = $this->queries->visibleForList();
4851

4952
return $this->apiListingResponse($chapters, [
5053
'id', 'book_id', 'name', 'slug', 'description', 'priority',
@@ -60,7 +63,7 @@ public function create(Request $request)
6063
$requestData = $this->validate($request, $this->rules['create']);
6164

6265
$bookId = $request->get('book_id');
63-
$book = Book::visible()->findOrFail($bookId);
66+
$book = $this->bookQueries->findVisibleByIdOrFail(intval($bookId));
6467
$this->checkOwnablePermission('chapter-create', $book);
6568

6669
$chapter = $this->chapterRepo->create($requestData, $book);
@@ -73,7 +76,7 @@ public function create(Request $request)
7376
*/
7477
public function read(string $id)
7578
{
76-
$chapter = Chapter::visible()->findOrFail($id);
79+
$chapter = $this->queries->findVisibleByIdOrFail(intval($id));
7780
$chapter = $this->forJsonDisplay($chapter);
7881

7982
$chapter->load([
@@ -94,7 +97,7 @@ public function read(string $id)
9497
public function update(Request $request, string $id)
9598
{
9699
$requestData = $this->validate($request, $this->rules()['update']);
97-
$chapter = Chapter::visible()->findOrFail($id);
100+
$chapter = $this->queries->findVisibleByIdOrFail(intval($id));
98101
$this->checkOwnablePermission('chapter-update', $chapter);
99102

100103
if ($request->has('book_id') && $chapter->book_id !== intval($requestData['book_id'])) {
@@ -122,7 +125,7 @@ public function update(Request $request, string $id)
122125
*/
123126
public function delete(string $id)
124127
{
125-
$chapter = Chapter::visible()->findOrFail($id);
128+
$chapter = $this->queries->findVisibleByIdOrFail(intval($id));
126129
$this->checkOwnablePermission('chapter-delete', $chapter);
127130

128131
$this->chapterRepo->destroy($chapter);

app/Entities/Controllers/PageApiController.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
namespace BookStack\Entities\Controllers;
44

5-
use BookStack\Entities\Models\Book;
6-
use BookStack\Entities\Models\Chapter;
7-
use BookStack\Entities\Models\Page;
5+
use BookStack\Entities\Queries\EntityQueries;
86
use BookStack\Entities\Queries\PageQueries;
97
use BookStack\Entities\Repos\PageRepo;
108
use BookStack\Exceptions\PermissionsException;
@@ -38,6 +36,7 @@ class PageApiController extends ApiController
3836
public function __construct(
3937
protected PageRepo $pageRepo,
4038
protected PageQueries $queries,
39+
protected EntityQueries $entityQueries,
4140
) {
4241
}
4342

@@ -46,7 +45,7 @@ public function __construct(
4645
*/
4746
public function list()
4847
{
49-
$pages = Page::visible();
48+
$pages = $this->queries->visibleForList();
5049

5150
return $this->apiListingResponse($pages, [
5251
'id', 'book_id', 'chapter_id', 'name', 'slug', 'priority',
@@ -72,9 +71,9 @@ public function create(Request $request)
7271
$this->validate($request, $this->rules['create']);
7372

7473
if ($request->has('chapter_id')) {
75-
$parent = Chapter::visible()->findOrFail($request->get('chapter_id'));
74+
$parent = $this->entityQueries->chapters->findVisibleByIdOrFail(intval($request->get('chapter_id')));
7675
} else {
77-
$parent = Book::visible()->findOrFail($request->get('book_id'));
76+
$parent = $this->entityQueries->books->findVisibleByIdOrFail(intval($request->get('book_id')));
7877
}
7978
$this->checkOwnablePermission('page-create', $parent);
8079

@@ -120,9 +119,9 @@ public function update(Request $request, string $id)
120119

121120
$parent = null;
122121
if ($request->has('chapter_id')) {
123-
$parent = Chapter::visible()->findOrFail($request->get('chapter_id'));
122+
$parent = $this->entityQueries->chapters->findVisibleByIdOrFail(intval($request->get('chapter_id')));
124123
} elseif ($request->has('book_id')) {
125-
$parent = Book::visible()->findOrFail($request->get('book_id'));
124+
$parent = $this->entityQueries->books->findVisibleByIdOrFail(intval($request->get('book_id')));
126125
}
127126

128127
if ($parent && !$parent->matches($page->getParent())) {

app/Entities/Controllers/PageController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ public function showDelete(string $bookSlug, string $pageSlug)
276276
$this->checkOwnablePermission('page-delete', $page);
277277
$this->setPageTitle(trans('entities.pages_delete_named', ['pageName' => $page->getShortName()]));
278278
$usedAsTemplate =
279-
Book::query()->where('default_template_id', '=', $page->id)->count() > 0 ||
280-
Chapter::query()->where('default_template_id', '=', $page->id)->count() > 0;
279+
$this->entityQueries->books->start()->where('default_template_id', '=', $page->id)->count() > 0 ||
280+
$this->entityQueries->chapters->start()->where('default_template_id', '=', $page->id)->count() > 0;
281281

282282
return view('pages.delete', [
283283
'book' => $page->book,
@@ -298,8 +298,8 @@ public function showDeleteDraft(string $bookSlug, int $pageId)
298298
$this->checkOwnablePermission('page-update', $page);
299299
$this->setPageTitle(trans('entities.pages_delete_draft_named', ['pageName' => $page->getShortName()]));
300300
$usedAsTemplate =
301-
Book::query()->where('default_template_id', '=', $page->id)->count() > 0 ||
302-
Chapter::query()->where('default_template_id', '=', $page->id)->count() > 0;
301+
$this->entityQueries->books->start()->where('default_template_id', '=', $page->id)->count() > 0 ||
302+
$this->entityQueries->chapters->start()->where('default_template_id', '=', $page->id)->count() > 0;
303303

304304
return view('pages.delete', [
305305
'book' => $page->book,

app/Entities/Controllers/RecycleBinController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ public function destroy(DeletionRepo $deletionRepo, string $id)
116116
*
117117
* @throws \Exception
118118
*/
119-
public function empty()
119+
public function empty(TrashCan $trash)
120120
{
121-
$deleteCount = (new TrashCan())->empty();
121+
$deleteCount = $trash->empty();
122122

123123
$this->logActivity(ActivityType::RECYCLE_BIN_EMPTY);
124124
$this->showSuccessNotification(trans('settings.recycle_bin_destroy_notification', ['count' => $deleteCount]));

app/Entities/Models/Book.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,20 +117,11 @@ public function shelves(): BelongsToMany
117117
/**
118118
* Get the direct child items within this book.
119119
*/
120-
public function getDirectChildren(): Collection
120+
public function getDirectVisibleChildren(): Collection
121121
{
122122
$pages = $this->directPages()->scopes('visible')->get();
123123
$chapters = $this->chapters()->scopes('visible')->get();
124124

125125
return $pages->concat($chapters)->sortBy('priority')->sortByDesc('draft');
126126
}
127-
128-
/**
129-
* Get a visible book by its slug.
130-
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
131-
*/
132-
public static function getBySlug(string $slug): self
133-
{
134-
return static::visible()->where('slug', '=', $slug)->firstOrFail();
135-
}
136127
}

app/Entities/Queries/BookQueries.php

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

21+
public function findVisibleByIdOrFail(int $id): Book
22+
{
23+
return $this->start()->scopes('visible')->findOrFail($id);
24+
}
25+
2126
public function findVisibleBySlugOrFail(string $slug): Book
2227
{
2328
/** @var ?Book $book */

0 commit comments

Comments
 (0)