]> BookStack Code Mirror - bookstack/blob - app/Entities/Queries/BookshelfQueries.php
Queries: Extracted PageRepo queries to own class
[bookstack] / app / Entities / Queries / BookshelfQueries.php
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 implements ProvidesEntityQueries
10 {
11     public function start(): Builder
12     {
13         return Bookshelf::query();
14     }
15
16     public function findVisibleById(int $id): ?Bookshelf
17     {
18         return $this->start()->scopes('visible')->find($id);
19     }
20
21     public function findVisibleBySlugOrFail(string $slug): Bookshelf
22     {
23         /** @var ?Bookshelf $shelf */
24         $shelf = $this->start()
25             ->scopes('visible')
26             ->where('slug', '=', $slug)
27             ->first();
28
29         if ($shelf === null) {
30             throw new NotFoundException(trans('errors.bookshelf_not_found'));
31         }
32
33         return $shelf;
34     }
35
36     public function visibleForList(): Builder
37     {
38         return $this->start()->scopes('visible');
39     }
40
41     public function visibleForListWithCover(): Builder
42     {
43         return $this->visibleForList()->with('cover');
44     }
45
46     public function recentlyViewedForCurrentUser(): Builder
47     {
48         return $this->visibleForList()
49             ->scopes('withLastView')
50             ->having('last_viewed_at', '>', 0)
51             ->orderBy('last_viewed_at', 'desc');
52     }
53
54     public function popularForList(): Builder
55     {
56         return $this->visibleForList()
57             ->scopes('withViewCount')
58             ->having('view_count', '>', 0)
59             ->orderBy('view_count', 'desc');
60     }
61 }