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', 'created_at', 'updated_at', 'image_id'
15 public function start(): Builder
17 return Bookshelf::query();
20 public function findVisibleById(int $id): ?Bookshelf
22 return $this->start()->scopes('visible')->find($id);
25 public function findVisibleByIdOrFail(int $id): Bookshelf
27 $shelf = $this->findVisibleById($id);
29 if (is_null($shelf)) {
30 throw new NotFoundException(trans('errors.bookshelf_not_found'));
36 public function findVisibleBySlugOrFail(string $slug): Bookshelf
38 /** @var ?Bookshelf $shelf */
39 $shelf = $this->start()
41 ->where('slug', '=', $slug)
44 if ($shelf === null) {
45 throw new NotFoundException(trans('errors.bookshelf_not_found'));
51 public function visibleForList(): Builder
53 return $this->start()->scopes('visible')->select(static::$listAttributes);
56 public function visibleForListWithCover(): Builder
58 return $this->visibleForList()->with('cover');
61 public function recentlyViewedForCurrentUser(): Builder
63 return $this->visibleForList()
64 ->scopes('withLastView')
65 ->having('last_viewed_at', '>', 0)
66 ->orderBy('last_viewed_at', 'desc');
69 public function popularForList(): Builder
71 return $this->visibleForList()
72 ->scopes('withViewCount')
73 ->having('view_count', '>', 0)
74 ->orderBy('view_count', 'desc');