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