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