mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-11-25 17:57:28 +08:00
8e78b4c43e
Updated query classes to align to interface for common aligned operations. Extracted repeated string-identifier-based finding from page/chapter repos to shared higher-level entity queries.
62 lines
1.5 KiB
PHP
62 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace BookStack\Entities\Queries;
|
|
|
|
use BookStack\Entities\Models\Bookshelf;
|
|
use BookStack\Exceptions\NotFoundException;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
class BookshelfQueries implements ProvidesEntityQueries
|
|
{
|
|
public function start(): Builder
|
|
{
|
|
return Bookshelf::query();
|
|
}
|
|
|
|
public function findVisibleById(int $id): ?Bookshelf
|
|
{
|
|
return $this->start()->scopes('visible')->find($id);
|
|
}
|
|
|
|
public function findVisibleBySlug(string $slug): Bookshelf
|
|
{
|
|
/** @var ?Bookshelf $shelf */
|
|
$shelf = $this->start()
|
|
->scopes('visible')
|
|
->where('slug', '=', $slug)
|
|
->first();
|
|
|
|
if ($shelf === null) {
|
|
throw new NotFoundException(trans('errors.bookshelf_not_found'));
|
|
}
|
|
|
|
return $shelf;
|
|
}
|
|
|
|
public function visibleForList(): Builder
|
|
{
|
|
return $this->start()->scopes('visible');
|
|
}
|
|
|
|
public function visibleForListWithCover(): Builder
|
|
{
|
|
return $this->visibleForList()->with('cover');
|
|
}
|
|
|
|
public function recentlyViewedForCurrentUser(): Builder
|
|
{
|
|
return $this->visibleForList()
|
|
->scopes('withLastView')
|
|
->having('last_viewed_at', '>', 0)
|
|
->orderBy('last_viewed_at', 'desc');
|
|
}
|
|
|
|
public function popularForList(): Builder
|
|
{
|
|
return $this->visibleForList()
|
|
->scopes('withViewCount')
|
|
->having('view_count', '>', 0)
|
|
->orderBy('view_count', 'desc');
|
|
}
|
|
}
|