mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-12-14 23:23:37 +08:00
1559b0acd1
Also moved to a non-static approach, and added a high-level class to allow easy access to all other entity queries, for use in mixed-entity scenarios and easier/simpler injection.
57 lines
1.3 KiB
PHP
57 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace BookStack\Entities\Queries;
|
|
|
|
use BookStack\Entities\Models\Book;
|
|
use BookStack\Exceptions\NotFoundException;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
class BookQueries
|
|
{
|
|
public function start(): Builder
|
|
{
|
|
return Book::query();
|
|
}
|
|
|
|
public function findVisibleBySlug(string $slug): Book
|
|
{
|
|
/** @var ?Book $book */
|
|
$book = $this->start()
|
|
->scopes('visible')
|
|
->where('slug', '=', $slug)
|
|
->first();
|
|
|
|
if ($book === null) {
|
|
throw new NotFoundException(trans('errors.book_not_found'));
|
|
}
|
|
|
|
return $book;
|
|
}
|
|
|
|
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');
|
|
}
|
|
}
|