]> BookStack Code Mirror - bookstack/blob - app/Entities/Queries/BookshelfQueries.php
7edff45b958a5e7a5ee23878a4f608143d62e9fa
[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
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 }