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