3 namespace BookStack\Entities\Queries;
5 use BookStack\Entities\Models\Bookshelf;
6 use BookStack\Exceptions\NotFoundException;
7 use Illuminate\Database\Eloquent\Builder;
9 class BookshelfQueries implements ProvidesEntityQueries
11 protected static array $listAttributes = [
12 'id', 'slug', 'name', 'description',
13 'created_at', 'updated_at', 'image_id', 'owned_by',
16 public function start(): Builder
18 return Bookshelf::query();
21 public function findVisibleById(int $id): ?Bookshelf
23 return $this->start()->scopes('visible')->find($id);
26 public function findVisibleByIdOrFail(int $id): Bookshelf
28 $shelf = $this->findVisibleById($id);
30 if (is_null($shelf)) {
31 throw new NotFoundException(trans('errors.bookshelf_not_found'));
37 public function findVisibleBySlugOrFail(string $slug): Bookshelf
39 /** @var ?Bookshelf $shelf */
40 $shelf = $this->start()
42 ->where('slug', '=', $slug)
45 if ($shelf === null) {
46 throw new NotFoundException(trans('errors.bookshelf_not_found'));
52 public function visibleForList(): Builder
54 return $this->start()->scopes('visible')->select(static::$listAttributes);
57 public function visibleForListWithCover(): Builder
59 return $this->visibleForList()->with('cover');
62 public function recentlyViewedForCurrentUser(): Builder
64 return $this->visibleForList()
65 ->scopes('withLastView')
66 ->having('last_viewed_at', '>', 0)
67 ->orderBy('last_viewed_at', 'desc');
70 public function popularForList(): Builder
72 return $this->visibleForList()
73 ->scopes('withViewCount')
74 ->having('view_count', '>', 0)
75 ->orderBy('view_count', 'desc');