Skip to content

Commit 3886aed

Browse files
committed
Queries: Migrated bookshelf repo queries to new class
1 parent 1559b0a commit 3886aed

File tree

5 files changed

+80
-79
lines changed

5 files changed

+80
-79
lines changed

app/App/HomeController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use BookStack\Entities\Queries\EntityQueries;
99
use BookStack\Entities\Queries\RecentlyViewed;
1010
use BookStack\Entities\Queries\TopFavourites;
11-
use BookStack\Entities\Repos\BookshelfRepo;
1211
use BookStack\Entities\Tools\PageContent;
1312
use BookStack\Http\Controller;
1413
use BookStack\Uploads\FaviconHandler;
@@ -80,7 +79,9 @@ public function index(Request $request, ActivityQueries $activities)
8079
}
8180

8281
if ($homepageOption === 'bookshelves') {
83-
$shelves = app()->make(BookshelfRepo::class)->getAllPaginated(18, $commonData['listOptions']->getSort(), $commonData['listOptions']->getOrder());
82+
$shelves = $this->queries->shelves->visibleForListWithCover()
83+
->orderBy($commonData['listOptions']->getSort(), $commonData['listOptions']->getOrder())
84+
->paginate(18);
8485
$data = array_merge($commonData, ['shelves' => $shelves]);
8586

8687
return view('home.shelves', $data);

app/Entities/Controllers/BookshelfController.php

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use BookStack\Activity\ActivityQueries;
66
use BookStack\Activity\Models\View;
77
use BookStack\Entities\Models\Book;
8+
use BookStack\Entities\Queries\BookshelfQueries;
89
use BookStack\Entities\Repos\BookshelfRepo;
910
use BookStack\Entities\Tools\ShelfContext;
1011
use BookStack\Exceptions\ImageUploadException;
@@ -20,8 +21,9 @@ class BookshelfController extends Controller
2021
{
2122
public function __construct(
2223
protected BookshelfRepo $shelfRepo,
24+
protected BookshelfQueries $queries,
2325
protected ShelfContext $shelfContext,
24-
protected ReferenceFetcher $referenceFetcher
26+
protected ReferenceFetcher $referenceFetcher,
2527
) {
2628
}
2729

@@ -37,10 +39,15 @@ public function index(Request $request)
3739
'updated_at' => trans('common.sort_updated_at'),
3840
]);
3941

40-
$shelves = $this->shelfRepo->getAllPaginated(18, $listOptions->getSort(), $listOptions->getOrder());
41-
$recents = $this->isSignedIn() ? $this->shelfRepo->getRecentlyViewed(4) : false;
42-
$popular = $this->shelfRepo->getPopular(4);
43-
$new = $this->shelfRepo->getRecentlyCreated(4);
42+
$shelves = $this->queries->visibleForListWithCover()
43+
->orderBy($listOptions->getSort(), $listOptions->getOrder())
44+
->paginate(18);
45+
$recents = $this->isSignedIn() ? $this->queries->recentlyViewedForCurrentUser()->get() : false;
46+
$popular = $this->queries->popularForList()->get();
47+
$new = $this->queries->visibleForList()
48+
->orderBy('created_at', 'desc')
49+
->take(4)
50+
->get();
4451

