]> BookStack Code Mirror - bookstack/blob - app/Entities/Queries/BookQueries.php
6de28f0c2640dc96c131ac69946b0615f152a260
[bookstack] / app / Entities / Queries / BookQueries.php
1 <?php
2
3 namespace BookStack\Entities\Queries;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Exceptions\NotFoundException;
7 use Illuminate\Database\Eloquent\Builder;
8
9 class BookQueries
10 {
11     public function start(): Builder
12     {
13         return Book::query();
14     }
15
16     public function findVisibleBySlug(string $slug): Book
17     {
18         /** @var ?Book $book */
19         $book = $this->start()
20             ->scopes('visible')
21             ->where('slug', '=', $slug)
22             ->first();
23
24         if ($book === null) {
25             throw new NotFoundException(trans('errors.book_not_found'));
26         }
27
28         return $book;
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 }