4552
$this->shelfContext->clearShelfContext();
4653
$this->setPageTitle(trans('entities.shelves'));
@@ -96,7 +103,7 @@ public function store(Request $request)
96103
*/
97104
public function show(Request $request, ActivityQueries $activities, string $slug)
98105
{
99-
$shelf = $this->shelfRepo->getBySlug($slug);
106+
$shelf = $this->queries->findVisibleBySlug($slug);
100107
$this->checkOwnablePermission('bookshelf-view', $shelf);
101108

102109
$listOptions = SimpleListOptions::fromRequest($request, 'shelf_books')->withSortOptions([
@@ -134,7 +141,7 @@ public function show(Request $request, ActivityQueries $activities, string $slug
134141
*/
135142
public function edit(string $slug)
136143
{
137-
$shelf = $this->shelfRepo->getBySlug($slug);
144+
$shelf = $this->queries->findVisibleBySlug($slug);
138145
$this->checkOwnablePermission('bookshelf-update', $shelf);
139146

140147
$shelfBookIds = $shelf->books()->get(['id'])->pluck('id');
@@ -157,7 +164,7 @@ public function edit(string $slug)
157164
*/
158165
public function update(Request $request, string $slug)
159166
{
160-
$shelf = $this->shelfRepo->getBySlug($slug);
167+
$shelf = $this->queries->findVisibleBySlug($slug);
161168
$this->checkOwnablePermission('bookshelf-update', $shelf);
162169
$validated = $this->validate($request, [
163170
'name' => ['required', 'string', 'max:255'],
@@ -183,7 +190,7 @@ public function update(Request $request, string $slug)
183190
*/
184191
public function showDelete(string $slug)
185192
{
186-
$shelf = $this->shelfRepo->getBySlug($slug);
193+
$shelf = $this->queries->findVisibleBySlug($slug);
187194
$this->checkOwnablePermission('bookshelf-delete', $shelf);
188195

189196
$this->setPageTitle(trans('entities.shelves_delete_named', ['name' => $shelf->getShortName()]));
@@ -198,7 +205,7 @@ public function showDelete(string $slug)
198205
*/
199206
public function destroy(string $slug)
200207
{
201-
$shelf = $this->shelfRepo->getBySlug($slug);
208+
$shelf = $this->queries->findVisibleBySlug($slug);
202209
$this->checkOwnablePermission('bookshelf-delete', $shelf);
203210

204211
$this->shelfRepo->destroy($shelf);
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace BookStack\Entities\Queries;
4+
5+
use BookStack\Entities\Models\Bookshelf;
6+
use BookStack\Exceptions\NotFoundException;
7+
use Illuminate\Database\Eloquent\Builder;
8+
9+
class BookshelfQueries
10+
{
11+
public function start(): Builder
12+
{
13+
return Bookshelf::query();
14+
}
15+
16+
public function findVisibleBySlug(string $slug): Bookshelf
17+
{
18+
/** @var ?Bookshelf $shelf */
19+
$shelf = $this->start()
20+
->scopes('visible')
21+
->where('slug', '=', $slug)
22+
->first();
23+
24+
if ($shelf === null) {
25+
throw new NotFoundException(trans('errors.bookshelf_not_found'));
26+
}
27+
28+
return $shelf;
29+
}
30+
31+
public function visibleForList(): Builder
32+
{
33+
return $this->start()->scopes('visible');
34+
}
35+
36+
public function visibleForListWithCover(): Builder
37+
{
38+
return $this->visibleForList()->with('cover');
39+
}
40+
41+
public function recentlyViewedForCurrentUser(): Builder
42+
{
43+
return $this->visibleForList()
44+
->scopes('withLastView')
45+
->having('last_viewed_at', '>', 0)
46+
->orderBy('last_viewed_at', 'desc');
47+
}
48+
49+
public function popularForList(): Builder
50+
{
51+
return $this->visibleForList()
52+
->scopes('withViewCount')
53+
->having('view_count', '>', 0)
54+
->orderBy('view_count', 'desc');
55+
}
56+
}

app/Entities/Queries/EntityQueries.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
class EntityQueries
66
{
77
public function __construct(
8+
public BookshelfQueries $shelves,
89
public BookQueries $books,
910
public PageQueries $pages,
1011
) {

app/Entities/Repos/BookshelfRepo.php

Lines changed: 3 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -6,78 +6,14 @@
66
use BookStack\Entities\Models\Book;
77
use BookStack\Entities\Models\Bookshelf;
88
use BookStack\Entities\Tools\TrashCan;
9-
use BookStack\Exceptions\NotFoundException;
109
use BookStack\Facades\Activity;
1110
use Exception;
12-
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
13-
use Illuminate\Support\Collection;
1411

1512
class BookshelfRepo
1613
{
17-
protected $baseRepo;
18-
19-
/**
20-
* BookshelfRepo constructor.
21-
*/
22-
public function __construct(BaseRepo $baseRepo)
23-
{
24-
$this->baseRepo = $baseRepo;
25-
}
26-
27-
/**
28-
* Get all bookshelves in a paginated format.
29-
*/
30-
public function getAllPaginated(int $count = 20, string $sort = 'name', string $order = 'asc'): LengthAwarePaginator
31-
{
32-
return Bookshelf::visible()
33-
->with(['visibleBooks', 'cover'])
34-
->orderBy($sort, $order)
35-
->paginate($count);
36-
}
37-
38-
/**
39-
* Get the bookshelves that were most recently viewed by this user.
40-
*/
41-
public function getRecentlyViewed(int $count = 20): Collection
42-
{
43-
return Bookshelf::visible()->withLastView()
44-
->having('last_viewed_at', '>', 0)
45-
->orderBy('last_viewed_at', 'desc')
46-
->take($count)->get();
47-
}
48-
49-
/**
50-
* Get the most popular bookshelves in the system.
51-
*/
52-
public function getPopular(int $count = 20): Collection
53-
{
54-
return Bookshelf::visible()->withViewCount()
55-
->having('view_count', '>', 0)
56-
->orderBy('view_count', 'desc')
57-
->take($count)->get();
58-
}
59-
60-
/**
61-
* Get the most recently created bookshelves from the system.
62-
*/
63-
public function getRecentlyCreated(int $count = 20): Collection
64-
{
65-
return Bookshelf::visible()->orderBy('created_at', 'desc')
66-
->take($count)->get();
67-
}
68-
69-
/**
70-
* Get a shelf by its slug.
71-
*/
72-
public function getBySlug(string $slug): Bookshelf
73-
{
74-
$shelf = Bookshelf::visible()->where('slug', '=', $slug)->first();
75-
76-
if ($shelf === null) {
77-
throw new NotFoundException(trans('errors.bookshelf_not_found'));
78-
}
79-
80-
return $shelf;
14+
public function __construct(
15+
protected BaseRepo $baseRepo,
16+
) {
8117
}
8218

8319
/**

0 commit comments

Comments
 (0)